forked from pycom/pycom-modbus
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.py
105 lines (84 loc) · 3.61 KB
/
main.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
#
# Copyright (c) 2019, Pycom Limited.
#
# This software is licensed under the GNU GPL version 3 or any
# later version, with permitted additional terms. For more information
# see the Pycom Licence v1.0 document supplied with this file, or
# available at https://www.pycom.io/opensource/licensing
#
from uModBus.serial import Serial
from uModBus.tcp import TCP
from network import WLAN
import machine
####################### TCP MODBUS #########################
WIFI_SSID = 'your ssid'
WIFI_PASS = 'your password'
wlan = WLAN(mode=WLAN.STA)
wlan.connect(WIFI_SSID, auth=(None, WIFI_PASS), timeout=5000)
while not wlan.isconnected():
machine.idle() # save power while waiting
print('WLAN connection succeeded!')
slave_ip = 'slave ip'
modbus_obj = TCP(slave_ip)
######################### RTU SERIAL MODBUS #########################
#uart_id = 0x01
#modbus_obj = Serial(uart_id, pins=('P9', 'P10'))
# if the serial modbus requires a ctrl pin, you can set it like this:
#modbus_obj = Serial(uart_id, pins=('P9','P10'), ctrl_pin='P8')
######################### READ COILS #########################
#slave_addr=0x0A
#starting_address=0x00
#coil_quantity=100
#coil_status = modbus_obj.read_coils(slave_addr, starting_address, coil_quantity)
#print('Coil status: ' + ' '.join('{:d}'.format(x) for x in coil_status))
###################### READ DISCRETE INPUTS ##################
#slave_addr=0x0A
#starting_address=0x0
#input_quantity=100
#input_status = modbus_obj.read_discrete_inputs(slave_addr, starting_address, input_quantity)
#print('Input status: ' + ' '.join('{:d}'.format(x) for x in input_status))
###################### READ HOLDING REGISTERS ##################
#slave_addr=0x0A
#starting_address=0x00
#register_quantity=100
#signed=True
#register_value = modbus_obj.read_holding_registers(slave_addr, starting_address, register_quantity, signed)
#print('Holding register value: ' + ' '.join('{:d}'.format(x) for x in register_value))
###################### READ INPUT REGISTERS ##################
#slave_addr=0x0A
#starting_address=0x00
#register_quantity=100
#signed=True
#register_value = modbus_obj.read_input_registers(slave_addr, starting_address, register_quantity, signed)
#print('Input register value: ' + ' '.join('{:d}'.format(x) for x in register_value))
###################### WRITE SINGLE COIL ##################
#slave_addr=0x0A
#output_address=0x00
#output_value=0xFF00
#return_flag = modbus_obj.write_single_coil(slave_addr, output_address, output_value)
#output_flag = 'Success' if return_flag else 'Failure'
#print('Writing single coil status: ' + output_flag)
###################### WRITE SINGLE REGISTER ##################
#slave_addr=0x0A
#register_address=0x01
#register_value=-32768
#signed=True
#return_flag = modbus_obj.write_single_register(slave_addr, register_address, register_value, signed)
#output_flag = 'Success' if return_flag else 'Failure'
#print('Writing single coil status: ' + output_flag)
###################### WRITE MULIPLE COILS ##################
#slave_addr=0x0A
#starting_address=0x00
#output_values=[1,1,1,0,0,1,1,1,0,0,1,1,1]
#return_flag = modbus_obj.write_multiple_coils(slave_addr, starting_address, output_values)
#output_flag = 'Success' if return_flag else 'Failure'
#print('Writing multiple coil status: ' + output_flag)
###################### WRITE MULIPLE REGISTERS ##################
slave_addr=0x0A
register_address=0x01
register_values=[2, -4, 6, -256, 1024]
signed=True
return_flag = modbus_obj.write_multiple_registers(slave_addr, register_address, register_values, signed)
output_flag = 'Success' if return_flag else 'Failure'
print('Writing multiple register status: ' + output_flag)