Problem with serial port opening
-
@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?
-