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. Issue using QSerialPort
Qt 6.11 is out! See what's new in the release blog

Issue using QSerialPort

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 500 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.
  • G Offline
    G Offline
    GabRioux25
    wrote on last edited by GabRioux25
    #1

    Hey, I am currently trying to make a video games using a controller made of an arduino. I am making the GUI with Qt since I found it really cool that it has an integrated serial port library. So right now i'm trying to set-up the communication of the arduino and the pc with QSerial port, so far I'm able to open the port (not a big deal but it took me hours lol), when I try to write in it it get this error :

    <3,2,2,>QObject::startTimer: Timers can only be used with threads started with QThread
    

    Here the com.h file :

    #include <QSerialPort>
    #include <iostream>
    
    using namespace std;
    
    class Comunication
    {
    
    public:
    	void openSerial();
    	Comunication();
    	void closeSerial();
    	bool sendData(int module, int* tabData, uint8_t tabSize);
    
    private:
    	QSerialPort* serial;
    
    };
    

    And here the com.cpp file

    #include "com.h"
    
    Comunication::Comunication() {
    
    	serial = new QSerialPort;
    
    }
    
    void Comunication::openSerial() {
    
    
        serial->setPortName("COM5");
        serial->setBaudRate(QSerialPort::Baud9600, QSerialPort::AllDirections);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->setReadBufferSize(5000);
        serial->open(QIODevice::ReadWrite);
        if (serial->isOpen()) {
    
            cout << "opened" << endl;
        }
         
    
    
    }
    
    void Comunication::closeSerial() {
    
        serial->close();
    }
    
    
    bool Comunication::sendData(int module, int* tabData, uint8_t tabSize)
    {
    
        char txData[30];
        int index = 0;
    
        txData[index++] = '<';
        txData[index++] = module + '0';
    
        for (int i = 0; i < tabSize; i++)
        {
            txData[index++] = ',';
    
            if (tabData[i] != 'I') {
                char tempTab[15];
    
                int sizeTempTab = sprintf(tempTab, "%d", tabData[i]);
    
                for (int j = 0; j < sizeTempTab; j++)
                {
                    txData[index++] = tempTab[j];
                }
            }
            else
                txData[index++] = 'I';
        }
    
        txData[index++] = ',';
        txData[index++] = '>';
        txData[index] = '\0';
        cout << txData;
    
    
    
        if (serial->isOpen()) {
            serial->write(txData, index);
    
        }
    
        return 0;
    
       
        
    }
    

    If anyone could help it would be much appreciated.

    Have a great day/evening
    PS: im using 6.2.3

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      And do you use threads in your program?

      using namespace std;

      Please don't use 'using namespace FOO' in headers...

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • G Offline
        G Offline
        GabRioux25
        wrote on last edited by
        #3

        I found the issue, it's all fixed, i'll post the solution tomorrow for those in the future who might have the same problem

        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