1
- """Module for Modbus Communication with PLC"""
1
+ """Module for Modbus Communication with PLC. """
2
2
3
3
from pymodbus .client .sync import ModbusTcpClient
4
4
from pyModbusTCP .server import DataBank , DeviceIdentification
5
5
6
+
6
7
def prepare_server ():
7
- """Prepare server properties"""
8
+ """Prepare server properties. """
8
9
identity = DeviceIdentification (
9
10
vendor_name = b"Lab. Schneider DTETI UGM" ,
10
11
product_code = b"MCD" ,
11
12
major_minor_revision = b"1.0" ,
12
13
vendor_url = b"https://github.com/javanese-programmer/conveyor-object-detection" ,
13
14
product_name = b"McDetector" ,
14
15
model_name = b"McDetector-1" ,
15
- user_application_name = b"Conveyor Object Detection"
16
+ user_application_name = b"Conveyor Object Detection" ,
16
17
)
17
18
databank = DataBank ()
18
19
return databank , identity
19
20
21
+
20
22
def server_set_di (databank : DataBank , bit_value : str ):
21
- """Set a value to Discretes Input of a server
23
+ """Set values to Discrete Inputs of a server.
24
+
22
25
Args:
23
26
databank: a DataBank object that store data
24
27
bit_value: binary value in string format
@@ -28,10 +31,11 @@ def server_set_di(databank: DataBank, bit_value: str):
28
31
for addr , bit in enumerate (bit_value ):
29
32
databank .set_discrete_inputs (addr , [int (bit )])
30
33
print ("Discretes Inputs updated!" )
31
-
34
+
32
35
33
36
def server_set_ir (databank : DataBank , values : tuple ):
34
- """Set a value to Input Registers of a server
37
+ """Set values to Input Registers of a server.
38
+
35
39
Args:
36
40
databank: a DataBank object that store data
37
41
values: tuple of values to be written to register
@@ -43,49 +47,52 @@ def server_set_ir(databank: DataBank, values: tuple):
43
47
print ("Input registers updated!" )
44
48
45
49
46
- class PLC () :
47
- """PLC Client Class"""
48
-
50
+ class PLC :
51
+ """PLC Client Class. """
52
+
49
53
def __init__ (self , ip_address : str ):
50
- """Constructor method
54
+ """Init object instance of the class.
55
+
51
56
Args:
52
57
ip_address: IP address of PLC to be connected
53
58
"""
54
59
# Define attribute for PLC IP Address
55
60
self .plc_ip = ip_address
56
-
61
+
57
62
def write_words (self , values : tuple ):
58
- """Write PLC Registers through Modbus TCP Communication
63
+ """Write PLC Registers through Modbus TCP Communication.
64
+
59
65
Args:
60
66
values: tuple of values to be written to register
61
67
"""
62
68
# Define Modbus TCP Client and connect to it
63
69
client = ModbusTcpClient (self .plc_ip )
64
70
client .connect ()
65
-
71
+
66
72
# Write value to registers: %MW0, %MW1, and so on
67
73
print ("Writing value to PLC registers..." )
68
74
for reg , val in enumerate (values ):
69
75
client .write_registers (reg , int (val ))
70
76
print ("Writing operation done!" )
71
-
77
+
72
78
# Close connection
73
79
client .close ()
74
-
80
+
75
81
def write_bits (self , bit_value : str ):
76
- """Write PLC Coils through Modbus TCP Communication
82
+ """Write PLC Coils through Modbus TCP Communication.
83
+
77
84
Args:
78
85
bit_value: binary value in string format
79
86
"""
80
87
# Define Modbus TCP Client and connect to it
81
88
client = ModbusTcpClient (self .plc_ip )
82
89
client .connect ()
83
-
90
+
84
91
# Write value to coils: %M0, %M1, and so on
85
92
print ("Writing value to PLC coils..." )
86
93
for coil , bit in enumerate (bit_value ):
87
94
client .write_coils (coil , [int (bit )])
88
95
print ("Writing operation done!" )
89
-
96
+
90
97
# Close connection
91
- client .close ()
98
+ client .close ()
0 commit comments