Unable to receive data from QserialPort
-
Hi everyone.
My application is to control a humidity sensor embedded on a board.
For this, I use a microcontroler STM32F103.I want to send information to IHM which I have already create, through UART communication and a RS232-USB adapter.
For example, i want to transfer 3 characters (3 bytes of data) :- 'W' --> 0x57
- 'e' --> 0x65
- 'l' --> 0x6c
UART communication transfers LSB first, so for 'W' i transfer this byte : '1110 1010' (0xEA).
Nevertheless, data received on my IHM are wrong : i receive 'T' (0x54) .
For communication, I use the QSerialPort library, and a thread which wakes up when bytes are available on USB port.
Here is my code :
QSerialPort tab_liaison[10]; QByteArray test;int FTDI::read2_message(int x) { test=tab_liaison[x].read(10); ui->textBrowser_port1->append(test); return 0; }void FTDI::waitAnswer() { //Reading Thread while(1) { QCoreApplication::processEvents(); for (int i=1;i<=compt;i++) { if(tab_liaison[i].bytesAvailable()!=0) { this->read2_message(i); } } break; QThread::yieldCurrentThread(); // gele le thread } }void FTDI::receiveThread() { //Reading Thread while(1) { waitAnswer(); } }The function read_message isn't good and I try to modify the reading of bytes but i don't understand my mistakes.
Please help me, and sorry for the bad English !!
Regards,
Sel
-
Hi and welcome to devnet,
Your architecture seems a bit convoluted for what you want to do. One thing that strange is that break in waitAnswer, you then only execute that infinite loop once. You also have an infinite loop calling a function containing an infinite loop.
In any case, why not make use of the asynchronous mode of QSerialPort ?
-
I just begin to use Qt Framework ( i'm an electronician) so i don't know how to to use QThread.
I used a code that I found on web, but it is true that there is 2 infinite loo^p.
For you, what is the best solution ? Delete the (while) loop in function "void FTDI::waitAnswer()" and the break ? Do you think the thread will work normally ?I will try to use the asynchronous mode of QSerialPort although I don't know what is it, but I will post on the subject if I stay blocked.
-
If you are starting from scratch then first thing: don't try to write a threaded program. Take a look at the Terminal example from QSerialPort documentation, it should help you get started.