Issue using QSerialPort
-
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 -
And do you use threads in your program?
using namespace std;
Please don't use 'using namespace FOO' in headers...
-
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