Solved QSerialPort to Arduino doesn't write but I bet it is opened.
-
Hi,
Are you getting any answer when using another terminal application ?
-
Indeed! Using the Arduino Serial Monitor, it works nicely.
-
Are you sure you're not missing any setting like baud rate, parity, flow control etc?
-
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.
-
Why don't you implement some sort of flow control yourself? Make the Arduino send a "hello" message once it's up, so you know when you're ready for sending data.
-
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.