⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 par4ch.c

📁 基于PC104的24位数据采集器的完整源码
💻 C
📖 第 1 页 / 共 4 页
字号:
/* FILE: par4ch.c                 Copyright (c), Symmetric Research, 2001

These are the PAR4CH user library functions.  They are mostly
wrappers around the lower level read, write, and ioctl device driver
functions that do the actual work of controlling the PAR4CH.  Users
should limit themselves to calling functions listed in this file
unless they are EXTREMELY EXPERT at calling device driver code
directly.

To use this library, just compile it to an object file and statically
link it with your application.  For OS's other than DOS, it can also
be compiled to a DLL or shared object library.  See the makefiles for
examples of how to compile.

This single source code files services all the supported operating
systems.  When compiling, define one of the SROS_xxxxx constants
shown in par4ch.h on the compiler command line to indicate which OS
you are using.

The location and use of the low level device driver code varies
depending on which OS you are using.  For DOS and Win9x, the driver
code is located in par4chkd.obj and should be linked in statically.
For NT and Linux, the driver code is provided as a kernel mode device
driver and must be installed using the indriver utility in the driver
directory.  For NT, the driver is named par4chkd.sys and for Linux,
it is named par4chkd.o.  See the driver directory for more details.

*/


#include <stdio.h>


// OS dependent includes ...

#if defined( SROS_WIN2K ) || defined( SROS_WINNT )
#include <windows.h>     // windef.h -> winnt.h -> defines GENERIC_READ etc
#include <winioctl.h>    // device driver I/O control macros
#include <sys\stat.h>    // fstat function
#include <io.h>          // access function


#elif defined( SROS_WIN95 )
#include <sys\stat.h>    // fstat function
#include <io.h>          // access, commit functions


#elif defined( SROS_MSDOS )
#include <sys\timeb.h>   // ftime function
#include <sys\stat.h>    // fstat function
#include <io.h>          // access, commit functions


#elif defined( SROS_LINUX )
#include <errno.h>       // errno global
#include <fcntl.h>       // open  function
#include <unistd.h>      // close, access, fsync fns and STDIN_FILENO define
#include <sys/ioctl.h>   // ioctl function and macros
#include <sys/stat.h>    // fstat function


#endif  // SROS_xxxxx



// Par4ch includes ...

#include "par4ch.h"    // for PAR4CH defines and prototypes
#include "par4chkd.h"  // for PAR4CH ioctl id constants



// Actual allocation of PAR4CH error string array, see par4ch.h ...

VARTYPE( char ) *PAR4CH_ERROR_MSG[] = {

        PAR4CH_ERROR_MSG_NONE,
        PAR4CH_ERROR_MSG_PORT_ADDRESS,
        PAR4CH_ERROR_MSG_PORT_MODE,
        PAR4CH_ERROR_MSG_DATA_FORMAT,
        PAR4CH_ERROR_MSG_CAL_MODE,
        PAR4CH_ERROR_MSG_GAIN,
        PAR4CH_ERROR_MSG_TURBO,
        PAR4CH_ERROR_MSG_GAIN_TURBO_PRODUCT,
        PAR4CH_ERROR_MSG_DECIMATION,
        PAR4CH_ERROR_MSG_DRIVER_NOT_OPEN,
        PAR4CH_ERROR_MSG_DRIVER_REQUEST_FAILED,
        PAR4CH_ERROR_MSG_VOLTAGE_BAD_ON,
        PAR4CH_ERROR_MSG_VOLTAGE_BAD_OFF,
        PAR4CH_ERROR_MSG_NOT_UNLOCKED,
        PAR4CH_ERROR_MSG_ADS1210_CR_READBACK,
        PAR4CH_ERROR_MSG_FIFO_SANITY,
        PAR4CH_ERROR_MSG_OVERFLOW,
        PAR4CH_ERROR_MSG_KEYPRESS,
        PAR4CH_ERROR_MSG_MAX

        };




