}
bool CSerialPort::OpenListenThread() {
** 检测线程是否已经开启了 * if (m_ false; }
s_bExit = false; ** 线程ID * UINT threadId;
** 开启串口数据监听线程 * m_ false; }
** 设置线程的优先级,高于普通线程 * }
** 退出临界区 *
LeaveCriticalSection(&m_csCommunicationSync); return true;
return false;
if (!SetThreadPriority(m_ false; } }
bool CSerialPort::CloseListenTread() {
if (m_ true; return true;
}
bool CSerialPort::OpenHeartBeatThread() {
** 检测线程是否已经开启了 * if (m_ false; }
s_bExit = false; ** 线程ID * UINT threadId;
** 开启串口数据监听线程 * m_ false; }
** 设置线程的优先级,高于普通线程 * if (!SetThreadPriority(m_ false; }
return true; }
bool CSerialPort::CloseHeartBeatThread() {
if (m_ true; }
UINT CSerialPort::GetBytesInCOM() {
DWORD dwError = 0;
** 错误码 *
COMSTAT comstat; ** COMSTAT结构体,记录通信设备的状态信息 *
}
UINT WINAPI CSerialPort::ListenThread( void* pParam ) {
** 得到本类的指针 *
CSerialPort *pSerialPort = reinterpret_cast
** 读取输入缓冲区中的数据并输出显示 * char cRecved = 0x00; accum = -1; char command;
memset(&comstat, 0, sizeof(COMSTAT)); UINT BytesInQue = 0;
** 在调用ReadFile和WriteFile之前,通过本函数清除以前遗留的错误标志 * if ( ClearCommError(m_ BytesInQue;
char length = 0;
char tagNum; char num; char accLen = 0; char commandLength = 0;
if(buffer != NULL){ delete []buffer; }
buffer = new char[1000]; 线程循环,轮询方式读取串口数据 while (!pSerialPort->s_bExit)
{
UINT BytesInQue = pSerialPort->GetBytesInCOM(); ** 如果串口输入缓冲区中无数据,则休息一会再查询 * ** 如果Inventory标志位为true,等一轮结束再接受* if ( BytesInQue == 0 && flag1 ==false) if ( BytesInQue == 0) {
Sleep(SLEEP_TIME_INTERVAL); continue;
}
do {
cRecved = 0x00;
if(pSerialPort->ReadChar(cRecved)) {
accum++;
buffer[accum] = cRecved;
}
}while(--BytesInQue); accLen+=(m+1);
cout<<\*即使break剩余的字节还会在串口中取得*
UINT BytesInQue1 = pSerialPort->GetBytesInCOM(); if(m == length -1 && invFlag ==false){
m = -1;