Closed jaimealsilva closed 12 years ago
OK, now I've done it the right way: sent a pull request
Has this been merged? having a similar issue with local echo..
In case it helps anyone else: Unless/until libmodbus deals with local echo directly, it's relatively easy to handle it yourself. For example, replace
modbus_reply(context, query, rc, &mapping);
with
int reply_length= modbus_reply(ctx, query, rc, &mapping);
int serial_fd = modbus_get_socket(ctx);
char buffer[reply_length];
int remaining = reply_length;
while (remaining) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(serial_fd, &fds);
int result = select(serial_fd + 1, &fds, NULL, NULL, NULL);
if (result < 0) {
fprintf(stderr, "Read and discard select failed with error %i\n", errno);
return;
}
result = read(serial_fd, buffer, remaining);
if (result < 0) {
fprintf(stderr, "Read and discard failed with error %i\n", errno);
return;
} else if (result == 0) {
fprintf(stderr, "Read and discard failed: 0 bytes read\n");
return;
}
remaining -= result;
}
(This uses select
- which is probably overkill, but it allows for plenty of flexibility if you want to change it - and is based on code I'm using with libmodbus 3.0.5, as shipped with Ubuntu 14.04.)
Hello there.
First I want to thank Stephane for this work. Kudos man! This is a great library that makes working with Modbus so easy ! and I can get it on my Debian machines by only using apt-get! Thanks a lot! I know is not easy to find the time and energy to organize and share your work.
Now to the subject: 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.
Please excuse me for sending the patch this way but I'm not familiar with how patches should be send on Github. Maybe I should fork or something but I'm on a hurry and I want to share this before I forget. So after this paragraph comes the patch against the src directory of the current stable version (3..0.1). And once again, thanks for the great work.