Linux 如何使用 Qextserialport 在串口上写入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6274887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:24:16  来源:igfitidea点击:

How to write on serial port using Qextserialport

linuxqtserial-portqextserialport

提问by Karim

I am using Ubunt 10.10.

我正在使用 Ubuntu 10.10。

I want to communicate with a microcontroller (ATmega328p) using serial through QT, so for testing I have created a dummy application on the microcontroller that reads character from the UART and responds on the serial port (replies) with the same character but in between brackets.

我想通过 QT 使用串行与微控制器 (ATmega328p) 进行通信,因此为了测试,我在微控制器上创建了一个虚拟应用程序,该应用程序从 UART 读取字符并在串行端口上响应(回复)具有相同字符但在括号之间.

If PC send: a , then PC receives reply [a]

如果 PC send: a ,则 PC 收到回复 [a]

The application is working great, when I am using hyper terminal (GtkTerm) to send the characters from PC, but when I use QT it does not work. I am sending from QT a character on the serial port, and I am waiting to receive a reply on the hyper terminal, but I do not get any reply.

该应用程序运行良好,当我使用超级终端 (GtkTerm) 从 PC 发送字符时,但当我使用 QT 时它不起作用。我在串口上从 QT 发送一个字符,我正在等待在超级终端上收到回复,但我没有收到任何回复。

Serial Communication properties: Baud9600, Parity_none, Stop_bits_1, Data_8bits, Flow_control_Off

串行通信属性:Baud9600、Parity_none、Stop_bits_1、Data_8bits、Flow_control_Off

#include <QtCore/QCoreApplication>
#include <qextserialport.h>

int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);

   const char data[]= "e";
   QextSerialPort * port = new QextSerialPort("/dev/ttyUSB0");

   port->setBaudRate(BAUD9600);
   port->setFlowControl(FLOW_OFF);
   port->setParity(PAR_NONE);
   port->setDataBits(DATA_8);
   port->setStopBits(STOP_1);
   port->setTimeout(0);
   bool res = false;
   res = port->open(QIODevice::ReadWrite | QIODevice::Unbuffered);

   if(res)
   {
       qDebug("Opened");
       qDebug(data);
       int i = port->write(data, sizeof(data));
   }
   else
   {
       qDebug("Failed to connect");
   }

   return a.exec();
}

Any idea what is wrong?

知道出了什么问题吗?

采纳答案by Rapha?l Poggi

It's not working because you open the serial port 2 times ( 1 time in Qt , and an other time with your HyperTerminal ).

它不起作用,因为您打开串行端口 2 次(在 Qt 中打开 1 次,在 HyperTerminal 中打开一次)。

If you want get the reply of your command, you have to it on your Qt program.

如果你想得到你的命令的回复,你必须在你的 Qt 程序中。