Arduino Serial Port Communications - Can't Open Arduino
-
wrote on 9 Jun 2015, 15:38 last edited by
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.
-
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 ?
-
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.
wrote on 30 Jun 2015, 15:04 last edited by@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).
-
wrote on 1 Jul 2015, 05:43 last edited by
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; }
-
Qt Champions 2020wrote on 1 Jul 2015, 10:07 last edited by kuzulis 7 Jan 2015, 10:08This post is deleted!
-
wrote on 1 Jul 2015, 13:57 last edited by
Workaround: Start an application (QProcess) that pipes the serialport to a localhost socket.
-
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; }
wrote on 6 Oct 2023, 11:39 last edited by@yoavmil I have been searching many forum and what was missing for me was the setDataTerminalReady(true);
Finaly, it is working now!