Arduino Serial Port Communications - Can't Open Arduino



  • 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.


  • Lifetime Qt Champion

    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 ?



  • @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).



  • 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;
    }
    


  • This post is deleted!


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



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