itas109 / CSerialPort

CSerialPort - lightweight cross-platform serial port library for C++/C/C#/Java/Python/Node.js/Electron
https://blog.csdn.net/itas109/article/details/84282860
Other
740 stars 338 forks source link

无法读取串口数据 #71

Closed UnknownObject000 closed 1 year ago

UnknownObject000 commented 1 year ago

描述bug 根据示例编写测试程序,可以发送数据但无法从串口读取数据

重现步骤 运行以下代码

#include <iostream>
#include <CSerialPort/SerialPort.h>
#include <CSerialPort/SerialPortInfo.h>

itas109::CSerialPort serial;

class SerialEvent : public itas109::CSerialPortListener
{
public:
    void onReadEvent(const char* portName, unsigned int readBufferLen)
    {
        printf("Port %s, Data Size: %u\n", portName, readBufferLen);
        if (readBufferLen <= 0)
            return;
        char* str = new char[readBufferLen + 1];
        int readed_siz = serial.readData(str, readBufferLen);
        if (readed_siz <= 0)
        {
            delete[] str;
            return;
        }
        printf("%s\0", str);
        delete[] str;
    }
};

int main()
{
    SerialEvent s_event;
    char cmds[8] = { 0x01,0x04,0x00,0x00,0x00,0x0A,0x70,0x0D };
    serial.init("COM3");
    serial.setReadIntervalTimeout(500);
    serial.setMinByteReadNotify(1);
    if (!serial.open())
    {
        printf("Can't Open Serial\n");
        exit(-2);
    }
    else
        printf("Serial Open Finished.\n");
    if (serial.connectReadEvent(&s_event) != 0)
    {
        printf("Can't Connect ReadEvent\n");
        exit(-1);
    }
    else
        printf("Serial ReadEvent Connected.\n");
    while (true)
    {
        printf("Command Writed: %d/8 (%zu)\n", serial.writeData(cmds, 8), time(0));
        Sleep(2000);
    }
    serial.close();
    return 0;
}

预期行为 串口数据正常被读取

截图 以下是控制台输出结果

PS D:\FTP-ROOT\SerialDebug> .\SerialDebug.exe
Serial Open Finished.
Serial ReadEvent Connected.
Command Writed: 8/8 (1676990424)
Command Writed: 8/8 (1676990426)
Command Writed: 8/8 (1676990428)
Command Writed: 8/8 (1676990430)
Command Writed: 8/8 (1676990432)
Command Writed: 8/8 (1676990434)
Command Writed: 8/8 (1676990436)
Command Writed: 8/8 (1676990438)
Command Writed: 8/8 (1676990440)
Command Writed: 8/8 (1676990442)

运行环境信息:

其他内容

  1. 通过vcpkg安装了CSerialPort库。
  2. 该串口数据可以使用PurgeCommReadFile函数正常读取。例如:
    PurgeComm(hCom, PURGE_RXCLEAR | PURGE_TXCLEAR);
    char buf[64] = { '\0' };
    DWORD dwRead;
    while (hCom != INVALID_HANDLE_VALUE)
    {
        memset(buf, 0, sizeof(buf));
        if (ReadFile(hCom, buf, 64, &dwRead, NULL))
        {
            if (dwRead == 0)
                continue;
            vBuffer.clear();
            vBuffer.insert(vBuffer.end(), &buf[0], &buf[(dwRead >= 64 ? 63 : dwRead)]);
            vBuffer.shrink_to_fit();
        }
    }
    //hCom: HANDLE
    //vBuffer: std::vector<unsigned char>
itas109 commented 1 year ago

建议先不设置读取超时进行测试 serial.setReadIntervalTimeout(0);

或者 参考以下文档在4.2.2或4.3.0中开启日志功能测试 https://blog.csdn.net/itas109/article/details/129250464

