Skip to content

Commit

Permalink
switched to modbus_new_tcp_pi
Browse files Browse the repository at this point in the history
  • Loading branch information
v-zhuravlev committed Sep 24, 2018
1 parent 982506e commit f45fd7c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 7 additions & 3 deletions src/modbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/test_00modbus_connection_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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):
Expand Down

0 comments on commit f45fd7c

Please sign in to comment.