@@ -122,19 +122,25 @@ comment of [`main.py`](main.py).
122122Act as host, get Modbus data via RTU or TCP from a client device
123123
124124``` python
125+ # import modbus host classes
126+ from umodbus.tcp import TCP as ModbusTCPMaster
127+ from umodbus.serial import Serial as ModbusRTUMaster
128+
125129# RTU Master setup
126130# 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
127132rtu_pins = (25 , 26 ) # (TX, RX)
128133slave_addr = 10 # bus address of client
129134host = ModbusRTUMaster(
130135 baudrate = 9600 , # optional, default 9600
131- data_bits = 8 , # optional, default 7
136+ data_bits = 8 , # optional, default 8
132137 stop_bits = 1 , # optional, default 1
133138 parity = None , # optional, default None
134139 pins = rtu_pins)
135140
136141# TCP Master setup
137142# 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
138144host = ModbusTCPMaster(
139145 slave_ip = 192.168 .178.34,
140146 slave_port = 180 ,
@@ -144,7 +150,7 @@ host = ModbusTCPMaster(
144150coil_address = 123
145151coil_status = host.read_coils(
146152 slave_addr = slave_addr,
147- starting_addr = 123 ,
153+ starting_addr = coil_address ,
148154 coil_qty = 1 )
149155print (' Status of coil {} : {} ' .format(coil_status, coil_address))
150156
@@ -167,7 +173,7 @@ print('Status of hreg {}: {}'.format(hreg_address, register_value))
167173
168174# WRITE HREGS
169175new_hreg_val = 44
170- operation_status = self . host.write_single_register(
176+ operation_status = host.write_single_register(
171177 slave_addr = slave_addr,
172178 register_address = hreg_address,
173179 register_value = new_hreg_val,
@@ -176,15 +182,15 @@ print('Result of setting hreg {} to {}'.format(hreg_address, operation_status))
176182
177183# READ ISTS
178184ist_address = 67
179- input_status = self . host.read_discrete_inputs(
185+ input_status = host.read_discrete_inputs(
180186 slave_addr = slave_addr,
181187 starting_addr = ist_address,
182188 input_qty = 1 )
183189print (' Status of ist {} : {} ' .format(ist_address, input_status))
184190
185191# READ IREGS
186192ireg_address = 10
187- register_value = self . host.read_input_registers(
193+ register_value = host.read_input_registers(
188194 slave_addr = slave_addr,
189195 starting_addr = ireg_address,
190196 register_qty = 2 ,
@@ -194,82 +200,18 @@ print('Status of ireg {}: {}'.format(ireg_address, register_value))
194200
195201### Slave implementation
196202
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.
198204
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 )
211207
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 )
216209
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.
273215
274216### Register configuration
275217
@@ -308,6 +250,7 @@ of this library.
308250[ ref-pycom-modbus ] : https://github.com/pycom/pycom-modbus
309251[ ref-remote-upy-shell ] : https://github.com/dhylands/rshell
310252[ ref-github-be-mircopython-modules ] : https://github.com/brainelectronics/micropython-modules
253+ [ ref-github-be-python-modules ] : https://github.com/brainelectronics/python-modules
311254[ ref-myevse-be ] : https://brainelectronics.de/
312255[ ref-myevse-tindie ] : https://www.tindie.com/stores/brainelectronics/
313256[ ref-giampiero7 ] : https://github.com/giampiero7
0 commit comments