从Serial Com Port读取所有数据

时间:2017-04-27 07:34:24

标签: c++ serial-port

简介:

我有一台设备连接到我的电脑到串口COM3。借助“串行端口监视器”的试用版,我能够读出设备发出的数据。输出看起来像这样:(我把它减少到所需的内容)

对不起html表,我想这不是表格

<table border=1px>
  <tbody>
    <tr>
      <th>#</th>
      <th>TIMESTAMP</th>
      <th>Function</th>
      <th>Direction</th>
      <th>Status</th>
      <th>Data</th>
      <th>Data char</th>
      <th>Data length</th>
      <th>Port</th>
    </tr>
    <tr>
      <td>7765</td>
      <td>07:59:11:235</td>
      <td>IRP_MJ_READ</td>
      <td>UP</td>
      <td>STATUS_SUCCESS</td>
      <td>f0 </td>
      <td>≡</td>
      <td>1</td>
      <td>COM3</td>
    </tr>
    <tr>
      <td>7795</td>
      <td>07:59:11:417</td>
      <td>IRP_MJ_READ</td>
      <td>UP</td>
      <td>STATUS_SUCCESS</td>
      <td>fb </td>
      <td>√</td>
      <td>1</td>
      <td>COM3</td>
    </tr>
    <tr>
      <td>9405</td>
      <td>07:59:21:836</td>
      <td>IRP_MJ_READ</td>
      <td>UP</td>
      <td>STATUS_SUCCESS</td>
      <td>f6 ff f6 ff f7 </td>
      <td>÷&nbsp;÷&nbsp;≈</td>
      <td>5</td>
      <td>COM3</td>
    </tr>
    <tr>
      <td>11045</td>
      <td>07:59:35:641</td>
      <td>IRP_MJ_READ</td>
      <td>UP</td>
      <td>STATUS_SUCCESS</td>
      <td>f6 ff f6 </td>
      <td>÷&nbsp;÷</td>
      <td>3</td>
      <td>COM3</td>
    </tr>
    <tr>
      <td>13815</td>
      <td>07:59:57:720</td>
      <td>IRP_MJ_READ</td>
      <td>UP</td>
      <td>STATUS_SUCCESS</td>
      <td>f6 ff f7 </td>
      <td>÷&nbsp;≈</td>
      <td>3</td>
      <td>COM3</td>
    </tr>
    <tr>
      <td>14005</td>
      <td>07:59:59:160</td>
      <td>IRP_MJ_READ</td>
      <td>UP</td>
      <td>STATUS_SUCCESS</td>
      <td>f2 fe f3 ff f3 </td>
      <td>≥■≤&nbsp;≤</td>
      <td>5</td>
      <td>COM3</td>
    </tr>
    <tr>
      <td>15885</td>
      <td>08:00:15:897</td>
      <td>IRP_MJ_READ</td>
      <td>UP</td>
      <td>STATUS_SUCCESS</td>
      <td>fd f0 </td>
      <td>²≡</td>
      <td>2</td>
      <td>COM3</td>
    </tr>
  </tbody>
</table>

示例代码:

要在c ++程序中使用此数据,我发现了以下功能:

int ReadByte(LPCWSTR PortSpecifier) {
DCB       dcb;
int       retVal;
BYTE      Byte = 0;
DWORD     dwBytesTransferred;
DWORD     dwCommModemStatus;

HANDLE hPort = CreateFile(
    PortSpecifier,
    GENERIC_READ,
    0,
    NULL,
    OPEN_EXISTING,
    0,
    NULL
    );
if (!GetCommState(hPort, &dcb)) return 0; //ERROR

dcb.BaudRate = CBR_9600;          //9600 Baud 
dcb.ByteSize = 8;                 //8 data bits 
dcb.Parity = NOPARITY;            //no parity 
dcb.StopBits = ONESTOPBIT;        //1 stop

if (!SetCommState(hPort, &dcb)) return 0; //ERROR

//SetCommMask(hPort, EV_RXCHAR | EV_ERR);       //receive character event  
SetCommMask(hPort, EV_RXCHAR | EV_TXEMPTY);       //receive character event  

WaitCommEvent(hPort, &dwCommModemStatus, 0);  //wait for character 

if (dwCommModemStatus & EV_RXCHAR)
    ReadFile(hPort, &Byte, 1, &dwBytesTransferred, 0);  //read 1 
else if (dwCommModemStatus & EV_ERR)
    retVal = 0x101;
retVal = Byte;
CloseHandle(hPort);
cout << retVal << endl;
return       retVal;}

问题:

只要设备中的数据输出中只有一个十六进制,这样就可以正常工作。但正如你所看到的,所有有趣的东西都发生在一个十六进制以上。

如何从输出中获取所有数字,可能是一组整数?

这是我第一次使用Ports,所以它可能非常简单,我还没有看到它。

0 个答案:

没有答案