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

📄 comwin32.cpp

📁 一个很好用的comm类
💻 CPP
📖 第 1 页 / 共 4 页
字号:
} // ComWin32::GetBuffer()

/************************************************************************
*  Function name   : ComWin32::GetChar
*  Description     : Get one character from COM port.
*                  :
*  Parameters      : byte  - Buffer.
*  Returns         : Number of bytes read.
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  31Aug99  RCS      Created.
************************************************************************/
int ComWin32::GetChar(char *byte)
{
   return GetBuffer(byte, 1);
} // ComWin32::GetChar()

/************************************************************************
*  Function name   : ComWin32::AvailableRxData
*  Description     : Check number of bytes available for reading.
*                  :
*  Parameters      : -
*  Returns         : Availble bytes.
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  03Sep99  RCS      Created.
************************************************************************/
int ComWin32::AvailableRxData(void)
{
   COMSTAT  queue;
   DWORD    error;

   ClearCommError(comHandle, &error, &queue);
   return queue.cbInQue;
} // ComWin32::AvailableRxData()

/************************************************************************
*  Function name   : ComWin32::PutBuffer
*  Description     : Write data to COM port.
*                  :
*  Parameters      : buffer   - Data to be output.
*                  : count    - Number of bytes to write.
*  Returns         : Number of bytes written.
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  31Aug99  RCS      Created.
************************************************************************/
int ComWin32::PutBuffer(char *buffer, int count)
{
   COMSTAT     queue;
   DWORD       error;
   DWORD       written = 0;
   int         bytesWrite;

   ClearCommError(comHandle, &error, &queue);
   bytesWrite = txQueue - queue.cbOutQue;
   if (count < bytesWrite)
      bytesWrite = count;

#ifndef NO_OVERLAPPED_SUPPORT
   if (WriteFile(comHandle, buffer, bytesWrite, &written, &ovReadWrite) == 0)
#else
   if (WriteFile(comHandle, buffer, bytesWrite, &written, 0) == 0)
#endif // !NO_OVERLAPPED_SUPPORT
   {
      if ((error = GetLastError()) == ERROR_IO_PENDING)
         ClearCommError(comHandle, &error, &queue);
      else
         written = bytesWrite;
   } /* end of if */

   return written;
} // ComWin32::PutBuffer()

/************************************************************************
*  Function name   : ComWin32::PutChar
*  Description     : Output one byte to COM port.
*                  :
*  Parameters      : byte  - Character to output.
*  Returns         : Number of bytes send out.
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  31Aug99  RCS      Created.
************************************************************************/
int ComWin32::PutChar(char byte)
{
   char  sendData = byte;

   return PutBuffer(&sendData, 1);
} // ComWin32::PutChar()

/************************************************************************
*  Function name   : ComWin32::PutString
*  Description     : Send a string of data.
*                  :
*  Parameters      : option   - String end marker.  Default to -2.
*                  :            if -3, CR/LF pair is appended.
*                  :            if -2, LF is appended.
*                  :            if -1, nothing is appended.
*                  :            0x00 to 0xff, the value will be appended.
*  Returns         : Number of bytes sent.
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  02Sep99  RCS      Created.
************************************************************************/
int ComWin32::PutString(char *buffer, int option)
{
   int   bytesSent;
   char  append[3];

   memset(append, 0, sizeof(append));
   switch (option)
   {
   case -3:                // Attach CR-LF
      append[1] = '\n';
   case -2:                // Attach LF only
      append[0] = '\r';
      break;
   case -1:                // Attach noting
      break;
   default:                // Attach the specified character
      append[0] = (char )option;
      break;
   } /* end of switch */

   bytesSent = PutBuffer(buffer, strlen(buffer));
   if (option != -1)
   {
      if (append[0] == 0x00)
      bytesSent += PutChar(append[0]);       // NULL character
   else
      bytesSent += PutBuffer(append, strlen(append));
   } /* end of if */

   return bytesSent;
} // ComWin32::PutString()

/************************************************************************
*  Function name   : ComWin32::ImmediateSend
*  Description     : Send the character immediately without buffering the
*                  : byte.
*  Parameters      : byte  - Character to send.
*  Returns         : Number of bytes sent.
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  02Sep99  RCS      Created.
************************************************************************/
int ComWin32::ImmediateSend(char byte)
{
   return (TransmitCommChar(comHandle, byte)) ? 1 : 0;
} // ComWin32::ImmediateSend()

/************************************************************************
*  Function name   : ComWin32::Flush
*  Description     : Flush buffer(s).
*                  :
*  Parameters      : which - Buffer to flush.  Default to FLUSH_ALL.
*  Returns         : -
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  31Aug99  RCS      Created.
*  01May03  RCS      Modified.  Fixed bug of using PurgeComm() incorrectly.
************************************************************************/
void ComWin32::Flush(FlushType which)
{
   COMSTAT     queue;
   DWORD       flags = 0;

   switch (which)
   {
   case FLUSH_RX:             // Flush Rx queue
      flags = PURGE_RXCLEAR;
      break;
   case FLUSH_TX:
      flags = PURGE_TXCLEAR;  // Flush Tx queue
      break;
   case FLUSH_ALL:            // Flush both Rx and Tx queue
      flags = PURGE_RXCLEAR | PURGE_TXCLEAR;
      break;
   case FLUSH_COMERROR:       // Clear COM error only
      flags = 0;
      break;
   } /* end of switch */

   if (flags != 0)
      PurgeComm(comHandle, flags);

   ClearCommError(comHandle, &flags, &queue);
} // ComWin32::Flush()

