Solved QSerialPort - OpenError [solved]
-
Hello everyone,
I'm trying to use the QSerialPort to send data to a FTDI and read the data that comes back. I'm fairly new in coding, so I tried to get all the information from the examples and other posts, but nothing really helped. So my problem is that I cannot open the port and the error code that I get is number 3 OpenError what means that the port is already open?? I made sure that no other program uses the port e.g. Terminal. But still I can't open it.
meinlayout::meinlayout(QWidget *parent) : QMainWindow(parent) { serial = new QSerialPort(this); //info = new QSerialPortInfo(); foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { qDebug() << "Available Ports: " << info.portName(); } serial->setPortName("COM3"); if(serial->portName()!= "COM3") { qDebug() << tr("error set name %1").arg(serial->error()) << serial->portName(); return; } serial->setBaudRate(QSerialPort::Baud115200); if(serial->baudRate()!= 115200) { qDebug() << tr("error set Baud %1").arg(serial->error()); return; } serial->setDataBits(QSerialPort::Data8); if(serial->dataBits()!= 8) { qDebug() << tr("error set bits %1").arg(serial->error()); return; } serial->setParity(QSerialPort::NoParity); if(serial->parity()!= 0) { qDebug() << tr("error set parity %1").arg(serial->error()); return; } serial->setFlowControl(QSerialPort::NoFlowControl); if(serial->flowControl()!= 0) { qDebug() << tr("error set flow %1").arg(serial->error()); return; } serial->setStopBits(QSerialPort::OneStop); if(serial->stopBits()!= 1) { qDebug() << tr("error set stop %1").arg(serial->error()); return; } if(serial->isOpen()) { qDebug() << "Port already opened!"; serial->close(); } serial->open(QIODevice::ReadWrite); if(!serial->open(QIODevice::ReadWrite)) { qDebug() << tr("error open Port %1").arg(serial->error()); return; } } meinlayout::~meinlayout() { serial->close(); }
so the output I get is:
Available Ports: "COM3"
"error open Port 3"any suggestions what I'm doing wrong?
-
@don_kon said in QSerialPort - OpenError:
serial->open(QIODevice::ReadWrite);
if(!serial->open(QIODevice::ReadWrite)
Why do you open it twice?!
-
@jsulm Thank you for your help, after I annotated the first open() the program worked.
I didn't know theif(!serial->open(QIODevice::ReadWrite))
will try to open the port again.
-
@don_kon said in QSerialPort - OpenError [solved]:
will try to open the port again
I don't think it will try to open it again, but if it is already open it will return false.
-
@jsulm Thanks!