PDA

View Full Version : Communication via SerialPort



porterneon
1st August 2011, 15:14
Hi.
I need to write application that will communicate with electronic device via serial port. I was thinking to use QExtSerialPort library but I have some doubts.
In QExtSerialPort library port is being read after signal readyRead(). In my application computer will ask electronic device about his status in loop. And then base of status send to device next commands. I need to have something like this:

while(keepAsking)
{
QByteArray status = serialPort->checkStatus();
if(status == something[]) //<- this is only example i know it's wrong :)
{
serialPort->DoA();
}
if(status == something2[])
{
serialPort->DoB();
}
}

What is best approach for this logic? I thing QExtSerialPort with read port on signal will not work in this situation. How would you solve this issue?

marcvanriet
2nd August 2011, 23:34
Hi,
Instead of a loop, you should use a timer to get the device status every xxx milliseconds.

To ask the device about its status, you probably need to send it a command like "give me your status". This command can be sent in the code activated by the timer (let's call this the timer event).

The device will send a reply. You could use the ReadyRead() event to process the data as it is received.

Another way is to check in the timer event if data is already sent by the device. The code in the timer event must never wait until data is received. It should only check if data is available, and if so process the data. If no data or unsufficient data is available, it should just exit. Typically you use some sort of "state machine" code for the timer event.

Hope this helps,
Regards,
Marc