@@ -122,19 +122,25 @@ comment of [`main.py`](main.py).
122
122
Act as host, get Modbus data via RTU or TCP from a client device
123
123
124
124
``` python
125
+ # import modbus host classes
126
+ from umodbus.tcp import TCP as ModbusTCPMaster
127
+ from umodbus.serial import Serial as ModbusRTUMaster
128
+
125
129
# RTU Master setup
126
130
# act as host, get Modbus data via RTU from a client device
131
+ # ModbusRTUMaster can make serial requests to a client device to get/set data
127
132
rtu_pins = (25 , 26 ) # (TX, RX)
128
133
slave_addr = 10 # bus address of client
129
134
host = ModbusRTUMaster(
130
135
baudrate = 9600 , # optional, default 9600
131
- data_bits = 8 , # optional, default 7
136
+ data_bits = 8 , # optional, default 8
132
137
stop_bits = 1 , # optional, default 1
133
138
parity = None , # optional, default None
134
139
pins = rtu_pins)
135
140
136
141
# TCP Master setup
137
142
# act as host, get Modbus data via TCP from a client device
143
+ # ModbusTCPMaster can make TCP requests to a client device to get/set data
138
144
host = ModbusTCPMaster(
139
145
slave_ip = 192.168 .178.34,
140
146
slave_port = 180 ,
@@ -144,7 +150,7 @@ host = ModbusTCPMaster(
144
150
coil_address = 123
145
151
coil_status = host.read_coils(
146
152
slave_addr = slave_addr,
147
- starting_addr = 123 ,
153
+ starting_addr = coil_address ,
148
154
coil_qty = 1 )
149
155
print (' Status of coil {} : {} ' .format(coil_status, coil_address))
150
156
@@ -167,7 +173,7 @@ print('Status of hreg {}: {}'.format(hreg_address, register_value))
167
173
168
174
# WRITE HREGS
169
175
new_hreg_val = 44
170
- operation_status = self . host.write_single_register(
176
+ operation_status = host.write_single_register(
171
177
slave_addr = slave_addr,
172
178
register_address = hreg_address,
173
179
register_value = new_hreg_val,
@@ -176,15 +182,15 @@ print('Result of setting hreg {} to {}'.format(hreg_address, operation_status))
176
182
177
183
# READ ISTS
178
184
ist_address = 67
179
- input_status = self . host.read_discrete_inputs(
185
+ input_status = host.read_discrete_inputs(
180
186
slave_addr = slave_addr,
181
187
starting_addr = ist_address,
182
188
input_qty = 1 )
183
189
print (' Status of ist {} : {} ' .format(ist_address, input_status))
184
190
185
191
# READ IREGS
186
192
ireg_address = 10
187
- register_value = self . host.read_input_registers(
193
+ register_value = host.read_input_registers(
188
194
slave_addr = slave_addr,
189
195
starting_addr = ireg_address,
190
196
register_qty = 2 ,
@@ -194,82 +200,18 @@ print('Status of ireg {}: {}'.format(ireg_address, register_value))
194
200
195
201
### Slave implementation
196
202
197
- Act as client, provide Modbus data via RTU or TCP to a host device
203
+ Act as client, provide Modbus data via RTU or TCP to a host device.
198
204
199
- ``` python
200
- # RTU Slave setup
201
- # act as client, provide Modbus data via RTU to a host device
202
- rtu_pins = (25 , 26 ) # (TX, RX)
203
- slave_addr = 10 # address on bus as client
204
- client = ModbusRTU(
205
- addr = slave_addr, # address on bus
206
- baudrate = 9600 , # optional, default 9600
207
- data_bits = 8 , # optional, default 7
208
- stop_bits = stop_bits, # optional, default 1
209
- parity = parity, # optional, default None
210
- pins = rtu_pins)
205
+ See [ Modbus TCP Client example] ( examples/tcp_client_example.py ) and
206
+ [ Modbus RTU Client example] ( examples/rtu_client_example.py )
211
207
212
- # TCP Slave setup
213
- # act as client, provide Modbus data via TCP to a host device
214
- local_ip = ' 192.168.4.1' # IP address
215
- tcp_port = 502 # port to listen to
208
+ Both examples are using [ example register definitions] ( examples/example.json )
216
209
217
- """
218
- # to get from MicroPython core functions use this
219
- import network
220
- station = network.WLAN(network.STA_IF)
221
- if station.active():
222
- if station.isconnected():
223
- local_ip = station.ifconfig()[0]
224
- """
225
-
226
- client = ModbusTCP()
227
- is_bound = False
228
-
229
- # check whether client has been bound to an IP and port
230
- is_bound = client.get_bound_status()
231
-
232
- if not is_bound:
233
- client.bind(local_ip = local_ip, local_port = tcp_port)
234
-
235
- # commond slave register setup, to be used with the Master example above
236
- register_definitions = {
237
- " COILS" : {
238
- " EXAMPLE_COIL" : {
239
- " register" : 123 ,
240
- " len" : 1 ,
241
- }
242
- },
243
- " HREGS" : {
244
- " EXAMPLE_HREG" : {
245
- " register" : 93 ,
246
- " len" : 1 ,
247
- }
248
- },
249
- " ISTS" : {
250
- " EXAMPLE_ISTS" : {
251
- " register" : 67 ,
252
- " len" : 1 ,
253
- }
254
- },
255
- " IREGS" : {
256
- " EXAMPLE_IREG" : {
257
- " register" : 10 ,
258
- " len" : 2 ,
259
- }
260
- }
261
- }
262
-
263
- """
264
- # alternatively the register definitions can also be loaded from a JSON file
265
- import json
266
-
267
- with open('registers/modbusRegisters-MyEVSE.json', 'r') as file:
268
- register_definitions = json.load(file)
269
- """
270
-
271
- client.setup_registers(registers = register_definitions, use_default_vals = True )
272
- ```
210
+ Use the provided example scripts [ read RTU] ( examples/read_registers_rtu.sh ) or
211
+ [ read TCP] ( examples/read_registers_tcp.sh ) to read the data from the devices.
212
+ This requires the [ modules submodule] [ ref-github-be-python-modules ] to be
213
+ cloned as well and the required packages being installed as described in the
214
+ modules README file.
273
215
274
216
### Register configuration
275
217
@@ -308,6 +250,7 @@ of this library.
308
250
[ ref-pycom-modbus ] : https://github.com/pycom/pycom-modbus
309
251
[ ref-remote-upy-shell ] : https://github.com/dhylands/rshell
310
252
[ ref-github-be-mircopython-modules ] : https://github.com/brainelectronics/micropython-modules
253
+ [ ref-github-be-python-modules ] : https://github.com/brainelectronics/python-modules
311
254
[ ref-myevse-be ] : https://brainelectronics.de/
312
255
[ ref-myevse-tindie ] : https://www.tindie.com/stores/brainelectronics/
313
256
[ ref-giampiero7 ] : https://github.com/giampiero7
0 commit comments