4

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?

László Papp
  • 51,870
  • 39
  • 111
  • 135
Karim
  • 1,526
  • 4
  • 13
  • 16
  • What is `port->write` returning? – Nemo Jun 08 '11 at 06:31
  • 1
    _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._ I'm not entirely sure about how serial ports work in Linux, but aren't they opened exclusively by one application at a time? Why don't you read the response in the Qt program as well? – Manjabes Jun 08 '11 at 07:49
  • two ideas: 1) set timeout to 100; 2) fry to call flush after write – Raiv Jun 08 '11 at 11:41
  • @Raiv: I have tried both the timeout and flush idea and still nothing happens. @Manjabes `int i = port->write(data, sizeof(data));` The debugger got `i = 2` after executing this line – Karim Jun 08 '11 at 19:21
  • well... it means that data at least written to port. Maybe you trying to catch data you sent in the wrong way? if you are reading in terminal the same port wou writing to - you will se nothing, try some kind of sniffer instead – Raiv Jun 09 '11 at 07:59
  • @Raiv I am controlling motors through the serial port, so when I use the terminal, the motors are controlled perfectly, but this is not the case in QT. I just want to say that reading the serial is not the problem, because, if it was written correctly on the serial, the motors should have been moved according to the control strategy, I am just replying with character in between brackets just to make sure that the communications is working probably. Python is great with dealing with serial ports, and it is working, but I need good GUI :( – Karim Jun 09 '11 at 15:02
  • there is pyQt - qt port to python... – Raiv Jun 09 '11 at 15:26
  • Have you gotten further with this? If you are "debugging" using the state of your motors, it could happen that e.g. the endian is wrong between the PC and your microcontroller, or the formatting sent is wrong. Have your tried looking at the (maybe) received data from a debugger on your AVR? – chwi Apr 04 '13 at 13:56

1 Answers1

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.