[CSERIALPORT_DEBUG] CSerialPortWinBase::openPort - portName: COM1, baudRate: 9600, dataBit: 8, parity: 0, stopBit: 0, flowControl: 0, mode: async, readBufferSize:4096(4096), readIntervalTimeoutMS: 0, minByteReadNotify: 1
Open COM1 Success
[CSERIALPORT_DEBUG] CSerialPortWinBase::writeData - write. len: 5, hex(top100): 3132333435
[CSERIALPORT_DEBUG] CSerialPortWinBase::writeData - write. len: 7, hex(top100): 69746173313039
[CSERIALPORT_DEBUG] CSerialPortWinBase::commThreadMonitor - write buffer(usedLen 29). len: 29, hex(top100): 68747470733A2F2F626C6F672E6373646E2E6E65742F69746173313039
[CSERIALPORT_DEBUG] CSerialPortWinBase::commThreadMonitor - onReadEvent. portName: COM1, readLen: 29
[CSERIALPORT_DEBUG] CSerialPortWinBase::readData - read. len: 29, hex(top100): 68747470733A2F2F626C6F672E6373646E2E6E65742F69746173313039
COM1 - Count: 1, Length: 29, Str: https://blog.csdn.net/itas109, Hex: 0x68 0x74 0x74 0x70 0x73 0x3A 0x2F 0x2F 0x62 0x6C 0x6F 0x67 0x2E 0x63 0x73 0x64 0x6E 0x2E 0x6E 0x65 0x74 0x2F 0x69 0x74 0x61 0x73 0x31 0x30 0x39
[CSERIALPORT_DEBUG] CSerialPortWinBase::writeData - write. len: 29, hex(top100): 68747470733A2F2F626C6F672E6373646E2E6E65742F69746173313039
sethome2 commented 1 year ago

建议先不设置读取超时进行测试 serial.setReadIntervalTimeout(0);

或者 参考以下文档在4.2.2或4.3.0中开启日志功能测试 https://blog.csdn.net/itas109/article/details/129250464

[CSERIALPORT_DEBUG] CSerialPortWinBase::openPort - portName: COM1, baudRate: 9600, dataBit: 8, parity: 0, stopBit: 0, flowControl: 0, mode: async, readBufferSize:4096(4096), readIntervalTimeoutMS: 0, minByteReadNotify: 1
Open COM1 Success
[CSERIALPORT_DEBUG] CSerialPortWinBase::writeData - write. len: 5, hex(top100): 3132333435
[CSERIALPORT_DEBUG] CSerialPortWinBase::writeData - write. len: 7, hex(top100): 69746173313039
[CSERIALPORT_DEBUG] CSerialPortWinBase::commThreadMonitor - write buffer(usedLen 29). len: 29, hex(top100): 68747470733A2F2F626C6F672E6373646E2E6E65742F69746173313039
[CSERIALPORT_DEBUG] CSerialPortWinBase::commThreadMonitor - onReadEvent. portName: COM1, readLen: 29
[CSERIALPORT_DEBUG] CSerialPortWinBase::readData - read. len: 29, hex(top100): 68747470733A2F2F626C6F672E6373646E2E6E65742F69746173313039
COM1 - Count: 1, Length: 29, Str: https://blog.csdn.net/itas109, Hex: 0x68 0x74 0x74 0x70 0x73 0x3A 0x2F 0x2F 0x62 0x6C 0x6F 0x67 0x2E 0x63 0x73 0x64 0x6E 0x2E 0x6E 0x65 0x74 0x2F 0x69 0x74 0x61 0x73 0x31 0x30 0x39
[CSERIALPORT_DEBUG] CSerialPortWinBase::writeData - write. len: 29, hex(top100): 68747470733A2F2F626C6F672E6373646E2E6E65742F69746173313039

我也遇到了同样的问题,但是 add_definitions(-DCSERIALPORT_DEBUG) 参数似乎在vcpkg安装的CSerialPort中不起作用,并没有打印调试信息,其版本过低。我试过在vcpkg中执行git pull更新数据,最新版本依旧是4.2.1。

利用readAllData(data)函数可以正常读取串口发来的数据。

CSerialPort版本:4.2.1 运行平台:Windows 10 22H2 编译器:VS2022 打开C++ 20支持

我还在进一步检查问题出在哪里了。。。

itas109 commented 1 year ago

vcpkg的CSerialPort已更新到4.3.0