Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSerialPort to Arduino doesn't write but I bet it is opened.
Forum Updated to NodeBB v4.3 + New Features

QSerialPort to Arduino doesn't write but I bet it is opened.

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 3.7k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Charlie_Hdz
    wrote on last edited by Charlie_Hdz
    #1

    Hi folks,

    I just started a new customer project where I will set functionality to various microcontrollers... Now just Arduino.

    The idea is to set from Qt to the Arduino specific commands using the Serial.

    The functionality is first:

    • Initialize the QSerialPort.

    • Open the SerialPort with the Configuration of the Arduino

    • Write commands, in this case, a character.

    In sudo su, I run the application and the Arduino responds with its LED "L" blinking a few times, going of and then turning on statically.

    But it's not writing anything even when I just trying to handle any command.

    I'm also handling errors in my functions and with the signal errorOccurred... It seems that there is no error.

    I tested the Arduino with the Serial Monitor and I works correctly... I bet the Arduino is open to Qt, and it permits the writing, but actually, the Arduino doesn't respond.

    For reference, I tested, isOpen,is Valid, is not busy, and finally is Writable. It passes all the test, just that is not writing!

    PD. I did a QWidget communicating perfectly with the Arduino once, it worked nicely and I followed the same logic in this project but I don't know what's bad.
    For reference: the send() function where I write... I tried also with QIODevice::write()

    void LedStripsSender::send(char command)
    {
        qDebug()<<Q_FUNC_INFO;
        if(microPort->isWritable()){
            qDebug()<<Q_FUNC_INFO<<"is Writable: "<<command;
            microPort->writeData(&command);
        }
        else
            qDebug()<<"Send() error:: unable to send command: "<<
                      microPort->error();
    }
    

    Kind Regards,
    Enrique Hernandez
    gearstech.com.mx
    chernandez@gearstech.com.mx

    aha_1980A 1 Reply Last reply
    0
    • L Leonardo

      Are you sure you're not missing any setting like baud rate, parity, flow control etc?

      C Offline
      C Offline
      Charlie_Hdz
      wrote on last edited by Charlie_Hdz
      #7

      @aha_1980 said in QSerialPort to Arduino doesn't write but I bet it is opened.:

      You should add yourself to the group "dialout" instead of running your program as root.

      That's a good advice.

      Actually, I solved it so far.

      The solution was to wait a few ms to the startup and send commands finish.

          QCoreApplication a(argc, argv);
      
          LedStripsSender s;
          QTest::qWait(500);
      
          for(char c;cin>>c;){
              if(c == 'i' || c=='o'){
                  s.send(c);
                  QTest::qWait(500);
              }
              else if(c=='q')
                  break;
              else
                  qDebug()<<"Main():: command not supported";
          }
          return 0;
      

      Now, I'm interested in know how much it las Arduino to startup (surely depends of the Arduino program).

      The qWait after sending the message is to ensure that the command was sent. This is because the application gives back the Serial Port resources when the application returns (apparently very fast).

      I'm going to make it as solved, thanks.

      Kind Regards,
      Enrique Hernandez
      gearstech.com.mx
      chernandez@gearstech.com.mx

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Are you getting any answer when using another terminal application ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        C 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Are you getting any answer when using another terminal application ?

          C Offline
          C Offline
          Charlie_Hdz
          wrote on last edited by
          #3

          @SGaist

          Indeed! Using the Arduino Serial Monitor, it works nicely.

          Kind Regards,
          Enrique Hernandez
          gearstech.com.mx
          chernandez@gearstech.com.mx

          1 Reply Last reply
          0
          • L Offline
            L Offline
            Leonardo
            wrote on last edited by
            #4

            Are you sure you're not missing any setting like baud rate, parity, flow control etc?

            C 2 Replies Last reply
            1
            • L Leonardo

              Are you sure you're not missing any setting like baud rate, parity, flow control etc?

              C Offline
              C Offline
              Charlie_Hdz
              wrote on last edited by
              #5

              @Leonardo

              I had a similar project, and I mimicked the same setup:

              microPort->setBaudRate(QSerialPort::Baud9600);
                  microPort->setDataBits(QSerialPort::Data8);
                  microPort->setParity(QSerialPort::NoParity);
                  microPort->setStopBits(QSerialPort::OneStop);
                  microPort->setFlowControl(QSerialPort::NoFlowControl);
              

              Here is the output, the 8 and 7 shows the bytes that were send, http://doc.qt.io/qt-5/qiodevice.html#write says that return -1 if there is an error in teh writing, so here is no error.

              
              
              Port Info:  6790 Product:  29987
              
              Micro Port Name is:  "ttyUSB0"
              void LedStripsSender::send(const char&)
              void LedStripsSender::send(const char&) is Writable:  i
              8
              void LedStripsSender::send(const char&)
              void LedStripsSender::send(const char&) is Writable:  o
              7
              

              Kind Regards,
              Enrique Hernandez
              gearstech.com.mx
              chernandez@gearstech.com.mx

              1 Reply Last reply
              0
              • C Charlie_Hdz

                Hi folks,

                I just started a new customer project where I will set functionality to various microcontrollers... Now just Arduino.

                The idea is to set from Qt to the Arduino specific commands using the Serial.

                The functionality is first:

                • Initialize the QSerialPort.

                • Open the SerialPort with the Configuration of the Arduino

                • Write commands, in this case, a character.

                In sudo su, I run the application and the Arduino responds with its LED "L" blinking a few times, going of and then turning on statically.

                But it's not writing anything even when I just trying to handle any command.

                I'm also handling errors in my functions and with the signal errorOccurred... It seems that there is no error.

                I tested the Arduino with the Serial Monitor and I works correctly... I bet the Arduino is open to Qt, and it permits the writing, but actually, the Arduino doesn't respond.

                For reference, I tested, isOpen,is Valid, is not busy, and finally is Writable. It passes all the test, just that is not writing!

                PD. I did a QWidget communicating perfectly with the Arduino once, it worked nicely and I followed the same logic in this project but I don't know what's bad.
                For reference: the send() function where I write... I tried also with QIODevice::write()

                void LedStripsSender::send(char command)
                {
                    qDebug()<<Q_FUNC_INFO;
                    if(microPort->isWritable()){
                        qDebug()<<Q_FUNC_INFO<<"is Writable: "<<command;
                        microPort->writeData(&command);
                    }
                    else
                        qDebug()<<"Send() error:: unable to send command: "<<
                                  microPort->error();
                }
                
                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #6

                Hi @Charlie_Hdz,

                How is the Ardiuno connected to the PC? With a "real" serial connection, or by USB? If USB, which serial-USB converter hardware/driver is used?

                In sudo su, I run the application and the Arduino responds with its LED "L" blinking a few times, going of and then turning on statically.

                You should add yourself to the group "dialout" instead running your program as root.

                Qt has to stay free or it will die.

                1 Reply Last reply
                0
                • L Leonardo

                  Are you sure you're not missing any setting like baud rate, parity, flow control etc?

                  C Offline
                  C Offline
                  Charlie_Hdz
                  wrote on last edited by Charlie_Hdz
                  #7

                  @aha_1980 said in QSerialPort to Arduino doesn't write but I bet it is opened.:

                  You should add yourself to the group "dialout" instead of running your program as root.

                  That's a good advice.

                  Actually, I solved it so far.

                  The solution was to wait a few ms to the startup and send commands finish.

                      QCoreApplication a(argc, argv);
                  
                      LedStripsSender s;
                      QTest::qWait(500);
                  
                      for(char c;cin>>c;){
                          if(c == 'i' || c=='o'){
                              s.send(c);
                              QTest::qWait(500);
                          }
                          else if(c=='q')
                              break;
                          else
                              qDebug()<<"Main():: command not supported";
                      }
                      return 0;
                  

                  Now, I'm interested in know how much it las Arduino to startup (surely depends of the Arduino program).

                  The qWait after sending the message is to ensure that the command was sent. This is because the application gives back the Serial Port resources when the application returns (apparently very fast).

                  I'm going to make it as solved, thanks.

                  Kind Regards,
                  Enrique Hernandez
                  gearstech.com.mx
                  chernandez@gearstech.com.mx

                  1 Reply Last reply
                  1
                  • L Offline
                    L Offline
                    Leonardo
                    wrote on last edited by
                    #8

                    Why don't you implement some sort of flow control yourself? Make the Arduino send a "hello" message once it's up, so you know when you're ready for sending data.

                    1 Reply Last reply
                    2
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

                      Don't use QTest::qWait in anything else than tests. It's really not meant for prototyping nor production code.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      C 1 Reply Last reply
                      1
                      • SGaistS SGaist

                        Don't use QTest::qWait in anything else than tests. It's really not meant for prototyping nor production code.

                        C Offline
                        C Offline
                        Charlie_Hdz
                        wrote on last edited by
                        #10

                        @SGaist

                        I tried with sleep() and usleep() before... Rarely, they didn't work. Can be the level in which they work?

                        usleep() stops execution fo the complete thread. http://man7.org/linux/man-pages/man3/usleep.3.html

                        While qWait is in program level with enabling UI and network communication http://doc.qt.io/qt-5/qtest.html#qWait

                        Kind Regards,
                        Enrique Hernandez
                        gearstech.com.mx
                        chernandez@gearstech.com.mx

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #11

                          For testing. If you want to start something after a delay, then use QTimer and a lambda for example.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          5

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved