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

📄 comwin32.cpp

📁 一个很好用的comm类
💻 CPP
📖 第 1 页 / 共 4 页
字号:
      parity = MARKPARITY;
      break;
   case 'S':
   case 's':
      parity = SPACEPARITY;
      break;
   } /* end of switch */

   comDCB.Parity = parity;
   if (!SetCommState(comHandle, &comDCB))
   {
      comLastError  = GetLastError();
      comDCB.Parity = oldParity;

      return 0;
   } /* end of if */

   return parity;
} // ComWin32::Parity()

/************************************************************************
*  Function name   : ComWin32::Parity
*  Description     : Get parity.
*                  :
*  Parameters      : -
*  Returns         : Parity.
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  31Aug99  RCS      Created.
************************************************************************/
char ComWin32::Parity(void)
{
   char  parity = 'N';

   switch (comDCB.Parity)
   {
   case NOPARITY:
      parity = 'N';
      break;
   case EVENPARITY:
      parity = 'E';
      break;
   case ODDPARITY:
      parity = 'O';
      break;
   case MARKPARITY:
      parity = 'M';
      break;
   case SPACEPARITY:
      parity = 'S';
      break;
   } /* end of switch */

   return parity;
} // ComWin32::Parity()

/************************************************************************
*  Function name   : ComWin32::StopBits
*  Description     : Set up stop bits.
*                  :
*  Parameters      : stopBits - Stop bits to set to.
*  Returns         : Stop bits if successful.  0 if failed.
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  31Aug99  RCS      Created.
************************************************************************/
int ComWin32::StopBits(int stopBits)
{
   BYTE  oldStopBits = comDCB.StopBits;

   switch (stopBits)
   {
   case 1:
      stopBits = ONESTOPBIT;
      break;
   case 2:
      stopBits = TWOSTOPBITS;
      break;
   default:
      stopBits = (DataLength() == 5) ? ONE5STOPBITS : ONESTOPBIT;
      break;
   } /* end of switch */

   comDCB.StopBits = stopBits;
   if (!SetCommState(comHandle, &comDCB))
   {
      comLastError    = GetLastError();
      comDCB.StopBits = oldStopBits;

      return 0;
   } /* end of if */

   return stopBits;
} // ComWin32::StopBits()

/************************************************************************
*  Function name   : ComWin32::StopBits
*  Description     : Get stop bits.
*                  :
*  Parameters      : -
*  Returns         : Stop bits.
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  31Aug99  RCS      Created.
************************************************************************/
int ComWin32::StopBits(void)
{
   int   stopBits = 1;

   switch (comDCB.StopBits)
   {
   case ONESTOPBIT:
   case ONE5STOPBITS:   // 1.5 stop bits must be combined with 5 bits data
      stopBits = 1;
      break;
   case TWOSTOPBITS:
      stopBits = 2;
      break;
   } /* end of switch */

   return stopBits;
} // ComWin32::StopBits()

/************************************************************************
*  Function name   : ComWin32::LineSignal
*  Description     : Set/clear line signal.
*                  :
*  Parameters      : lineSignal  - Signal to change.
*                  : state       - State of signal to change to.
*                  :               Default to SIGNAL_QUERY.
*  Returns         : -
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  30Aug99  RCS      Created.
************************************************************************/
int ComWin32::LineSignal(RS232Signal lineSignal, int state)
{
   int      level = SIGNAL_QUERY;
   DWORD    modemStatus;

   switch (lineSignal)
   {
   case DTR:
      if (state != SIGNAL_QUERY)
      {
         comDCB.fDtrControl = (state == SIGNAL_ON) ? DTR_CONTROL_ENABLE :
                                                     DTR_CONTROL_DISABLE;
      } /* end of if */

      level = (comDCB.fDtrControl == DTR_CONTROL_ENABLE) ? SIGNAL_ON :
                                                           SIGNAL_OFF;
      break;
   case RTS:
      if (state != SIGNAL_QUERY)
      {
         comDCB.fRtsControl = (state == SIGNAL_ON) ? RTS_CONTROL_ENABLE :
                                                     RTS_CONTROL_DISABLE;
      } /* end of if */

      level = (comDCB.fDtrControl == DTR_CONTROL_ENABLE) ? SIGNAL_ON :
                                                           SIGNAL_OFF;
      break;
   case DSR:
   case CTS:
   case DCD:
   case RI:
      if (state != SIGNAL_QUERY)
         return level;

      if (GetCommModemStatus(comHandle, &modemStatus))
      {
         switch (lineSignal)
         {
         case DSR:
            level = (modemStatus & MS_DSR_ON)  ? SIGNAL_ON : SIGNAL_OFF;
            break;
         case CTS:
            level = (modemStatus & MS_CTS_ON)  ? SIGNAL_ON : SIGNAL_OFF;
            break;
         case DCD:
            level = (modemStatus & MS_RLSD_ON) ? SIGNAL_ON : SIGNAL_OFF;
            break;
         case RI:
            level = (modemStatus & MS_RING_ON) ? SIGNAL_ON : SIGNAL_OFF;
            break;
         } /* end of switch */
      } /* end of if */

      break;
   default:
      break;
   } /* end of switch */

   if (state != SIGNAL_QUERY && (lineSignal == DTR || lineSignal == RTS))
   {
      if (!SetCommState(comHandle, &comDCB))
         comLastError = GetLastError();
   } /* end of if */

   return level;
} // ComWin32::LineSignal()

