Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hardware that echoes transmitted bytes #35

Closed
wants to merge 2 commits into from
Closed

Hardware that echoes transmitted bytes #35

wants to merge 2 commits into from

Conversation

jaimealsilva
Copy link

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:

int modbus_rtu_set_echohw_mode(modbus_t *ctx, uint8_t mode);
int modbus_rtu_get_echohw_mode(modbus_t *ctx);

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.

Jaime Alberto Silva added 2 commits October 23, 2011 15:56
…ceiver

is always enabled 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.

Added a new field to the modbus_rtu structure, added a couple functions and
modified the rtu's transmit function. The new functions are:

int modbus_rtu_set_echohw_mode(modbus_t *ctx, uint8_t mode);
int modbus_rtu_get_echohw_mode(modbus_t *ctx);

echohw mode can be one of MODBUS_RTU_NO_ECHOHW or 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.
@stephane
Copy link
Owner

stephane commented Nov 1, 2011

Your patch mustn't change the existing coding conventions (4 spaces).
The change on .gitignore must be reverted.

To update your commits, you can use 'git rebase -i' or create a new branch...

I'm intend to merge your commit 834dfea (with some tweaks) so it's not necessary to update this one.

@stephane
Copy link
Owner

Could you rebase on upstream (see my previous comments), please?

@stephane stephane closed this Mar 12, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants