I have a program that opens a serial port using boost asio.
The serial port, by default, has a delay that keeps the line idle. On windows platforms I saw a delay of 30ms and on Linux platforms the delay was 20ms.
For the Linux environment I found that the class 'ioctl' of "linux.h" has a way to set the serial settings with some flags (and what I needed: low_latency).
the code is as follows:
boost::asio::basic_serial_port<boost::asio::serial_port_service>::native_type native = serial_port_.native(); // serial_port_ is the boost's serial port class.
struct serial_struct serial;
ioctl(native, TIOCGSERIAL, &serial);
serial.flags |= ASYNC_LOW_LATENCY; // (0x2000)
ioctl(native, TIOCSSERIAL, &serial);
I want to reduce the delay on my windows platform as well. Is there an equivalent way that does the same for windows with C++?
BTW, I saw that there are some solutions that suggests to change the properties of the serial port at the Windows Device Manager, but I don't have those properties as these solutions showed and I need a code solution.
Take the native handle you get in from boost asio in windows and pass it to SetCommTimeouts: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363437(v=vs.85).aspx
In particular, look at the ReadIntervalTimeout of the COMMTIMEOUT structure: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363190(v=vs.85).aspx
ReadIntervalTimeout The maximum time allowed to elapse between the arrival of two bytes on the communications line, in milliseconds. During a ReadFile operation, the time period begins when the first byte is received. If the interval between the arrival of any two bytes exceeds this amount, the ReadFile operation is completed and any buffered data is returned. A value of zero indicates that interval time-outs are not used. A value of MAXDWORD, combined with zero values for both the ReadTotalTimeoutConstant and ReadTotalTimeoutMultiplier members, specifies that the read operation is to return immediately with the bytes that have already been received, even if no bytes have been received.
You can also query the current values with GetCommTimeouts: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363261(v=vs.85).aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With