阅读:1545回复:3
求教关于串口读数据的问题
DWORD CCESeries::ReadThreadFunc(LPVOID lparam)
{ CCESeries *ceSeries = (CCESeries*)lparam; DWORD evtMask; BYTE * readBuf = NULL; DWORD actualReadLen=0; DWORD willReadLen; DWORD dwReadErrors; COMSTAT cmState; CString strInChar; DWORD dwMinSize; ASSERT(ceSeries->m_hComm !=INVALID_HANDLE_VALUE); PurgeComm(ceSeries->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR ); SetCommMask (ceSeries->m_hComm, EV_RXCHAR | EV_CTS | EV_DSR ); while (TRUE) { if (WaitCommEvent(ceSeries->m_hComm,&evtMask,0)) { SetCommMask (ceSeries->m_hComm, EV_RXCHAR | EV_CTS | EV_DSR ); if (evtMask & EV_RXCHAR) { ClearCommError(ceSeries->m_hComm,&dwReadErrors,&cmState); willReadLen = cmState.cbInQue ; if (willReadLen <= 0) { continue; } readBuf = new BYTE[willReadLen]; ReadFile(ceSeries->m_hComm, readBuf, willReadLen, &actualReadLen,0); if (actualReadLen>0) { AfxMessageBox((CString)readBuf); ceSeries->m_OnSeriesRead(ceSeries->m_pPortOwner,readBuf,actualReadLen); } delete readBuf; } } if (WaitForSingleObject(ceSeries->m_hReadCloseEvent,500) == WAIT_OBJECT_0) { break; } } return 0; } 串口接收到的数据应该是:00030202BC3D 但是打印出来的数据是:口0$口06口38L 不知道是哪里错了?请大虾帮忙看看? 后来怀疑是编码问题,又把evc里的unicode去掉了,还加了如下的语句 dwMinSize = MultiByteToWideChar(CP_ACP, MB_COMPOSITE, (char*)readBuf, -1,0, 0 ); strInChar.GetBuffer(dwMinSize); MultiByteToWideChar(CP_ACP, MB_COMPOSITE, (char*)readBuf, -1, strInChar.GetBuffer(dwMinSize),dwMinSize); 但结果还是一样的.和前面不加转换代码的结果一样 |
|
沙发#
发布于:2007-04-16 08:21
以Hex打印
|
|
板凳#
发布于:2007-04-16 09:12
hex怎么打印?给个例子
|
|
地板#
发布于:2007-04-26 13:39
可能是串口时钟不正确
|
|