How to write on serial port using Qextserialport

▼魔方 西西 提交于 2019-12-22 08:48:51

问题


I am using Ubunt 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.

If PC send: a , then PC receives reply [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.

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();
}

Any idea what is wrong?


回答1:


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

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



来源:https://stackoverflow.com/questions/6274887/how-to-write-on-serial-port-using-qextserialport

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!