Hardware that echoes transmitted bytes #35
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
OK, now I'm doing it right. Instead of opening an issue I'm sending you a pull request. So here goes the explanation again:
I'm using libmodbus on a Technologic Systems TS-7800 SBC with the RS485 optional bus. It turns out that this board's MAX485 is wired in such a way that the receiver is always enabled (check out page 6 of it's schematic bottom-right corner) so everything you transmit gets back on the receive buffer, libmodbus thinks it is the slave answer and fails with a checksum error.
I tried to fix it by disabling the receiver before transmitting with tcsetatrr and turning it back on after transmission but it didn't worked, the bytes were still echoed to the receive buffer. So finally I fixed it by reading from the port the same amount of bytes transmitted so they get out of the buffer.
I think this can be useful to someone else who has hardware that echoes the transmitted bytes. So I add a new field to the modbus_rtu structure and, added a couple functions and modified the rtu's transmit function. The new functions are:
echohw mode can be one of MODBUS_RTU_NO_ECHOHW and MODBUS_RTU_HAS_ECHOHW. By default the RTU is in MODBUS_RTU_NO_ECHOHW which means it works as usual, but if you change it to MODBUS_RTU_HAS_ECHOHW then all the transmitted bytes will be taken out of the receive buffer.
I also fixed some issues with the new RTS functions that break compilation when HAVE_DECL_TIOCSRS485 is not defined.
I hope this code is useful and will be waiting for comments.
Thanks again for libmodbus.