QSerialPort timeout between two telegrams
-
Hello there,
I am using Qt 5.4.1 for Windows to create a programm, that sniffes the communication between two modbus devices.
The end of each modbus message is defined by a pause of 3.5 characters.
How can I determine this "timeout" using QSerialPort? Or should I use a different API?
I like QSerialPort, it is very simple.In conclusion, I want to know how can I read only one message a time, which starts or ends with a pause of a defined time, and not the hole buffer with readall().
The message size can be different each time.I have a RS485-USB-converter with an FTDI to sniff the traffic.
I have already test "libmodbus", but there is almost the first byte wrong, every second time, there is a "wrong" byte at the start of the message.Thank you all & and sorry for my english, greetings from germany
George -
How can I determine this "timeout" using QSerialPort?
It is impossible in commom case. But as workaround, you can try to use QSerialPort::handle() to change COMMTIMEOUTS on Windows, ot VMIN/VTIME on Linux/MacOSX, but this will cause freezes, since it is blocking.
Or should I use a different API?
There are no different API.
In conclusion, I want to know how can I read only one message a time, which starts or ends with a pause of a defined time, and not the hole buffer with readall().
You can use QSerialPort::bytesAvailable(), QSerialPort::read(), QSerialPort::peek().
-
Thank you,
I think ReadIntervalTimeout is what I am searching for.
How can I use this with QSerialPort::handle()?
I cannot find any example for QSerialPort::handle(), or what it is good for.Can you give me a small example how to change COMMTIMEOUTS with QSerialPort::handle()?
-
Can you give me a small example how to change COMMTIMEOUTS with QSerialPort::handle()?
See: https://msdn.microsoft.com/ru-ru/library/windows/desktop/aa363437(v=vs.85).aspx
For example:
QSerialPort port("COM10");
port.setBaudRate(115200);
// and other settings, if needif (port.open(QIODevice::ReadWrite) {
COMMTIMEOUTS timeouts;
GetCommTimeouts(port.handle(), &timeouts);
timeouts.ReadIntervalTimeout = 100500;
SetCommTimeouts(port.handle(), &timeouts)) ;
} -
It works well! Thank you! My problem is solved :)