/************************************************************************
*  Function name   : ComWin32::SendBreak
*  Description     : Send break signal.
*                  :
*  Parameters      : duration - Duration of break signal in msec.
*  Returns         : -
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  01Sep99  RCS      Created.
************************************************************************/
void ComWin32::SendBreak(DWORD duration)
{
   SetCommBreak(comHandle);
   Delay(duration);
   ClearCommBreak(comHandle);
} // ComWin32::SendBreak()

/************************************************************************
*  Function name   : ComWin32::GetEventMask
*  Description     : Get communication event mask.
*                  :
*  Parameters      : eventMask   -
*  Returns         : Error code.
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  31Aug99  RCS      Created.
************************************************************************/
DWORD ComWin32::GetEventMask(DWORD &eventMask)
{
   if (!GetCommMask(comHandle, &eventMask))
      comLastError = GetLastError();
   else
      comLastError = NO_ERROR;

   return comLastError;
} // ComWin32::GetEventMask()

/************************************************************************
*  Function name   : ComWin32::SetEventMask
*  Description     : Set up event mask.
*                  :
*  Parameters      : eventMask   - Event mask to set.  For the valid
*                  :               values refer to the valid value of the
*                  :               API function SetCommMask().
*  Returns         : Error code.
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  31Aug99  RCS      Created.
************************************************************************/
DWORD ComWin32::SetEventMask(DWORD eventMask)
{
   if (!SetCommMask(comHandle, eventMask))
   {
      comLastError = GetLastError();
      return comLastError;
   } /* end of if */

#ifndef NO_OVERLAPPED_SUPPORT
   DWORD    evWait = eventMask;
   DWORD    count  = 0;

   comEventMask = 0;
   WaitCommEvent(comHandle, &comEventMask, &ovComEvent);

   /*
       We expect ERROR_IO_PENDING returned from WaitCommEvent
       because we are waiting with an overlapped structure.
   */
   if ((comLastError = GetLastError()) == ERROR_IO_PENDING)
   {
      // Perform overlapped operation
      if (GetOverlappedResult(comHandle, &ovComEvent, &count, FALSE))
         ovComEvent.Offset += count;

      comLastError = NO_ERROR;
   } /* end of if */
#endif // !NO_OVERLAPPED_SUPPORT

   return comLastError;
} // ComWin32::SetEventMask()

/************************************************************************
*  Function name   : ComWin32::SetEventCharacter
*  Description     : Set up event character.
*                  :
*  Parameters      : evChar   - Event character.
*  Returns         : -
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  31Aug99  RCS      Created.
************************************************************************/
void ComWin32::SetEventCharacter(char evChar)
{
   GetCommState(comHandle, &comDCB);
   comDCB.EvtChar = evChar;
   SetCommState(comHandle, &comDCB);
} // ComWin32::SetEventCharacter()

/************************************************************************
*  Function name   : ComWin32::WaitForEvent
*  Description     : Wait for the arrival of COM port event(s).
*                  :
*  Parameters      : evWait   - Events to wait.
*                  : timeOut  - Wait time out.  Default to INFINITE.
*  Returns         : TRUE     - Event(s) arrived.
*                  : FALSE    - No event.
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  31Aug99  RCS      Created.
************************************************************************/
BOOL ComWin32::WaitForEvent(DWORD &eventWait, DWORD timeOut)
{
#ifndef NO_OVERLAPPED_SUPPORT
   DWORD    waitEv;

   waitEv = WaitForSingleObject(ovComEvent.hEvent, timeOut);
   if (waitEv == WAIT_OBJECT_0)
   {
      eventWait = comEventMask;     // Event received
      return TRUE;
   } /* end of if */
#else
   timeOut = timeOut;               // Get rid of the compiler warning
   if (WaitCommEvent(comHandle, &eventWait, 0))
      return TRUE;
#endif // !NO_OVERLAPPED_SUPPORT

   return FALSE;
} // ComWin32::WaitForEvent()

/************************************************************************
*  Function name   : ComWin32::GetBuffer
*  Description     : Read COM port data.
*                  :
*  Parameters      : buffer   - Buffer to store data.
*                  : count    - Number of bytes to read.
*  Returns         : Number of bytes actually read.
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  31Aug99  RCS      Created.
************************************************************************/
int ComWin32::GetBuffer(char *buffer, int count)
{
   DWORD    error;
   DWORD    toRead;
   DWORD    bytesRead = 0;
   int      avail;

   if ((avail = AvailableRxData()) > 0)
   {
      if (avail < count)
         toRead = avail;
      else
         toRead = count;

#ifndef NO_OVERLAPPED_SUPPORT
      if (ReadFile(comHandle, buffer, toRead, &bytesRead, &ovReadWrite) == 0)
#else
      if (ReadFile(comHandle, buffer, toRead, &bytesRead, 0) == 0)
#endif // !NO_OVERLAPPED_SUPPORT
      {
         if ((error = GetLastError()) != ERROR_IO_PENDING)
         {
            COMSTAT  queue;

            ClearCommError(comHandle, &error, &queue);
         } /* end of if */
      } /* end of if */

#ifndef NO_OVERLAPPED_SUPPORT
      if (bytesRead < toRead)
         GetOverlappedResult(comHandle, &ovReadWrite, &bytesRead, TRUE);
#endif // !NO_OVERLAPPED_SUPPORT
   } /* end of if */

   return (int )bytesRead;

⌨️ 快捷键说明

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