QSerialPort to Arduino doesn't write but I bet it is opened.
-
Hi folks,
I just started a new customer project where I will set functionality to various microcontrollers... Now just Arduino.
The idea is to set from Qt to the Arduino specific commands using the Serial.
The functionality is first:
-
Initialize the QSerialPort.
-
Open the SerialPort with the Configuration of the Arduino
-
Write commands, in this case, a character.
In sudo su, I run the application and the Arduino responds with its LED "L" blinking a few times, going of and then turning on statically.
But it's not writing anything even when I just trying to handle any command.
I'm also handling errors in my functions and with the signal errorOccurred... It seems that there is no error.
I tested the Arduino with the Serial Monitor and I works correctly... I bet the Arduino is open to Qt, and it permits the writing, but actually, the Arduino doesn't respond.
For reference, I tested, isOpen,is Valid, is not busy, and finally is Writable. It passes all the test, just that is not writing!
PD. I did a QWidget communicating perfectly with the Arduino once, it worked nicely and I followed the same logic in this project but I don't know what's bad.
For reference: the send() function where I write... I tried also with QIODevice::write()void LedStripsSender::send(char command) { qDebug()<<Q_FUNC_INFO; if(microPort->isWritable()){ qDebug()<<Q_FUNC_INFO<<"is Writable: "<<command; microPort->writeData(&command); } else qDebug()<<"Send() error:: unable to send command: "<< microPort->error(); }
-
-
@aha_1980 said in QSerialPort to Arduino doesn't write but I bet it is opened.:
You should add yourself to the group "dialout" instead of running your program as root.
That's a good advice.
Actually, I solved it so far.
The solution was to wait a few ms to the startup and send commands finish.
QCoreApplication a(argc, argv); LedStripsSender s; QTest::qWait(500); for(char c;cin>>c;){ if(c == 'i' || c=='o'){ s.send(c); QTest::qWait(500); } else if(c=='q') break; else qDebug()<<"Main():: command not supported"; } return 0;
Now, I'm interested in know how much it las Arduino to startup (surely depends of the Arduino program).
The qWait after sending the message is to ensure that the command was sent. This is because the application gives back the Serial Port resources when the application returns (apparently very fast).
I'm going to make it as solved, thanks.
-
Hi,
Are you getting any answer when using another terminal application ?
-
I had a similar project, and I mimicked the same setup:
microPort->setBaudRate(QSerialPort::Baud9600); microPort->setDataBits(QSerialPort::Data8); microPort->setParity(QSerialPort::NoParity); microPort->setStopBits(QSerialPort::OneStop); microPort->setFlowControl(QSerialPort::NoFlowControl);
Here is the output, the 8 and 7 shows the bytes that were send, http://doc.qt.io/qt-5/qiodevice.html#write says that return -1 if there is an error in teh writing, so here is no error.
Port Info: 6790 Product: 29987 Micro Port Name is: "ttyUSB0" void LedStripsSender::send(const char&) void LedStripsSender::send(const char&) is Writable: i 8 void LedStripsSender::send(const char&) void LedStripsSender::send(const char&) is Writable: o 7
-
Hi @Charlie_Hdz,
How is the Ardiuno connected to the PC? With a "real" serial connection, or by USB? If USB, which serial-USB converter hardware/driver is used?
In sudo su, I run the application and the Arduino responds with its LED "L" blinking a few times, going of and then turning on statically.
You should add yourself to the group "dialout" instead running your program as root.
-
@aha_1980 said in QSerialPort to Arduino doesn't write but I bet it is opened.:
You should add yourself to the group "dialout" instead of running your program as root.
That's a good advice.
Actually, I solved it so far.
The solution was to wait a few ms to the startup and send commands finish.
QCoreApplication a(argc, argv); LedStripsSender s; QTest::qWait(500); for(char c;cin>>c;){ if(c == 'i' || c=='o'){ s.send(c); QTest::qWait(500); } else if(c=='q') break; else qDebug()<<"Main():: command not supported"; } return 0;
Now, I'm interested in know how much it las Arduino to startup (surely depends of the Arduino program).
The qWait after sending the message is to ensure that the command was sent. This is because the application gives back the Serial Port resources when the application returns (apparently very fast).
I'm going to make it as solved, thanks.
-
Don't use
QTest::qWait
in anything else than tests. It's really not meant for prototyping nor production code. -
I tried with sleep() and usleep() before... Rarely, they didn't work. Can be the level in which they work?
usleep() stops execution fo the complete thread. http://man7.org/linux/man-pages/man3/usleep.3.html
While qWait is in program level with enabling UI and network communication http://doc.qt.io/qt-5/qtest.html#qWait
-
For testing. If you want to start something after a delay, then use QTimer and a lambda for example.