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. Arduino Serial Port Communications - Can't Open Arduino
QtWS25 Last Chance

Arduino Serial Port Communications - Can't Open Arduino

Scheduled Pinned Locked Moved General and Desktop
arduinoserial portserialportwriteread
7 Posts 7 Posters 4.3k Views
  • 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.
  • M Offline
    M Offline
    MagicalJourney
    wrote on last edited by
    #1

    Currently I'm working on a project with Qt that requires me to be able to read and write to an Arduino. Right now I'm using this code to communicate with the Arduino:

    if(arduino_is_available){
    //open and configure serialport
    arduino->setPortName(arduino_port_name);
    arduino->open(QIODevice::ReadWrite);
    arduino->setBaudRate(QSerialPort::Baud9600);
    arduino->setDataBits(QSerialPort::Data8);
    arduino->setParity(QSerialPort::NoParity);
    arduino->setStopBits(QSerialPort::OneStop);
    arduino->setFlowControl(QSerialPort::NoFlowControl);
    

    But I've determined that the Arduino is not being opened even though I think I tell it to open in that code. I know that the Arduino is connected to the right port because Qt can recognize that Arduino is connected but won't open it and therefore can't read or write to it. I'm largely basing my serial port code on this one here: https://github.com/vannevar-morgan/Qt-RGB-LED/blob/master/Serial_RGB_Qt/dialog.cpp

    This code works fine when he does it, and I did everything he did on the serial port side of the code, so I don't understand what is wrong. Any help would be appreciated.

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

      Hi and welcome to devnet,

      First thing to do is check whether open return true of false. If false, check what error you get. One other thing to check is whether you have the permissions to access the serial port ?

      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
      0
      • M MagicalJourney

        Currently I'm working on a project with Qt that requires me to be able to read and write to an Arduino. Right now I'm using this code to communicate with the Arduino:

        if(arduino_is_available){
        //open and configure serialport
        arduino->setPortName(arduino_port_name);
        arduino->open(QIODevice::ReadWrite);
        arduino->setBaudRate(QSerialPort::Baud9600);
        arduino->setDataBits(QSerialPort::Data8);
        arduino->setParity(QSerialPort::NoParity);
        arduino->setStopBits(QSerialPort::OneStop);
        arduino->setFlowControl(QSerialPort::NoFlowControl);
        

        But I've determined that the Arduino is not being opened even though I think I tell it to open in that code. I know that the Arduino is connected to the right port because Qt can recognize that Arduino is connected but won't open it and therefore can't read or write to it. I'm largely basing my serial port code on this one here: https://github.com/vannevar-morgan/Qt-RGB-LED/blob/master/Serial_RGB_Qt/dialog.cpp

        This code works fine when he does it, and I did everything he did on the serial port side of the code, so I don't understand what is wrong. Any help would be appreciated.

        P Offline
        P Offline
        pschuster
        wrote on last edited by
        #3

        @MagicalJourney Did you have any luck? I also want to talk to an Arduino and every time I try to open the port I get the error message that "File exists" (QSerialPort::errorString).

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          yoavmil
          wrote on last edited by
          #4

          may be try setDataTerminalReady(true);. This is some code I've written over a year ago. I don't remember why each line, but it worked.

          port = new QSerialPort(*portInfo);
          port->moveToThread(thread);
          
          port->setDataTerminalReady(false);
          
          if (!port->open(QIODevice::ReadWrite)) {
          	qDebug() << QString("failed to connect: failed to open port %1").arg(port->errorString());
          	goto failed;
          }
          
          if (!port->setBaudRate(115200)) {
          	qDebug() << QString("failed to connect: failed to setBaudRate(115200) %1").arg(port->errorString());
          	goto failed;
          }
          
          if (!port->setStopBits(QSerialPort::OneStop)) {
          	qDebug() << QString("failed to connect: failed to set QSerialPort::OneStop %1").arg(port->errorString());
          	goto failed;
          }
          if (!port->setParity(QSerialPort::NoParity)) {
          	qDebug() << QString("failed to connect: failed to set QSerialPort::NoParity %1").arg(port->errorString());
          	goto failed;
          }
          if (!port->setDataBits(QSerialPort::Data8)) {
          	qDebug() << QString("failed to connect: failed to set QSerialPort::Data8 %1").arg(port->errorString());
          	goto failed;
          }
          
          port->setDataTerminalReady(true);
          
          if (!port->isOpen()) {
          	goto failed;
          }
          
          F 1 Reply Last reply
          1
          • K Offline
            K Offline
            kuzulis
            Qt Champions 2020
            wrote on last edited by kuzulis
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jeroen3
              wrote on last edited by
              #6

              Workaround: Start an application (QProcess) that pipes the serialport to a localhost socket.

              1 Reply Last reply
              0
              • Y yoavmil

                may be try setDataTerminalReady(true);. This is some code I've written over a year ago. I don't remember why each line, but it worked.

                port = new QSerialPort(*portInfo);
                port->moveToThread(thread);
                
                port->setDataTerminalReady(false);
                
                if (!port->open(QIODevice::ReadWrite)) {
                	qDebug() << QString("failed to connect: failed to open port %1").arg(port->errorString());
                	goto failed;
                }
                
                if (!port->setBaudRate(115200)) {
                	qDebug() << QString("failed to connect: failed to setBaudRate(115200) %1").arg(port->errorString());
                	goto failed;
                }
                
                if (!port->setStopBits(QSerialPort::OneStop)) {
                	qDebug() << QString("failed to connect: failed to set QSerialPort::OneStop %1").arg(port->errorString());
                	goto failed;
                }
                if (!port->setParity(QSerialPort::NoParity)) {
                	qDebug() << QString("failed to connect: failed to set QSerialPort::NoParity %1").arg(port->errorString());
                	goto failed;
                }
                if (!port->setDataBits(QSerialPort::Data8)) {
                	qDebug() << QString("failed to connect: failed to set QSerialPort::Data8 %1").arg(port->errorString());
                	goto failed;
                }
                
                port->setDataTerminalReady(true);
                
                if (!port->isOpen()) {
                	goto failed;
                }
                
                F Offline
                F Offline
                Francois_Lauzon
                wrote on last edited by
                #7

                @yoavmil I have been searching many forum and what was missing for me was the setDataTerminalReady(true);

                Finaly, it is working now!

                1 Reply Last reply
                0

                • Login

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