简体   繁体   English

Qt串口通讯

[英]Qt Serial Port communication

I am writing a Qt application to communicate with another computer over a serial port. 我正在编写Qt应用程序,以通过串行端口与另一台计算机进行通信。 I have 2 real issues. 我有2个实际问题。

1. I can send and receive data fine, but sometimes the serial port "eats" part of my input. 1.我可以很好地发送和接收数据,但有时串行端口会“吞噬”我输入的一部分。 For example if I send: 例如,如果我发送:

cd /application/bin

sometimes (not always) it will only receive: 有时(并非总是)它只会收到:

cd /applica

(Since it's a terminal it echoes the input back. Also my prompt tells me I'm clearly in the wrong spot.) (由于它是一个终端,它回显输入。此外,我的提示还告诉我我显然在错误的位置。)

2. Also, sometimes the Qt slot which fires when there is data available doesn't fire even though I know that there's data I can receive. 2.此外,即使我知道可以接收到数据,有时在有可用数据时触发的Qt插槽也不会触发。 If I send another \\r\\n down the port the slot will fire. 如果我向该端口发送另一个\\r\\n ,则插槽将触发。 For example sometimes I'll ls something, and the command name will be read back from the port, but the contents of the folder sit there in limbo until I hit return again. 比如有时我会ls的东西,并命令名称将会从端口读回,但该文件夹的内容坐在那里无人过问,直到我再敲回车。 Then I get the listing of the directory and two prompts. 然后,我得到目录清单和两个提示。

Here's my code: 这是我的代码:

void Logic::onReadyRead(){        
        QByteArray incomingData;  
        incomingData = port->readAll();
        QString s(incomingData);
        emit dataAvailable(s);// this is a Qt slot if you don't know what it is.
        qDebug() << "in:"<< s.toLatin1();     
}

void Logic::writeToTerminal(QString string )
{
    string.append( "\r\n");
    port->write((char*)string.data(), string.length());
    if ( port->bytesToWrite() > 0){
        port->flush();
    }
    qDebug() << "out:" << string.toLatin1();
}

I found the solution, and I suspect it was an encoding error, but I'm not sure. 我找到了解决方案,但我怀疑这是编码错误,但是我不确定。 Instead of sending a QString down the serial port, sending a QByteArray fixed both problems. 而不是通过串行端口发送QString,而是发送QByteArray解决了这两个问题。 I changed the writeToTerminal() method: 我更改了writeToTerminal()方法:

void Logic::writeToTerminal(QString string )
{
    string.append( "\r");
    QByteArray ba = string.toAscii();
    port->write(ba);
}

From this forum , it appears that sometimes not all the data gets sent, and whatever does gets sent has a '\\0' appended to it. 这个论坛上 ,似乎有时并非所有数据都被发送,并且发送的所有内容都附加了“ \\ 0”。 So if 因此,如果

cd /applica'\\0' got sent, then the port->readAll() would stop there, because it thinks it has read everything. cd / applica'\\ 0'已发送,则port->readAll()会在那里停止,因为它认为已读取了所有内容。

One suggested answer on that forum was to read line by line, which your code almost does. 在该论坛上,一个建议的答案是逐行阅读,您的代码几乎可以做到。 So I think in your case, you can change your code to: 因此,我认为您可以将代码更改为:

void Logic::onReadyRead(){        
    QByteArray incomingData;  
    if(port->canReadLine()) {
      incomingData = port->readLine();
      QString s(incomingData);
      emit dataAvailable(s);// this is a Qt slot if you don't know what it is.
      qDebug() << "in:"<< s.toLatin1();
    }     
}

void Logic::writeToTerminal(QString string )
{
    string.append( "\r\n");
    port->write((char*)string.data(), string.length());
    if ( port->bytesToWrite() > 0){
        port->flush();
    }
    qDebug() << "out:" << string.toLatin1();
}

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

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