/* RELEASE REV INFO:

This function allows the user to determine the library rev they are
working with.  See par4ch.h for the define of PAR4CH_REV.

Short rev history:

  PAR4CH_REV  100  ( 07/01/2000 )  < first release, EPP only
  PAR4CH_REV  101  ( 10/01/2000 )  < added BPP support, and NT
  PAR4CH_REV  110  ( 12/20/2000 )  < Toshiba fix
  PAR4CH_REV  201  ( 03/15/2001 )  < major changes, added open etc ...
  PAR4CH_REV  210  ( 04/20/2001 )  < added board unlock, port timeout reset
  PAR4CH_REV  211  ( 04/25/2001 )  < added DRAM FIFO empty flag skew
  PAR4CH_REV  212  ( 05/05/2001 )  < BPP unlock and no voltage bad check
  PAR4CH_REV  213  ( 05/30/2001 )  < ECP support
  PAR4CH_REV  214  ( 12/15/2001 )  < Win2K ACPI support, SrWait for MSDOS

*/

#define PAR4CH_REV      214

FUNCTYPE( void ) Par4chGetRev( int *Rev ) {

        *Rev = PAR4CH_REV;
        return;
}












/* LOCAL DEFINE:

This define controls whether the library helper functions are visible
to other source files.  Most users should let SRLOCAL be defined as
static to avoid namespace clutter and possible function name
contention.  The only exceptions would be for specialized debugging
and diagnostic programs.

*/

#if !defined( SRLOCAL )
#define SRLOCAL static
#endif






/* OS DEPENDENT HELPER FUNCTIONS:
 *
 * These functions help hide OS dependencies from the rest of
 * the library by providing a common interface.  The functions
 * are repeated once for each supported OS, but the SROS_xxxxx
 * defines select only one set to be compiled in to the code.
 * 
 * Supported OS's include
 *
 *     SROS_WIN2K  Windows 2000
 *     SROS_WINNT  Windows NT 4.0
 *     SROS_WIN95  Windows 95, 98, and ME
 *     SROS_MSDOS  MS Dos
 *     SROS_LINUX  Linux 2.2.12 kernel (RedHat 6.0,6.1,6.2)
 *
 *
 */

#if defined( SROS_WIN2K ) || defined( SROS_WINNT )

SRLOCAL DEVHANDLE OsDriverOpen( char *PortName ) {

        DEVHANDLE SrHandle;
        HANDLE    NtHandle;
        char      DeviceNameBuffer[256];

        sprintf( DeviceNameBuffer, "\\\\.\\%s", PortName );

        NtHandle = CreateFile(
                        DeviceNameBuffer,                   // "file" name
                        GENERIC_READ    | GENERIC_WRITE,    // access mode
                        FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
                        NULL,                               // security
                        OPEN_EXISTING,                      // create mode
                        0,                                  // file attrib
                        NULL                                // copy from
                        );

        if (NtHandle == INVALID_HANDLE_VALUE)
                SrHandle = BAD_DEVHANDLE;
        else
                SrHandle = (DEVHANDLE)NtHandle;
        
        return( SrHandle );
}

SRLOCAL int OsDriverClose( DEVHANDLE Par4chHandle ) {
        return( CloseHandle( Par4chHandle ) );
}

SRLOCAL int OsDriverRead(
                          DEVHANDLE      Par4chHandle,
                          void          *pValues,
                          unsigned long  BytesToRead,
                          unsigned long *pBytesRead
                        ) {

        return(
                ReadFile(
                          Par4chHandle,    // Handle to device
                          pValues,         // Buffer to receive data
                          BytesToRead,     // Number of bytes to read
                          pBytesRead,      // Bytes read
                          NULL             // NULL = wait till I/O completes
                        )
                );               
}

SRLOCAL int OsDriverIoctl(
                           DEVHANDLE      Par4chHandle,
                           unsigned long  IoCtlCode,
                           void          *pValueIn,
                           unsigned long  InSize,
                           void          *pValueOut,
                           unsigned long  OutSize,
                           unsigned long *pBytesReturned
                          ) {

        return(
                DeviceIoControl(
                       Par4chHandle,     // Handle to device
                       IoCtlCode,        // IO Control code
                       pValueIn,         // Input data to driver
                       InSize,           // Length of in data in bytes
                       pValueOut,        // Output data from driver
                       OutSize,          // Length of out data in bytes
                       pBytesReturned,   // Bytes placed in output buffer
                       NULL              // NULL = wait till I/O completes
                       )
              );
               
}

