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

📄 wincelnk.c

📁 DALLAS 1 Wire 总线 SDK 支持多种高级语言
💻 C
📖 第 1 页 / 共 2 页
字号:
// stdafx.cpp : source file that includes just the standard includes
//	first.pch will be the pre-compiled header
//	stdafx.obj will contain the pre-compiled type information

#include "ownet.h"
#include "WinCElnk.h"
#include "ibhs.h"

// local functions
int OpenCOM(int, char *);      //IBAPI
int OpenCOMEx(char *);
int SetupCOM(short, ulong);    //IBAPI
void FlushCOM(int);
void CloseCOM(int);            //IBAPI
//IBAPI WriteCOM(int, int, uchar IBFAR *);
int WriteCOM(int, int, uchar *);
//IBAPI ReadCOM(int, int, uchar IBFAR *);
int ReadCOM(int,int,uchar *);
void BreakCOM(int);            //IBAPI
int PowerCOM(short, short);   //IBAPI
int RTSCOM(short, short);     //IBAPI
int FreeCOM(short);           //IBAPI
void StdFunc MarkTime(void);
int ElapsedTime(long);        //IBAPI
int DSRHigh(short);           //IBAPI
void msDelay(int len);
void SetBaudCOM(int, uchar);


static DWORD TimeStamp;
int          globalPortnumCounter = 0;

//---------------------------------------------------------------------------
// Utility Functions
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Attempt to open a com port.  Keep the handle in ComID.
// Set the starting baud rate to 9600.
//
// 'portnum'   - number 0 to MAX_PORTNUM-1.  This number provided will
//               be used to indicate the port number desired when calling
//               all other functions in this library.
//
//
// Returns: the port number if it was succesful otherwise -1
//
int OpenCOMEx(char *port_zstr)
{
   int portnum = globalPortnumCounter++;

   // check to see if exceeded number of ports
   if (globalPortnumCounter >= MAX_PORTNUM)
      globalPortnumCounter = 0;

   if(!OpenCOM(portnum, port_zstr))
   {
      return -1;
   }

   return portnum;
}

//---------------------------------------------------------------------------
//  Description:
//  Attept to open a com port.  Keep the handle in ComID.
//
int OpenCOM(int Prt, char *port_zstr)
{
   TCHAR szPort[15];
   short fRetVal;
   COMMTIMEOUTS CommTimeOuts;

   // Only open if not already open
   if (ComID[Prt] == 0)
   {
      // load the COM prefix string and append port number
      wsprintf(szPort, TEXT("COM%d:"), Prt);

      // open COMM device

      if ((ComID[Prt] =
               CreateFile( szPort, GENERIC_READ | GENERIC_WRITE,
                                      0,        // exclusive access
                                      NULL,     // no security attrs
                                      OPEN_EXISTING,
                                      0,
                                      NULL )) == (HANDLE) -1 )
      {
         ComID[Prt] = 0;
         return (FALSE) ;
      }
      else
      {
          // get any early notifications
          SetCommMask(ComID[Prt], EV_RXCHAR | EV_TXEMPTY | EV_ERR | EV_BREAK);

          // setup device buffers
          SetupComm(ComID[Prt], 1030, 1030);

          // purge any information in the buffer
          PurgeComm(ComID[Prt], PURGE_TXABORT | PURGE_RXABORT |
                                           PURGE_TXCLEAR | PURGE_RXCLEAR );

          // set up for overlapped non-blocking I/O (3.00Beta4)
          CommTimeOuts.ReadIntervalTimeout = DEF_READ_INT;
          CommTimeOuts.ReadTotalTimeoutMultiplier = DEF_READ_MULT;
          CommTimeOuts.ReadTotalTimeoutConstant = DEF_READ_CONST;
          CommTimeOuts.WriteTotalTimeoutMultiplier = DEF_WRITE_MULT;
          CommTimeOuts.WriteTotalTimeoutConstant = DEF_WRITE_CONST;
          SetCommTimeouts(ComID[Prt], &CommTimeOuts);
      }
   }

   // get the connection
   fRetVal = SetupCOM(Prt,brate);

   if (!fRetVal)
   {
      CloseHandle(ComID[Prt]);
      ComID[Prt] = 0;
   }

   return (fRetVal);
}


//---------------------------------------------------------------------------
//  Description:
//     This routines sets up the DCB and performs a SetCommState().
//
int SetupCOM(short Prt, ulong baudrate)
{
   short fRetVal;
   DCB dcb;

   dcb.DCBlength = sizeof(DCB);

   GetCommState(ComID[Prt], &dcb);

   dcb.BaudRate = baudrate;               // current baud rate
   dcb.fBinary = TRUE;                    // binary mode, no EOF check
   dcb.fParity = FALSE;                   // enable parity checking
   dcb.fOutxCtsFlow = FALSE;              // CTS output flow control
   dcb.fOutxDsrFlow = FALSE;              // DSR output flow control
   //???dcb.fDtrControl = DTR_CONTROL_ENABLE;  // DTR flow control type
   dcb.fDsrSensitivity = FALSE;           // DSR sensitivity
   dcb.fTXContinueOnXoff = TRUE;          // XOFF continues Tx
   dcb.fOutX = FALSE;                     // XON/XOFF out flow control
   dcb.fInX = FALSE;                      // XON/XOFF in flow control
   dcb.fErrorChar = FALSE;                // enable error replacement
   dcb.fNull = FALSE;                     // enable null stripping
   //???dcb.fRtsControl = RTS_CONTROL_ENABLE;  // RTS flow control
   dcb.fAbortOnError = FALSE;             // abort reads/writes on error
   dcb.XonLim = 0;                        // transmit XON threshold
   dcb.XoffLim = 0;                       // transmit XOFF threshold
   dcb.ByteSize = 8;                      // number of bits/byte, 4-8
   dcb.Parity = NOPARITY;                 // 0-4=no,odd,even,mark,space
   dcb.StopBits = ONESTOPBIT;             // 0,1,2 = 1, 1.5, 2
   dcb.XonChar = 0;                       // Tx and Rx XON character
   dcb.XoffChar = 1;                      // Tx and Rx XOFF character
   dcb.ErrorChar = 0;                     // error replacement character
   dcb.EofChar = 0;                       // end of input character
   dcb.EvtChar = 0;                       // received event character

   fRetVal = SetCommState(ComID[Prt], &dcb);

   return (fRetVal);
}


