-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient-tcp-modbus.py
More file actions
88 lines (82 loc) · 3.92 KB
/
client-tcp-modbus.py
File metadata and controls
88 lines (82 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
'''
Pymodbus Synchronous Client
#---------------------------------------------------------------------------#
# import the various server implementations
#---------------------------------------------------------------------------#
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
#---------------------------------------------------------------------------#
# configure the client logging
#---------------------------------------------------------------------------#
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
#---------------------------------------------------------------------------#
# choose the client you want
#---------------------------------------------------------------------------#
# make sure to start an implementation to hit against. For this
# you can use an existing device, the reference implementation in the tools
# directory, or start a pymodbus server.
#
# If you use the UDP or TCP clients, you can override the framer being used
# to use a custom implementation (say RTU over TCP). By default they use the
# socket framer::
#
# client = ModbusClient('localhost', port=5020, framer=ModbusRtuFramer)
#
# It should be noted that you can supply an ipv4 or an ipv6 host address for
# both the UDP and TCP clients.
#
# There are also other options that can be set on the client that controls
# how transactions are performed. The current ones are:
#
# * retries - Specify how many retries to allow per transaction (default = 3)
# * retry_on_empty - Is an empty response a retry (default = False)
# * source_address - Specifies the TCP source address to bind to
#
# Here is an example of using these options::
#
# client = ModbusClient('localhost', retries=3, retry_on_empty=True)
#---------------------------------------------------------------------------#
client = ModbusClient('129.194.184.124', port=1502)
client.connect()
#---------------------------------------------------------------------------#
# specify slave to query
#---------------------------------------------------------------------------#
# The slave to query is specified in an optional parameter for each
# individual request. This can be done by specifying the `unit` parameter
# which defaults to `0x00`
# Room 505: unit=0x05
# Room 507: unit=0x07
# Room 533: unit=0x33
# Room 534: unit=0x34
#
# REGISTERS FOR SAIA
# http://www.meterbuy.com/fileadmin/user_upload/Data_sheets/120102_SAIA_-_Data_sheet_26-527_EN_DS_Energy-Meter-ALE3-with-Modbus.pdf
# Page 6 -> register_requested = register-1
#---------------------------------------------------------------------------#
unit=0x05
count=1
register_requested=36-1
rr = client.read_holding_registers(register_requested, count, unit)
#---------------------------------------------------------------------------#
# example requests
#---------------------------------------------------------------------------#
# simply call the methods that you would like to use. An example session
# is displayed below along with some assert checks. Note that some modbus
# implementations differentiate holding/input discrete/coils and as such
# you will not be able to write to these, therefore the starting values
# are not known to these tests. Furthermore, some use the same memory
# blocks for the two sets, so a change to one is a change to the other.
# Keep both of these cases in mind when testing as the following will
# _only_ pass with the supplied async modbus server (script supplied).
#---------------------------------------------------------------------------#
# rq = client.write_register(1, 10)
# rr = client.read_holding_registers(1,1)
# assert(rq.function_code < 0x80) # test that we are not an error
# assert(rr.registers[0] == 10) # test the expected value
#---------------------------------------------------------------------------#
# close the client
#---------------------------------------------------------------------------#
client.close()