SRLOCAL long OsGetLastError( void ) {

        return( GetLastError() );  // Provided by the OS
}

SRLOCAL int OsSetNonBlock( int value ) {

        // Winnt is always in non-blocking mode (value=1)
        //   value = 0 blocks reads until a character is ready
        //   value = 1 allow reads to return immediately
        //   returns -1 for failure, 0 for success

        if (value == 1)
                return( 0 );
        else
                return( -1 );
}

SRLOCAL int OsGetKey( int wait ) {

        int c;

        // Get a ready keypress or wait until one is ready if requested.

        c = EOF;
        if ( kbhit() || wait ) {
                c = getch();
                if (c == 0xD) printf("\n");  // add newline if char was ENTER
                if (c == 0x00 || c == 0xE0 ) // deal with extended key (fn,->)
                        c = getch();         // c has scan code
                }
        
        return( c );
}

SRLOCAL void OsFlushPrint( void ) {
        
        // WinNT automatically flushes printf.
}

SRLOCAL int OsSleep( int ms, int hardwait ) {

        // Sleep for specifed number of milliseconds.
        // Returns 0 for success, -1 for error.
        

        if ( ms <= 0 )
                return( 0 );
        
        _sleep( ms );
        return( 0 );
}

#define OsCommit _commit



#elif defined( SROS_WIN95 ) || defined( SROS_MSDOS )

SRLOCAL DEVHANDLE OsDriverOpen( char *PortName ) {
        return( Msdos_DrvOpen( PortName ) );
}

SRLOCAL int OsDriverClose( DEVHANDLE Par4chHandle ) {
        return( Msdos_DrvClose( Par4chHandle ) );
}

SRLOCAL int OsDriverRead(
                          DEVHANDLE      Par4chHandle,
                          void          *pValues,
                          unsigned long  BytesToRead,
                          unsigned long *pBytesRead
                         ) {
        return(
                Msdos_DrvRead(
                               Par4chHandle,    // Handle to device
                               pValues,         // Buffer to receive data
                               BytesToRead,     // Number of bytes to read
                               pBytesRead       // Bytes read
                             )
              );
}

SRLOCAL int OsDriverIoctl(
                           DEVHANDLE      Par4chHandle,
                           unsigned long  IoCtlCode,
                           void          *pValueIn,
                           unsigned long  InSize,
                           void          *pValueOut,
                           unsigned long  OutSize,
                           unsigned long *pBytesReturned
                          ) {
        return(
               Msdos_DrvIoctl(
                       Par4chHandle,     // Handle to device
                       IoCtlCode,        // IO Control code
                       pValueIn,         // Input data to driver
                       InSize,           // Length of in data in bytes
                       pValueOut,        // Output data from driver
                       OutSize,          // Length of out data in bytes
                       pBytesReturned    // Bytes placed in output buffer
                       )
               );
}

SRLOCAL long OsGetLastError( void ) {

        return( Msdos_DrvGetLastError() ); // Provided in par4chkd.c
}

SRLOCAL int OsSetNonBlock( int value ) {

        // Win95/MsDOS are always in non-blocking mode (value=1)
        //   value = 0 blocks reads until a character is ready
        //   value = 1 allow reads to return immediately
        //   returns -1 for failure, 0 for success

        if (value == 1)
                return( 0 );
        else
                return( -1 );
}

SRLOCAL int OsGetKey( int wait ) {

        int c;

        // Get a ready keypress or wait until one is ready if requested.

        c = EOF;
        if ( kbhit() || wait ) {
                c = getch();
                if (c == 0xD) printf("\n");  // add newline if char was ENTER
                if (c == 0x00 || c == 0xE0 ) // deal with extended key (fn,->)
                        c = getch();         // c has scan code
                }
        
        return( c );
}

SRLOCAL void OsFlushPrint( void ) {
        
        // Win95/MsDOS automatically flushes printf.
}

#if defined ( SROS_WIN95 )
SRLOCAL int OsSleep( int ms, int hardwait ) {

        // Sleep for specifed number of milliseconds.
        // Returns 0 for success, -1 for error.
        
        if ( ms <= 0 )
                return( 0 );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -