Issue with serial communication with Arduino



  • Hello i am written a program to send and receive data through serial port.
    On arduino there is program which receives the serial data and send some response.
    My problem is that when i connect arduino to my pc and send data directly I do not get any response.
    But when i open the serial monitor program and send data it works fine. After that when i try to send and receive the data from my program it also work. Irrespective of how many times i restart the program. But when i remove the arduino and connect again I have to go through the same process again.
    I am using arduino leonardo


  • Moderators

    How do you send the data directly? Can you show us your code?



  • @sierdzio : I am using the terminal example (part of qt examples):
    //Sample code starts
    void MainWindow::writeData(const QByteArray &data)
    {
    serial->flush();
    serial->write(data);

    }

    void MainWindow::readData()
    {
    QByteArray data = serial->readAll();
    qDebug()<<"Received data is:"<<data;
    ui->lbl_receiveddata->setText(data);

    //Sample code completes
    Arduino sample code is available from following location:
    http://arduinobasics.blogspot.in/2012/07/arduino-basics-simple-arduino-serial.html

    At first i thought it would be issue related to windows or arduino and i tried so i tried accessing the code with python and it works fine.

    For more detail please refer the terminal example in qt example


  • Moderators

    Are you sure you have all port settings set exactly the same on both sides (baud rate, parity etc.)?

    Are you sure no other device is using that port at the same time? Only single app can connect simultaneously.



  • Yes, I have checked it many times and it the port is busy it give me error message.



  • @sierdzio : And one more thing when i try to run the application by keeping the baud rate as 1200
    It gives me error of the serial port.
    And when i run samples from serial monitor everything works fine


  • Moderators

    To be honest, I don't know how to help. Last time I used Arduino was a few years ago, I don't remember encountering similar issues.



  • @sierdzio :Can you please share any code, if available so that i can check the scenario with it.
    Actually the same problem is observed in qt's examples so don't know how to proceed.



  • Hi @Kira :

    And one more thing when i try to run the application by keeping the baud rate as 1200
    It gives me error of the serial port.

    You will need to show us more (initialization) code and the exact error message here...

    void MainWindow::writeData(const QByteArray &data)
    {
    serial->flush();
    serial->write(data);
    }

    Note: serial->flush() is not needed in most cases (the documentation also states this).

    And if, then it would be more useful to have it after the serial->write(data);.

    Can you verify that the data you send arrives at the Arduino?



  • @aha_1980 : You can refer to the terminal example in the qtcreator's example list
    I am using the same example.
    And i am using arduino leonardo, it program is in the given link.
    Please let me know of any other detail if required



  • @Kira

    You will need to show us [...] the exact error message here...

    Can you verify that the data you send arrives at the Arduino?



  • @aha_1980 : I was searching on internet and came across an topic which co-relates to my issue.
    Please have a look on the given link:
    https://stackoverflow.com/questions/31560694/qt-serial-port-not-working

    The serial error which i get when i connect with baud rate 1200 is as follows:

    Critical Error: The I/0 operation has been aborted because of either a thread exit or an application request.


  • Lifetime Qt Champion

    Hi,

    You should connect the errorOccured signal of your serial port and handle QSerialPort::ResourceError which would likely be close the port and open it again. Note that your arduino serial device might have had its name changed depending on the system.



  • @SGaist : One more thing i tried to communicate the same program using arduino uno and there is no issue with the program it works fine without any error any issue.
    Currently i am using arduino leonardo for my project and i need to move on with the same device.
    My python program and serial simulator program works fine with the board without giving any error and this scenario occur with qt program so just not able to figure out where exactly the problem is.



  • @SGaist : Thanks for your response. I tried to play with the terminal examples in qt example set and got the stuff working.
    Following are the observations:

    1. First you have to configure the port in the settings and set the flow control to RTS/CTS.
      Earlier was set to noflowcontrol.
    2. Connect the port
      3.Disconnect the port
      4.Reconnect the port
      Note: The above approach implies when arduino is disconnected and reconnected or fresh program is burned into the arduino.
      Apart from that only step 1 and 2 was sufficient.

    In case of Arudino uno only step 1 & 2 were required even after reconnecting and burning fresh program. May be because of the hardware specifications.
    Wondering why i have to connect, disconnect and again reconnect to get it working and only hardware flow control is working.


  • Lifetime Qt Champion

    For the control flow, check your Arduino documentation regarding the serial port settings.



  • Ohk thanks a lot


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.