串行通信不通过 QSerialPort (Qt) 工作,但通过终端工作(白蚁) [英] Serial Comms Not Working Through QSerialPort (Qt) But Is Working Via Terminal (Termite)

查看:117
本文介绍了串行通信不通过 QSerialPort (Qt) 工作,但通过终端工作(白蚁)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很奇怪的问题,这两天一直让我发疯.

I have a very strange issue, which has been driving me mad for the last two days.

我有一个要控制的串行设备(LS 100 光度计).使用设置了正确参数的终端(白蚁),我可以发送命令(MES"),然后发送定界符(CR LF),然后按预期返回一些测量数据.

I have a serial device (LS 100 Photometer) which I am trying to control. Using a terminal (termite) set up with the correct parameters, I can send the command ("MES"), followed by the delimeter (CR LF), and I get back some measurement data as expected.

问题是,从 Qt 中,我没有得到任何数据返回.使用嗅探器程序,我确认我发送的 5 个字节(MES CR LF)与终端发送的完全相同,并且端口设置相同.

The problem is, from Qt, I do not get any data returned. Using a sniffer program, I have confirmed that I am sending the exact same 5 bytes (MES CR LF) as the terminal is sending, and with the same port setup.

如果我将流控制更改为NoFlowControl",那么我可以取回一些数据,但它似乎毫无意义,只是一个随机字节.在任何情况下,设备文档都说要使用 RTS/CTS,这就是终端(白蚁)设置使用的.

If I change the flow control to "NoFlowControl" then I can get some data back, but it appears to be meaningless and is just one random byte. In any case the device documentation says to use RTS/CTS and that is what the terminal (termite) is set up to use.

此外,如果我使用 Qt 串行端口示例终端,我会遇到同样的问题,即无法让设备返回数据.我也尝试过使用 C# 并且遇到了完全相同的问题.似乎唯一能够与仪器通信的是 Termite 终端.

Also, if I use the Qt serialport example terminal, I get the same issue where I can't get the device to return data. I have also tried using C# and have had the exact same issue. The only thing that seems capable of communicating with the instrument is the Termite terminal.

Qt 代码:

port.setPortName(ui->cmbPort->currentText());
port.setBaudRate(QSerialPort::Baud4800);
port.setDataBits(QSerialPort::Data7);
port.setParity(QSerialPort::EvenParity);
port.setStopBits(QSerialPort::TwoStop);
port.setFlowControl(QSerialPort::HardwareControl);

if (!port.open(QIODevice::ReadWrite))
{
    connected = false;
    QMessageBox::information(this, "Failed To Open", "Failed to open the serial port");
    ui->statusBar->showMessage("Connection to " + ui->cmbPort->currentText() + " failed...");
}
else
{
    connected = true;
    ui->statusBar->showMessage("Connected to " + ui->cmbPort->currentText() + "...");
}

QByteArray cmdB;

cmdB[0] = 0x4d;
cmdB[1] = 0x45;
cmdB[2] = 0x53;
cmdB[3] = 0x0d;
cmdB[4] = 0x0a;

qint64 r = port.write(cmdB.data(), cmdB.size());
qDebug() << "Written: " << r;

然后ReadData函数在ReadyRead或每100ms调用一次:

Then the ReadData function which is called on ReadyRead or every 100ms:

QByteArray data = port.readAll();
if (data.count() != 0)
{
    qDebug() << "Read " << data.size() << " bytes";
    QString str = QString(data);
    ui->txtOutput->append(str);
}

任何帮助将不胜感激,我的头发快用完了……

Any help would be much appreciated, I'm running out of hair to pull out...

推荐答案

终于解决了.

虽然文档说要使用RTS/CTS,终端程序(白蚁)使用RTS/CTS,但解决的办法是在Qt应用程序中关闭流量控制(即NoFlowControl),然后手动开启RTS线路就在发送数据之前,像这样:

Even though the documentation says to use RTS/CTS, and the terminal program (termite) uses RTS/CTS, the solution was to turn off flow control in the Qt application (i.e. NoFlowControl), then turn on the RTS line manually just before sending data, like this:

port.setRequestToSend(true);
qint64 r = port.write(cmdB.data(), cmdB.size());
port.waitForBytesWritten(5000);
qDebug() << "Written: " << r;

这篇关于串行通信不通过 QSerialPort (Qt) 工作,但通过终端工作(白蚁)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