Problem with serial port opening
-
I've been trying to open a connection between my qt app and an Arduino uno via serial port connection. I have all the correct header files included, ive looked at multiple examples and done exactly as others have and i still get errors for no apparent reason my main problem is with the QIODevice::open() function it will always give an error and i can't quite figure out if i'm being stupid or if i've missed something. Main file cpp, header file, and header files #included are shown in the screenshots:
-
@will_craig You should check the return value of open().
Also, what does https://doc.qt.io/qt-5/qserialport.html#error-prop tell you? -
@jsulm said in Problem with serial port opening:
@will_craig You should check the return value of open().
Also, what does https://doc.qt.io/qt-5/qserialport.html#error-prop tell you?Won't tell him a thing if it does not compile ;-)
@Will_Craig not sure but try importing QIODevice. Or use
QSerialPort::ReadOnly
instead ofQIODevice::ReadOnly
. -
@sierdzio said in Problem with serial port opening:
Won't tell him a thing if it does not compile ;-)
It does - see the application output in the screen-shots. The error comes from QtCreators code model...
-
@will_craig What does https://doc.qt.io/qt-5/qserialport.html#error-prop tell you?
-
-
@will_craig Please post code as text, not as picture. That makes it easier for us to copy parts of the code for commenting.
What do you think should
QSerialPort::OpenMode()
return? It is not even related to yourarduino
variable.The correct way is:
if (!arduino->open(QIODevice::ReadWrite)) { // error handling } else { // port successfully opened }
As a side note: What should
ReadOnly
do with a serial port? I'm not even sure that is supported on all platforms. Better useReadWrite
(you don't need to write if you don't want to).Regards
-
@aha_1980 Sorry haven't used forums that much before but heres the code with the fix you told me. The output reaches the successful section of the if statement and i'm still getting the "No viable conversion" error on line 70 (above) and as noted in code below. The port still recognises that it is "NotOpen" which confuses me.
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->TempSensor->display("----"); ui->PHSensor->display("----"); ui->MoistureSensor->display("----"); arduino = new QSerialPort(this); arduinoPortName = "cu.usbmodem14201"; openSerial(); //readSerial(); } MainWindow::~MainWindow() { if(arduino->isOpen()){arduino->close();} delete ui; } void MainWindow::openSerial() { arduinoIsAvailable = true; // open and configure the serialport qDebug() << "Found the arduino port...\n" << "Port name used: " << arduinoPortName << "\n"; arduino->setPortName(arduinoPortName); arduino->setBaudRate(QSerialPort::Baud9600, QSerialPort::Directions()); arduino->setDataBits(QSerialPort::Data8); arduino->setFlowControl(QSerialPort::NoFlowControl); arduino->setParity(QSerialPort::NoParity); arduino->setStopBits(QSerialPort::OneStop); connect(arduino, SIGNAL(readyRead()), this, SLOT(readSerial())); if(!arduino->open(QIODevice::ReadWrite)){ //error is here qDebug() << "Error Handling Section Reached"; }else{ qDebug() << "Port opened succesfully...\n"; } qDebug() << arduino->error(); qDebug() << QSerialPort::OpenMode(); /* // give error message if not available QMessageBox MsgBx; MsgBx.setText("Error: Null Connection"); MsgBx.exec(); */ }
output:
Found the arduino port... Port name used: "cu.usbmodem14201" Port opened succesfully... QSerialPort::NoError OpenMode( "NotOpen" )
thankyou for you help
-
Hi @will_craig,
No problem. We all started some day :)
f statement and im still getting the "No viable conversion" error on line 70 (above) and as noted in code below.
That is a Code Model Error, please ignore it in the first place. If your code compiles, all is fine. Have you included
<QIODevice>
?if(arduino->open(QIODevice::ReadWrite)){ qDebug() << "Error Handling Section Reached"; }else{ qDebug() << "Port opened succesfully...\n"; }
You didn't carefully copy my code!
I wrote
!
in theif
condition, you completely inverted the branches so it will not work.And please put your code in code tags, I'll add them for you now.
Regards
Edit: and once again:
qDebug() << QSerialPort::OpenMode();
is completely pointless. Remove that line! -
@aha_1980 I've added the ! and the code goes to the correct section of the successful opening, i have used the header <QIODevice>, <QSerialPort> and <QSerialPortInfo>.
So you're saying that even though it gives me the error before compiling as long as it still compiles there is no problem? cause i have been assuming that if that error has come up then the code will not work properly. thanks again and sorry about not using code tags.
My code now looks like this:if(!arduino->open(QIODevice::ReadWrite)){ qDebug() << "Error Handling Section Reached"; }else{ qDebug() << "Port opened succesfully...\n"; } qDebug() << arduino->error();
and this is my debug output:
Found the arduino port... Port name used: "cu.usbmodem14201" Port opened succesfully... QSerialPort::NoError
Is this correct?, thanks again :)
-
Hi @will_craig,
looks good now and should work!
So you're saying that even though it gives me the error before compiling as long as it still compiles there is no problem?
Yeah, the Clang Code Model is nice, but sometimes it goes crazy. If it annoys you very much, you can deactivate it by Help > About Plugins > Clang Code Model and restart Creator. Then you use the "old" code model, which has fewer features but works more solid.
Regards
-
@will_craig You're welcome :)
-
@will_craig is your issue solved? Then please don't forget to mark your post as such. Thanks.
-
@Pablo-J-Rogina how do i do that?
-