Sending data to serial
-
wrote on 24 May 2023, 21:26 last edited by StenGay
I've tried for a few hours now to send a simple message to my Rasberry Pico through COM12 but I can't get it to work.
I'm fairly certain my Pico is set up correctly because when I use Hercules to send data it pick up the data just fine, it's just that I have to set the Mode from "Free" to "Data". on Hercules. Not sure what that means.
Am I sending the data wrongly formatted or what's wrong?
I do get "Data sent successfully" but the Pico is still not receiving it. I do however get the error "QObject::startTimer: Timers can only be used with threads started with QThread" even though I'm not using timers nor threads.
#include <QSerialPort> #include <QDebug> int main() { QSerialPort serialPort; serialPort.setPortName("COM12"); serialPort.setBaudRate(QSerialPort::Baud115200); serialPort.setDataBits(QSerialPort::Data8); serialPort.setParity(QSerialPort::NoParity); serialPort.setStopBits(QSerialPort::OneStop); serialPort.setFlowControl(QSerialPort::NoFlowControl); if (serialPort.open(QIODevice::WriteOnly)) { QByteArray input = QString("addPoints").toLocal8Bit(); qint64 bytesWritten = serialPort.write(input); serialPort.write("test"); if (bytesWritten == -1) { qDebug() << "Failed to write to serial port:" << serialPort.errorString(); } else { qDebug() << "Data sent successfully"; } serialPort.close(); } return 0; }
-
I've tried for a few hours now to send a simple message to my Rasberry Pico through COM12 but I can't get it to work.
I'm fairly certain my Pico is set up correctly because when I use Hercules to send data it pick up the data just fine, it's just that I have to set the Mode from "Free" to "Data". on Hercules. Not sure what that means.
Am I sending the data wrongly formatted or what's wrong?
I do get "Data sent successfully" but the Pico is still not receiving it. I do however get the error "QObject::startTimer: Timers can only be used with threads started with QThread" even though I'm not using timers nor threads.
#include <QSerialPort> #include <QDebug> int main() { QSerialPort serialPort; serialPort.setPortName("COM12"); serialPort.setBaudRate(QSerialPort::Baud115200); serialPort.setDataBits(QSerialPort::Data8); serialPort.setParity(QSerialPort::NoParity); serialPort.setStopBits(QSerialPort::OneStop); serialPort.setFlowControl(QSerialPort::NoFlowControl); if (serialPort.open(QIODevice::WriteOnly)) { QByteArray input = QString("addPoints").toLocal8Bit(); qint64 bytesWritten = serialPort.write(input); serialPort.write("test"); if (bytesWritten == -1) { qDebug() << "Failed to write to serial port:" << serialPort.errorString(); } else { qDebug() << "Data sent successfully"; } serialPort.close(); } return 0; }
wrote on 24 May 2023, 22:11 last edited by JonB@StenGay said in Sending data to serial:
QObject::startTimer: Timers can only be used with threads started with QThread
I wouldn't try this code without declaring a
QCoreApplication a()
object.
Then I would try aserialPort.waitForBytesWritten()
before closing, just in case. -
@StenGay said in Sending data to serial:
QObject::startTimer: Timers can only be used with threads started with QThread
I wouldn't try this code without declaring a
QCoreApplication a()
object.
Then I would try aserialPort.waitForBytesWritten()
before closing, just in case.wrote on 24 May 2023, 22:19 last edited by StenGay@JonB I made a console application just to be sure nothing else was preventing it from working.
I've also tried in a Widget application with ;
QCoreApplication a() ;
declared, I also addedserialPort.waitForBytesWritten();
before ;serialPort.close();;
but still no data received on the Rasberry. But no QObject::startTimer error so that's something. -
@StenGay said in Sending data to serial:
I made a console application
Also a Qt console application requires https://doc.qt.io/qt-6/qcoreapplication.html
-
@JonB I made a console application just to be sure nothing else was preventing it from working.
I've also tried in a Widget application with ;
QCoreApplication a() ;
declared, I also addedserialPort.waitForBytesWritten();
before ;serialPort.close();;
but still no data received on the Rasberry. But no QObject::startTimer error so that's something.wrote on 25 May 2023, 06:08 last edited by@StenGay
Yes, theQ...Application()
is needed for any Qt program. I cannot spot anything wrong in your code. Though I don't know anything about whether the serial port settings are correct or what you would see if they are not. Otherwise I have to question your "but the Pico is still not receiving it". I don't know, is there a tool you can use to monitor sending on a serial port to prove what is going out? -
@StenGay
Yes, theQ...Application()
is needed for any Qt program. I cannot spot anything wrong in your code. Though I don't know anything about whether the serial port settings are correct or what you would see if they are not. Otherwise I have to question your "but the Pico is still not receiving it". I don't know, is there a tool you can use to monitor sending on a serial port to prove what is going out?wrote on 1 Jun 2023, 22:23 last edited by@JonB hey sorry didnt receive a notification of the reply.
I dont know if my computer was acting up but everytime I ran the build I had to open and close the port with puyty then run the build. then the code worked. very weird.