Duncan
unread,Aug 24, 2020, 4:16:21 PM8/24/20Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to libm...@googlegroups.com
Good day, I do not think this is a bug error or problem as such, as I'm
quite new to modbus and libmodbus. I was hoping maybe someone can assist
me, cant seem to get this right.
So I have tried two diffrent slave devices set to
Baud: 19200
Bits:8
Stop bits: 2
Parity: N
Address I tried as both 1 then 2. I also change around on both the C as
well as the slave side of the program between different Bits, Parities
as well as stop bits. Didn't seem to make a difference.
Setup:
Triend both Raspberry pi 3B with Rasbpian (Debian 10) version and
running on VM ware. both showed same problem. I tried 3 different
USB-to-RS485 converters. The most successful one was the one in the
picture I posted.
I receive between 2 errors:
CRC error. Citing received data and calculated date differs.
Sometimes I strike luck and then I communicate with the device it seems,
but then it cites an error where "Responding device not same as
requested device". This is always pointing to the same "wrong
addresses". I'm calling on 1 or 2 then I receive responses from slave ID
15, 63, 127 etc. Not random numbers, always from the same group. I
suspected an decimal/binary mismatch, so I tried that, the response
didn't change, thus I believe the libmodbus libraries was designed to
work with both value sets?
This is all done in "testing"facilities, so controlled environment,
short cable, tried 2 wire as well as 3 wire setup. I tried 120ohm on the
slave connection as well. Didn't make a difference. Switching wires
didn't change the response. I believe the dip switches was set
correctly, only invoking RX and TX on the 485 channels.
I am not to flaunt in C (yet) but will paste my code underneath. Any
advice would be greatly appreciated.
-----------------------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <modbus.h>
int main()
{
int a;
uint16_t test;
modbus_t *mb;
// Create new modbus context: no parity, 8 start bits and 1 stop bit
mb = modbus_new_rtu("/dev/ttyUSB0",19200,'N',8,2);
modbus_set_debug(mb,TRUE);
modbus_rtu_set_serial_mode(mb, MODBUS_RTU_RS485);
modbus_set_error_recovery(mb, MODBUS_ERROR_RECOVERY_LINK |
MODBUS_ERROR_RECOVERY_PROTOCOL);
modbus_flush(mb);
if (mb == NULL)
{
fprintf(stderr, "Unable to create the libmodbus context for
RTU: %s\n", modbus_strerror(errno));
return -1;
}
if(modbus_set_slave(mb, 1) == -1)
{
fprintf(stderr, "Unable to set the slave ID in context for
RTU: %s\n", modbus_strerror(errno));
return -1;
}
if (modbus_connect(mb) == -1)
{
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
modbus_free(mb);
return -1;
}
struct timeval response;
response.tv_sec = 0;
response.tv_usec = 100000;
modbus_set_response_timeout(mb, 0, 100000);
a = modbus_read_input_registers(mb,1,1,&test);
printf("\n Value of a is: %d\n",a);
if (a==-1)
fprintf(stderr, "Custom error: %s\n", modbus_strerror(errno));
}
-----------------------------------------------------------------------------------------------------------------------------------------------------
Thanks in advance for your time.
Yours sincerely.
Duncan