check SerialPort successfully written to
-
I have a program where I am writing to a serial port, if the write was a success I want bool to return true or false.
When this line runs...
bool success = serialPort.write(someData); qDebug() << success;
I get the debug output "true". The serial port is not even connected!, please can somebody explain why it returns true when the serial port is not open....
QIODevice::read (QSerialPort): device not open
-
I have a program where I am writing to a serial port, if the write was a success I want bool to return true or false.
When this line runs...
bool success = serialPort.write(someData); qDebug() << success;
I get the debug output "true". The serial port is not even connected!, please can somebody explain why it returns true when the serial port is not open....
QIODevice::read (QSerialPort): device not open
@Aeroplane123 said in check SerialPort successfully written to:
QIODevice::read (QSerialPort): device not open
Here you are trying to read from serial port: is it the same QSerialPort instance as the one you're using for writing?
https://doc.qt.io/qt-6/qiodevice.html#write returns number of bytes written, not a boolean. In case it fails it returns -1 (which converts to TRUE if you assign it to a boolean!), so you should check the return value properly. -
Ah yes sorry, yes the serial port is definitely not open when I try to write. Yes it the same instance.
so I want to do
serialPort.write(someData);
But then please how can I check if it did actually send via the port?
-
Ah yes sorry, yes the serial port is definitely not open when I try to write. Yes it the same instance.
so I want to do
serialPort.write(someData);
But then please how can I check if it did actually send via the port?
@Aeroplane123 said in check SerialPort successfully written to:
But then please how can I check if it did actually send via the port?
Basic C/C++ stuff:
bool success = serialPort.write(someData) != -1;
-
serialPort.write(someData); bool success = serialPort.write(someData) != -1; qDebug() << success;
output is "True", but thre is no port open.
-
serialPort.write(someData); bool success = serialPort.write(someData) != -1; qDebug() << success;
output is "True", but thre is no port open.
@Aeroplane123
Please just tryqDebug() << serialPort.write(someData);
. -
OK, oddly now it is giving me false and -1, which is what I expect.