简体   繁体   English

读写十六进制trame qt串行端口

[英]write and read hexadecimale trame qt serial port

How can I write and read hex data when I open serial port? 打开串行端口时如何读写十六进制数据?

if (serial->isOpen()) {
    qDebug() << "seccus: " ;
    static const char buff[] = {0xA1, 0xFF, 0xFF, 0x00,97};
    serial->write(buff,sizeof(buff));
}
if (serial->bytesAvailable()) {   // If there are bytes available
    QByteArray f_data; // data container
    f_data.clear();

    if (serial->open(QIODevice::ReadWrite)) { // Try to open the port
        while(serial->bytesAvailable()) { // Reading loop
            f_data.append(serial->readAll());
       }

       serial->flush();
    }
    qDebug()<< "the result" << f_data; // Check the result
}

if(!serial->isOpen())
    qDebug() << "carte disconnect"<<serial->errorString();

To debug your issue, I would start with simplifying what you're trying to do. 为了调试您的问题,我将从简化您要尝试的操作开始。 Try writing some test code to start, to make sure things work as you expect: 尝试编写一些测试代码以启动,以确保一切正常。

  1. Test serial is open - should not be 测试序列已打开-不应
  2. Open serial 开放序列
  3. Test serial is open - should be 测试序列已打开-应该是
  4. Test if bytes are available - should not be 测试字节是否可用-不应该
  5. write data to serial. 将数据写入串行。 Flush. 同花顺。
  6. Test if bytes are available - should be 测试字节是否可用-应该是
  7. Read and print to screen (ie test your reading it correctly) 阅读并打印到屏幕上(即测试您的阅读是否正确)
  8. Close the serial port - test its not open 关闭串口-测试其未打开

Perhaps if one of these steps doesn't work as you expect, you can at least focus on why that particular part is not working. 也许如果这些步骤之一没有按您预期的那样工作,那么您至少可以专注于特定部分为何无法工作的原因。

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM