Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sending data to serial
Qt 6.11 is out! See what's new in the release blog

Sending data to serial

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    StenGay
    wrote on last edited by StenGay
    #1

    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;
    }
    
    JonBJ 1 Reply Last reply
    0
    • S 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;
      }
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @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 a serialPort.waitForBytesWritten() before closing, just in case.

      S 1 Reply Last reply
      0
      • JonBJ 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 a serialPort.waitForBytesWritten() before closing, just in case.

        S Offline
        S Offline
        StenGay
        wrote on last edited by StenGay
        #3

        @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 added serialPort.waitForBytesWritten(); before ; serialPort.close();; but still no data received on the Rasberry. But no QObject::startTimer error so that's something.

        JonBJ 1 Reply Last reply
        0
        • jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S 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 added serialPort.waitForBytesWritten(); before ; serialPort.close();; but still no data received on the Rasberry. But no QObject::startTimer error so that's something.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @StenGay
            Yes, the Q...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?

            S 1 Reply Last reply
            0
            • JonBJ JonB

              @StenGay
              Yes, the Q...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?

              S Offline
              S Offline
              StenGay
              wrote on last edited by
              #6

              @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.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved