-
Notifications
You must be signed in to change notification settings - Fork 7
/
cliente_modbus.py
332 lines (277 loc) · 11.5 KB
/
cliente_modbus.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env python
#---------------------------------------------------------------------------#
# import the various server implementations
#---------------------------------------------------------------------------#
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
#from pymodbus.client.sync import ModbusUdpClient as ModbusClient
#from pymodbus.client.sync import ModbusSerialClient as ModbusClient
from pymodbus.diag_message import *
from pymodbus.file_message import *
from pymodbus.other_message import *
from pymodbus.mei_message import *
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.payload import BinaryPayloadBuilder
from pymodbus.diag_message import *
import time
#######################################
#LEER REGISTROS DE ENTRADA (FUNC 4)
#######################################
#print "############################################"
#print "#LEER REGISTROS DE ENTRADA (FUNC 4)
#print "############################################"
#address = 0x01
#count = 1
#result = client.read_input_registers(address, count)
#print result.registers
#decoder = BinaryPayloadDecoder.fromRegisters(result.registers, endian=Endian.Little)
#decoded = {
#'string': decoder.decode_string(8),
#'float': decoder.decode_32bit_float(),
#'16uint': decoder.decode_16bit_uint(),
#'8int': decoder.decode_8bit_int(),
#'bits': decoder.decode_bits(),
#}
#print "-" * 60
#print "Decoded Data"
#print "-" * 60
#for name, value in decoded.iteritems():
# print ("%s\t" % name), value
##############################################
#LEER IDENTIFICACION DEL DISPOSITIVO (FUNC 43)
##############################################
def leer_id():
rq = ReadDeviceInformationRequest()
rr = client.execute(rq)
for x in range(0,len(rr.information)):
print (rr.information[x]),
print 2*""
print 2*""
#========================================================================
# COILS
#========================================================================
############################
# LEER COILS (FUNC 01)
############################
def leer_varios_coils(initial_coil, num_coils, slave):
rr = client.read_coils(initial_coil, num_coils, unit=slave)
if debug:
print "==> Leyendo", num_coils, " coils comenzando por el ",initial_coil, " del esclavo", slave
for x in range(1,num_coils+1):
if rr.bits[x-1]:
print "1",
else:
print "0",
print ""
print ""
#time.sleep(1)
############################
# ESCRIBIR UN COIL (FUNC 05)
############################
def escribir_coil(num_coil, value, slave):
if value<>0:
value=1
rq = client.write_coil(num_coil, value,unit=slave)
if debug:
print "==> Escribiendo el valor ", value, " en el coil ", num_coil , " del esclavo ", slave
print 2*""
print 2*""
############################
# ESCRIBIR VARIOS COILS (FUNC 15)
############################
def escribir_varios_coils(initial_coil, values, slave):
#rq = client.write_coils(1, [False]*8, unit=0x01)
rq = client.write_coils(initial_coil, values, unit=slave)
if debug:
print ("==> Escribiendo los siguientes valores desde el coil ", initial_coil, " del esclavo ", slave)
#========================================================================
# INPUTS
#========================================================================
############################
# LEER INPUTS (FUNC 02)
############################
def leer_inputs(initial_input, num_inputs, slave):
rr = client.read_discrete_inputs(initial_input,num_inputs, unit=slave)
if debug:
print "==> Leyendo los inputs ", initial_input, " al ", initial_input+num_inputs,""
for x in range(1,num_inputs+1):
if rr.bits[x-1]:
print "1",
else:
print "0",
print 3*""
print 2*""
#========================================================================
# REGISTROS
#========================================================================
############################
# ESCRIBIR UN REGISTRO (FUNC 06)
############################
def escribir_registro(register, value, slave):
rq = client.write_register(register, value,unit=slave)
if debug:
print "==> Escribiendo el registro ", register, "con el valor ", value, " en el esclavo ", slave , ""
############################
# READ HOLDING REGISTERS (FUNC 03)
############################
def leer_registros_rw(initial_register, num_registers, slave):
rr = client.read_holding_registers(initial_register,num_registers, unit=slave)
if debug:
print "==> Leyendo ", num_registers, " registros de E/S desde el registro ", initial_register, " en el esclavo ", slave , ""
if len(rr.registers)>0:
for x in range(1,num_registers):
print rr.registers[x-1],
print 2*""
print 2*""
############################
# ESCRIBIR VARIOS REGISTROS (FUNC 16)
############################
def escribir_registros(initial_register, values, slave):
#rq = client.write_registers(1, [10]*8, unit=0x01)
rq = client.write_registers(initial_register, values,unit=slave)
if debug:
print "==> Escribiendo desde el registro", initial_register, " del esclavo ", slave, "con los valores:"
print values
print 2*""
############################
# LEER REGISTROS DE ENTRADA (FUNC 04)
############################
def leer_registros_lectura(initial_register, num_registers, slave):
rr = client.read_input_registers(initial_register,num_registers, unit=slave)
if debug:
print "==> Leyendo ", num_registers, " de LECTURA desde el registro ", initial_register, " en el esclavo ", slave , ""
if rr:
for x in range(1,num_registers):
print rr.registers[x-1],
print 2*""
############################
# LEER y escribir registros (FUNC 23)
############################
def lee_escribe_registros(initial_read_register, num_read_register, initial_write_register, values, slave):
arguments = {
'read_address': initial_read_register,
'read_count': num_read_register,
'write_address': initial_write_register,
#'write_registers': [20]*8,
'write_registers': values,
'unit': slave,
}
rq = client.readwrite_registers(**arguments)
#assert(rq.function_code < 0x80) # test that we are not an error
#assert(rq.registers == [20]*8) # test the expected value
#assert(rr.registers == [20]*8) # test the expected value
#---------------------------------------------------------------------------#
# diagnostic requests
#---------------------------------------------------------------------------#
def get_diagnostics():
rq = ReturnQueryDataRequest()
rr = client.execute(rq)
#assert(rr.message[0] == 0x0000) # test the resulting message
rq = RestartCommunicationsOptionRequest()
rr = client.execute(rq)
#assert(rr == None) # not supported by reference
#assert(rr.message == 0x0000) # test the resulting message
rq = ReturnDiagnosticRegisterRequest()
rr = client.execute(rq)
#assert(rr == None) # not supported by reference
rq = ChangeAsciiInputDelimiterRequest()
rr = client.execute(rq)
#assert(rr == None) # not supported by reference
rq = ForceListenOnlyModeRequest()
client.execute(rq) # does not send a response
rq = ClearCountersRequest()
rr = client.execute(rq)
#assert(rr == None) # not supported by reference
rq = ReturnBusCommunicationErrorCountRequest()
rr = client.execute(rq)
#assert(rr == None) # not supported by reference
rq = ReturnBusExceptionErrorCountRequest()
rr = client.execute(rq)
#assert(rr == None) # not supported by reference
rq = ReturnSlaveMessageCountRequest()
rr = client.execute(rq)
#assert(rr == None) # not supported by reference
rq = ReturnSlaveNoResponseCountRequest()
rr = client.execute(rq)
#assert(rr == None) # not supported by reference
rq = ReturnSlaveNAKCountRequest()
rr = client.execute(rq)
#assert(rr == None) # not supported by reference
rq = ReturnSlaveBusyCountRequest()
rr = client.execute(rq)
#assert(rr == None) # not supported by reference
rq = ReturnSlaveBusCharacterOverrunCountRequest()
rr = client.execute(rq)
#assert(rr == None) # not supported by reference
rq = ReturnIopOverrunCountRequest()
rr = client.execute(rq)
#assert(rr == None) # not supported by reference
rq = ClearOverrunCountRequest()
rr = client.execute(rq)
#assert(rr == None) # not supported by reference
rq = GetClearModbusPlusRequest()
rr = client.execute(rq)
#assert(rr == None) # not supported by reference
print "KKK"
def Other_requests():
rq = ReportSlaveIdRequest()
rr = client.execute(rq)
#assert(rr == None) # not supported by reference
#assert(rr.function_code < 0x80) # test that we are not an error
#assert(rr.identifier == 0x00) # test the slave identifier
#assert(rr.status == 0x00) # test that the status is ok
rq = ReadExceptionStatusRequest()
rr = client.execute(rq)
#assert(rr == None) # not supported by reference
#assert(rr.function_code < 0x80) # test that we are not an error
#assert(rr.status == 0x55) # test the status code
rq = GetCommEventCounterRequest()
rr = client.execute(rq)
#assert(rr == None) # not supported by reference
#assert(rr.function_code < 0x80) # test that we are not an error
#assert(rr.status == True) # test the status code
#assert(rr.count == 0x00) # test the status code
rq = GetCommEventLogRequest()
rr = client.execute(rq)
#assert(rr == None) # not supported by reference
#assert(rr.function_code < 0x80) # test that we are not an error
#assert(rr.status == True) # test the status code
#assert(rr.event_count == 0x00) # test the number of events
#assert(rr.message_count == 0x00) # test the number of messages
#assert(len(rr.events) == 0x00) # test the number of events
#---------------------------------------------------------------------------#
# configure the client logging
#---------------------------------------------------------------------------#
import logging
logging.basicConfig()
log = logging.getLogger()
#log.setLevel(logging.DEBUG)
#---------------------------------------------------------------------------#
# choose the client
#---------------------------------------------------------------------------#
#
# client = ModbusClient('localhost', retries=3, retry_on_empty=True)
#---------------------------------------------------------------------------#
client = ModbusClient('10.3.1.205', port=502)
#client = ModbusClient(method='ascii', port='/dev/pts/2', timeout=1)
#client = ModbusClient(method='rtu', port='/dev/pts/2', timeout=1)
client.connect()
k=0
debug=True
#l={98+k,76+k,54+k,32+k,10+k,987+k,654+k,321+k}
l={6757,2340,340,8730,0,0,0,0}
print "======================"
print "Con el Tofino activado"
print "======================"
leer_id()
#while (k<9):
leer_registros_rw(1,len(l),0x01)
# leer_varios_coils(0,16,0x00)
#escribir_registros(1, l, 0x01)
# escribir_coil(k, 0, 0x01)
# leer_registros_rw(1,len(l),0x01)
# leer_varios_coils(0,16,0x00)
# k=k+1
# time.sleep(1)
# print "=============================="
client.close()