From f45fd7c2ff18bde8adc0070841b88b63355c2910 Mon Sep 17 00:00:00 2001 From: v-zhuravlev Date: Sun, 23 Sep 2018 22:09:01 +0300 Subject: [PATCH] switched to modbus_new_tcp_pi --- README.md | 4 +--- src/modbus.c | 10 +++++++--- tests/test_00modbus_connection_string.py | 12 ++++++++++++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index adc3e77..8f57b36 100644 --- a/README.md +++ b/README.md @@ -56,9 +56,7 @@ where: for Modbus Encapsulated (RTU over TCP): IPv4 of Modbus gate, for example: `enc://192.168.1.1` - TCP port may also be redefined (from Modbus default 502) if needed: `enc://192.168.1.1:5000` - - Note: DNS names are not supported for TCP and RTU over TCP + TCP port may also be redefined (from Modbus default 502) if needed: `enc://192.168.1.1:5000` for Modbus RTU over serial: Serial connection parameters in a form of: diff --git a/src/modbus.c b/src/modbus.c index ffb2ef6..994f9b3 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -449,30 +449,34 @@ void create_modbus_context(char *con_string, modbus_t **ctx_out, int *lock_requi char host[100]; int port = MODBUS_TCP_DEFAULT_PORT; + char port_str[10]; if (strncmp(con_string, "enc://", strlen("enc://")) == 0) { *lock_required_out = 1; con_string += strlen("enc://"); sscanf(con_string, "%99[^:]:%99d[^\n]", host, &port); + sprintf(port_str, "%d", port); *lock_key = hash(host) % NSEMS; - *ctx_out = modbus_new_rtutcp(host, port); + *ctx_out = modbus_new_rtutcp_pi(host, port_str); } else if (strncmp(con_string, "tcp://", strlen("tcp://")) == 0) { *lock_required_out = 0; con_string += strlen("tcp://"); sscanf(con_string, "%99[^:]:%99d[^\n]", host, &port); + sprintf(port_str, "%d", port); *lock_key = hash(host) % NSEMS; - *ctx_out = modbus_new_tcp(host, port); + *ctx_out = modbus_new_tcp_pi(host, port_str); } else { // try Modbus TCP *lock_required_out = 0; sscanf(con_string, "%99[^:]:%99d[^\n]", host, &port); + sprintf(port_str, "%d", port); *lock_key = hash(host) % NSEMS; - *ctx_out = modbus_new_tcp(host, port); + *ctx_out = modbus_new_tcp_pi(host, port_str); } } diff --git a/tests/test_00modbus_connection_string.py b/tests/test_00modbus_connection_string.py index b42110f..e9ca1c4 100644 --- a/tests/test_00modbus_connection_string.py +++ b/tests/test_00modbus_connection_string.py @@ -6,6 +6,8 @@ class TestModbusConnectionString(object): host = "172.16.238.2:5020" host_rtutcp = "172.16.238.2:5021" + host_dns = "modbus-server:5020" + host_rtutcp_dns = "modbus-server:5021" # test enc:// (rtu over tcp) @@ -18,6 +20,16 @@ def test_modbus_tcp(self): key = "modbus_read[tcp://"+self.host+",1,1,3,uint16]" assert zabbix_get(key) == '49677' + # test enc:// (rtu over tcp) using hostname + def test_modbus_test_rtutcp_dns(self): + key = "modbus_read[enc://"+self.host_rtutcp_dns+",1,0,3,uint16]" + assert zabbix_get(key) == '49807' + + # test tcp:// (plain tcp) using hostname + def test_modbus_tcp_dns(self): + key = "modbus_read[tcp://"+self.host_dns+",1,1,3,uint16]" + assert zabbix_get(key) == '49677' + # test serial /dev/ttyS0 @pytest.mark.skip("implement this first") def test_modbus_serial(self, host):