QextSerialPort - ReadWrite mode trouble
I need some help to be able to properly communicate with a device by qextserialport.
It operates at 9600bps with no flow control.
In ReadOnly mode I can manage bytes coming from the device since the device sends some bytes to allow me to "sync" and so I can properly read the following bytes...
Problems arise when the device is not operating as master (when I need so send a command to read the answer that follows ReadWrite mode is required).
I need to send one or two bytes and then the device should answer by sending one or two bytes.
It should be simple but I often see data lost.
By using @while (port->bytesAvailable()<1)
usleep(5000);@ before port->readRawData call nothing change.
I also added port->flush() or port->readAll() before port->write(...) but I didn't solve the trouble.
The code that manages the serial port is on a thread different from the main one. I didn't subclass the QThread class but moved my class that manages the serial device to a different thread. I think it should work...
Should I use buffered or unbuffered mode? And what about timeout (port settings)?
What do you suggest?
I am using the same serial port for reading/writing in an application. The application uses QextSerialPort and opens it with QIODevice::ReadWrite .
There is also another class QSerialDevice available. "Here":http://developer.qt.nokia.com/forums/viewthread/11634/#67246 you find some details on it.
Before evaluating to prefer QSerialDevice, I'd like to be sure I'm properly using QextSerialPort.
I obviously use ReadWrite mode to be able to read&write.
How would you write two bytes and then read the answer (2 bytes too)?
The reading and writing is done in different threads. When bytes are reeived a readyRead() signal is issued.
When I started to use QextSerialPort I have found a derivative showing that approach. I have used this for guiding my implementation.
At that time I have found source obviously having the same origin, but following different strategies. I did not find the SourceForge version (now in Google) at that time. Unfortunately, I cannot change easily.
When the pain is large enough and I need a serial device implementation I probably will have a very close look to QSerialDevice.
But are you referring to QextSerialPort::EventDriven mode?
I think it's not mandatory, is it?
That is the way I am using it. I have never tested the other mode.
QextSerialPort::EventDriven mode didn't solve the troubles.
So I tried to manage the RS232 by QSerialDevice and nothing changed.
Then I found that by writing one byte at time (by QSerialDevice - I had no time to try by QExtSerialPort) in unbuffered mode (in place of writing a QByteArray in buffered or unbuffered mode) everything worked as expected (without usleep calls between consecutive byte writings too).
So something is still unclear.
shorawitz last edited by
I don't know if you have the ability to add a message delimiter, I had to do that with my own protocol. Then all I did was loop:
readBytes = serialport->read(serialport->bytesAvailable());
and append readBytes to a QString until I reach my message delimiter. At the moment, I'm testing my code with a virtual serial port connecting 2 x VirtualBox's using the following for these serial port settings:
serialPort = new QextSerialPort(serialDevice, QextSerialPort::EventDriven);