libmodbus icon indicating copy to clipboard operation
libmodbus copied to clipboard

tcdrain to wait until all bytes have been sent

Open stephane opened this issue 10 years ago • 1 comments

From @geith:

https://github.com/stephane/libmodbus/commit/cf6449145a13f53aef969e89599be86f888539f3#commitcomment-5637856

Instead of calculating the transmission time manually, tcdrain() can be used to wait
until all bytes have been sent (which would be a safer an more generic method in my
opinion). After that, the usleep() would be needed for the 
MODBUS_RTU_TIME_BETWEEN_RTS_SWITCH only.

stephane avatar Mar 17 '14 08:03 stephane

Hello @stephane

Looking at the libmodbus features, I just read this issue and I see there was a PR proposed.

I just want to highlight that the tcdrain function can take a very long time to return even if on the serial bus the data have been sent. I have done that with a logic analyzer and measured about 25ms. This is very long when the messages are short and are sent at 115200 for example.

This was another project - not some modbus stuff - and maybe the serial port configuration was different. However I think this is a topic on which some specific attention should be taken to avoid issues later. The current implementation you have with some delays is much reactive and return quickly and it's doing the job in my opinion.

For this other project I was just speaking above I have also tried successfully to always enable the reception of data (this requires a different hardware with RE pin always low on the transceiver so not suitable for every people) and I clear the RTS output in the application when the data I have sent have been received back (+ a timeout just in case of error). This is also very reactive and usually RTS goes low about 40-50µs after the end of the transmission of data @ 115200 baud.

Is your library able to manage the echo for this kind of hardware with reception always active ?

Regards, Joel

joelguittet avatar Jul 16 '21 19:07 joelguittet