Коллеги, в продолжение темы.
Мучаюсь с libmodbus.org (3.0.1)
Программа:
Код:
#include <modbus/modbus.h>
#include <stdio.h>
#include <errno.h>
int main () {
modbus_t *st;
uint16_t tab_reg[32];
int rc;
int i;
struct timeval old_response_timeout;
struct timeval response_timeout;
st = modbus_new_tcp("10.10.10.6", 502);
if (st == 0) {
fprintf(stderr, "Error\n");
return -1;
}
if (modbus_connect(st) == -1) {
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
modbus_free(st);
return -1;
}
//modbus_get_response_timeout(st, &old_response_timeout);
//response_timeout.tv_sec = 10;
//response_timeout.tv_usec = 0;
//modbus_set_response_timeout(st, &response_timeout);
/* БД, адрес, кол-во регистров, куда пишем */
rc = modbus_read_registers(st, 0, 10, tab_reg);
if (rc == -1) {
printf("rc=%d\n", rc);
fprintf(stderr, "%s\n", modbus_strerror(errno));
// return -1;
}
for (i = 0 ; i < 10 ; i++) {
printf("reg[%d]=%d (0x%X)\n", i, tab_reg[i], tab_reg[i]);
}
modbus_close(st);
modbus_free(st);
}
Соответственно: gcc test.c `pkg-config --libs --cflags libmodbus`
Соединяется нормально, при опросе регистров возвращает -1:
Код:
:./a.out
rc=-1
Connection timed out
reg[0]=0 (0x0)
reg[1]=0 (0x0)
reg[2]=0 (0x0)
reg[3]=0 (0x0)
reg[4]=0 (0x0)
reg[5]=0 (0x0)
reg[6]=0 (0x0)
reg[7]=0 (0x0)
reg[8]=59656 (0xE908)
reg[9]=64905 (0xFD89)
А должен быть (modpoll -r 1 -c 4 -t 3:hex -m tcp 10.10.10.6):
Код:
modpoll 3.1 - FieldTalk(tm) Modbus(R) Master Simulator
Copyright (c) 2002-2011 proconX Pty Ltd
Visit http://www.modbusdriver.com for Modbus libraries and tools.
Protocol configuration: MODBUS/TCP
Slave configuration...: address = 1, start reference = 1, count = 4
Communication.........: 10.10.10.6, port 502, t/o 1.00 s, poll rate 1000 ms
Data type.............: 16-bit register (hex), input register table
-- Polling slave... (Ctrl-C to stop)
[1]: 0xCCCD
[2]: 0x4221
[3]: 0x0014
[4]: 0x0001
Может кто подскажет, что не так?