如何使用Qextserialport在串行端口上写 [英] How to write on serial port using Qextserialport

查看:241
本文介绍了如何使用Qextserialport在串行端口上写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ubunt 10.10.

I am using Ubunt 10.10.

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

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.

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

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

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

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.

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

Serial Communication properties: 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();
}

有什么想法吗?

推荐答案

它不起作用,因为您打开了2次串行端口(在Qt中打开了1次,在超级终端中又打开了一次).

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

如果要获得命令的答复,则必须在Qt程序中对其进行答复.

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

这篇关于如何使用Qextserialport在串行端口上写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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