/************************************************************************
*  Function name   : ComWin32::SetHandshake
*  Description     : Set up handshaking.
*                  :
*  Parameters      : inMode   - The new incoming handshake.
*                  : outMode  - The new outgoing handshake.
*  Returns         : TRUE     - Successful.
*                  : FALSE    - Error.
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  01Sep99  RCS      Created.
************************************************************************/
BOOL ComWin32::SetHandshake(HandshakeType inMode, HandshakeType outMode)
{
   // Clear all handshaking lines.
   comDCB.fInX  = 0;
   comDCB.fOutX = 0;

   if (comDCB.fRtsControl == RTS_CONTROL_HANDSHAKE)
      comDCB.fRtsControl = RTS_CONTROL_ENABLE;

   if (comDCB.fDtrControl == DTR_CONTROL_HANDSHAKE)
      comDCB.fDtrControl = DTR_CONTROL_ENABLE;

   comDCB.fOutxCtsFlow = 0;
   comDCB.fOutxDsrFlow = 0;

   // Set the incomming handshake method.
   switch (inMode)
   {
   case HS_NONE:
         break;
   case HS_DTR:
      comDCB.fDtrControl = DTR_CONTROL_HANDSHAKE;
      break;
   case HS_RTS:
      comDCB.fRtsControl = RTS_CONTROL_HANDSHAKE;
      break;
   case HS_XONXOFF:
      comDCB.fInX = 1;
      break;
   case HS_DSR:
   default:
      return FALSE;
   } /* end of switch */

   // Set the outgoing handshake method.
   switch (outMode)
   {
   case HS_NONE:
      break;
   case HS_DSR:
      comDCB.fOutxDsrFlow = 1;
      break;
   case HS_CTS:
      comDCB.fOutxCtsFlow = 1;
      break;
   case HS_XONXOFF:
      comDCB.fOutX = 1;
      break;
   default:
      return FALSE;
   } /* end of switch */

   // Set the handshake method.
   if (!SetCommState(comHandle, &comDCB))
   {
      comLastError = GetLastError();
      return FALSE;
   } /* end of if */

   inHandshake  = inMode;
   outHandshake = outMode;

   return TRUE;
} // ComWin32::SetHandshake()

/************************************************************************
*  Function name   : ComWin32::GetHandshake
*  Description     : Get the current handshaking mode.
*                  :
*  Parameters      : inMode   - Current incoming handshake mode.
*                  : outMode  - Current outgoing handshake mode.
*  Returns         : -
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  01Sep99  RCS      Created.
************************************************************************/
void ComWin32::GetHandshake(HandshakeType &inMode, HandshakeType &outMode)
{
   inMode  = inHandshake;
   outMode = outHandshake;
} // ComWin32::GetHandshake()

/************************************************************************
*  Function name   : ComWin32::GetBufferInfo
*  Description     : Get buffer information.
*                  :
*  Parameters      : info  - Buffer information structure.
*  Returns         : Error code.
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  02Sep99  RCS      Created.
************************************************************************/
void ComWin32::GetBufferInfo(ComBufferInfo &info)
{
   COMSTAT  que;
   DWORD    error;

   /*
      Get the port info, since we also get comm error information
      store it away also.
   */
   ClearCommError(comHandle, &error, &que );

   // Store the information for the receive buffer.
   info.rxCount = que.cbInQue;
   info.rxSize  = rxQueue;

   // Store the information for the transmit buffer.
   info.txCount = que.cbOutQue;
   info.txSize = txQueue;

   // Store info on port blocking.
   info.txPaused = (que.fCtsHold || que.fDsrHold || que.fXoffHold) ? TRUE :
                                                                     FALSE;
   info.rxPaused = (que.fXoffSent) ? TRUE : FALSE;
} // ComWin32::GetBufferInfo()

/************************************************************************
*  Function name   : ComWin32::Delay
*  Description     : Pause for a while.
*                  :
*  Parameters      : period   - Pause period in msec.
*  Returns         : -
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  02Sep99  RCS      Created.
************************************************************************/
void ComWin32::Delay(DWORD period)
{
   WaitForSingleObject(delayHandle, period);
} // ComWin32::Delay()

/************************************************************************
*  Function name   : ComWin32::StoreRxData
*  Description     : Read data from COM port and store into the internal
*                  : circular buffer.
*  Parameters      : -
*  Returns         : Number of bytes read from the COM port.
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  03Sep99  RCS      Created.
************************************************************************/
int ComWin32::StoreRxData(void)
{
   int   size = circular->GetBufferSize();
   char  *tmp = new char [size];
   int   bytesRead;

   if ((bytesRead = GetBuffer(tmp, size)) > 0)
      circular->PushIt(tmp, bytesRead);

   delete [] tmp;
   return bytesRead;
} // ComWin32::StoreRxData()

/************************************************************************
*  Function name   : ComWin32::StoreData
*  Description     : Store data into the circular buffer
*                  :
*  Parameters      : data  - Data to store.
*                  : size  - Number of characters to store.
*  Returns         : -
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  05Jul01  RCS      Created.
************************************************************************/
void ComWin32::StoreData(char *data, int size)
{
   circular->PushIt(data, size);
} // ComWin32::StoreData()

⌨️ 快捷键说明

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