//---------------------------------------------------------------------------
//  Description:
//     Closes the connection to the port.
//     - Use new PurgeComm() API to clear communications driver before
//       closing device.
//
void CloseCOM(int Prt)
{
   // disable event notification and wait for thread
   // to halt
   SetCommMask(ComID[Prt], 0);

   // purge any outstanding reads/writes and close device handle
   PurgeComm(ComID[Prt], PURGE_TXABORT | PURGE_RXABORT |
                         PURGE_TXCLEAR | PURGE_RXCLEAR );
   CloseHandle(ComID[Prt]);

   ComID[Prt] = 0;

}

//---------------------------------------------------------------------------
//  Description:
//     Closes the connection to the port
//     - Use new PurgeComm() API to clear communications driver before
//       closing device.
//
int FreeCOM(short Prt)
{
   CloseCOM(Prt);

   return TRUE;
}

//---------------------------------------------------------------------------
//  Description:
//     flush the rx and tx buffers
//
void FlushCOM(int Prt)
{
   // purge any information in the buffer
   PurgeComm(ComID[Prt], PURGE_TXABORT | PURGE_RXABORT |
                         PURGE_TXCLEAR | PURGE_RXCLEAR );
}


//---------------------------------------------------------------------------
// Write an array of bytes to the COM port, verify that it was
// sent out.  Assume that baud rate has been set and the buffers have
// been flushed.
//
// Returns 1 for success and 0 for failure
//
//IBAPI WriteCOM(int Prt, int outlen, uchar IBAPI *outbuf)
int WriteCOM(int Prt, int outlen, uchar *outbuf)
{
   BOOL fWriteStat;
   DWORD dwBytesWritten=0;
   DWORD ler=0,to;
   COMMTIMEOUTS CommTimeOuts;

   // check if not using default timeout
   if (timeout > 0)
   {
      CommTimeOuts.ReadIntervalTimeout = DEF_READ_INT;
      CommTimeOuts.ReadTotalTimeoutMultiplier = DEF_READ_MULT;
      CommTimeOuts.ReadTotalTimeoutConstant = DEF_READ_CONST;
      CommTimeOuts.WriteTotalTimeoutMultiplier = 1;
      CommTimeOuts.WriteTotalTimeoutConstant = timeout;
      SetCommTimeouts(ComID[Prt], &CommTimeOuts);
      to = timeout + outlen + 5;
   }
   // calculate a timeout
   else
      to = DEF_WRITE_MULT * outlen + DEF_WRITE_CONST + 5;

   // write the byte
   fWriteStat = WriteFile(ComID[Prt], (LPSTR) outbuf, outlen,
                          &dwBytesWritten, NULL );

   // check for an error
   if (!fWriteStat)
      ler = GetLastError();

   // check if need to restore timeouts
   if (timeout > 0)
   {
      CommTimeOuts.ReadIntervalTimeout = DEF_READ_INT;
      CommTimeOuts.ReadTotalTimeoutMultiplier = DEF_READ_MULT;
      CommTimeOuts.ReadTotalTimeoutConstant = DEF_READ_CONST;
      CommTimeOuts.WriteTotalTimeoutMultiplier = DEF_WRITE_MULT;
      CommTimeOuts.WriteTotalTimeoutConstant = DEF_WRITE_CONST;
      SetCommTimeouts(ComID[Prt], &CommTimeOuts);
   }

   // check results of write
   if (!fWriteStat || (dwBytesWritten != (unsigned short)outlen))
      return 0;
   else
      return 1;
}


//---------------------------------------------------------------------------
// Read an array of bytes to the COM port, verify that it was
// sent out.  Assume that baud rate has been set and the buffers have
// been flushed.
//
// Returns number of characters read
//
int ReadCOM(int Prt, int inlen, uchar *inbuf)   //,ulong IBAPI *more
{
   DWORD dwLength=0, dwErrorFlags, temp_len=0;
   BOOL fReadStat;
   DWORD ler=0,to;
   COMMTIMEOUTS CommTimeOuts;
   COMSTAT ComStat;

   // check if not using default timeout
   if (timeout > 0)
   {
      CommTimeOuts.ReadIntervalTimeout = DEF_READ_INT;
      CommTimeOuts.ReadTotalTimeoutMultiplier = 1;
      CommTimeOuts.ReadTotalTimeoutConstant = timeout;
      CommTimeOuts.WriteTotalTimeoutMultiplier = DEF_WRITE_MULT;
      CommTimeOuts.WriteTotalTimeoutConstant = DEF_WRITE_CONST;
      SetCommTimeouts(ComID[Prt], &CommTimeOuts);
      to = timeout + inlen + 5;
   }
   // calculate a timeout
   else
      to = DEF_READ_MULT * inlen + DEF_READ_CONST + 5;

⌨️ 快捷键说明

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