从头开始读取串行数据

时间:2018-08-01 09:33:58

标签: c++ visual-c++

我正在读取连续的串行数据,该数据以29字节数组的形式发送   第一个字节是标头,是oxD1的值。

如何从页眉开始选择缓冲区中的数据。

这是我的代码。

 int SerialPort::readSerialPortFreedPacket(BYTE *buffer, unsigned int 
 buf_size)
{
DWORD bytesRead;
int timeout = 500;
OVERLAPPED ovread = { 0 };
ovread.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);



if (ovread.hEvent == NULL) {
    // Error creating overlapped event; abort.
    return FALSE;
}



if (!ReadFile( this->handler , (void*)buffer, 29, &bytesRead, &ovread)) {
    if (GetLastError() != ERROR_IO_PENDING) {
        // Handle error in communications
        std::cout << "Error reading data " << std::endl;
    }
    else {
        DWORD ret = WaitForSingleObject(ovread.hEvent, timeout);

        switch (ret) {
        case WAIT_OBJECT_0:
        //  HandleASuccessfulRead(buf, bytesRead);
            std::cout << buffer << std::endl;
            break;
        case WAIT_TIMEOUT:
            // Handle timeout

            break;
        case WAIT_FAILED:
            // Handle failure

            break;
        default:
            // what else to handle?
            break;
        }
    }
}
else {
    // read completed immediately
    //HandleASuccessfulRead(buf,bytesRead);

}
return 0;

}

0 个答案:

没有答案