Skip to content

Commit 2755528

Browse files
committed
TestModbusProtocol read HR and IR passes
1 parent 32dad8c commit 2755528

File tree

3 files changed

+83
-19
lines changed

3 files changed

+83
-19
lines changed

minicps/protocols.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,7 @@ def _receive(self, what, address='localhost:44818'):
404404
# print 'DEBUG enip _receive cmd shlex list: ', cmd
405405

406406
try:
407-
client = subprocess.Popen(
408-
cmd,
409-
shell=False,
407+
client = subprocess.Popen(cmd, shell=False,
410408
stdout=subprocess.PIPE)
411409

412410
# client.communicate is blocking
@@ -417,6 +415,7 @@ def _receive(self, what, address='localhost:44818'):
417415
# between a pair of square brackets
418416
raw_string = raw_out[0]
419417
out = raw_string[(raw_string.find('[') + 1):raw_string.find(']')]
418+
420419
return out
421420

422421
except Exception as error:
@@ -655,8 +654,7 @@ def _send(self, what, value, address='localhost:502'):
655654
print 'ERROR modbus _send: ', error
656655

657656

658-
# TODO: implement it
659-
def _receive(self, what, address='localhost:44818'):
657+
def _receive(self, what, address='localhost:502'):
660658
"""Receive (read) a value from another host.
661659
662660
It is a blocking operation the parent process will wait till the child
@@ -667,35 +665,46 @@ def _receive(self, what, address='localhost:44818'):
667665
668666
:returns: tag value as a `str`
669667
"""
670-
671-
tag_string = ''
672-
tag_string = EnipProtocol._tuple_to_cpppo_tag(what)
668+
colon_index = address.find(':')
669+
IP = '-i {} '.format(address[:colon_index])
670+
PORT = '-p {} '.format(address[colon_index+1:])
671+
# NOTE: following data is validated by client script
672+
MODE = '-m {} '.format('r')
673+
TYPE = '-t {} '.format(what[0])
674+
OFFSET = '-o {} '.format(what[1]) # NOTE: 0-based
673675

674676
cmd = shlex.split(
675677
self._client_cmd +
676-
'--log ' + self._client_log +
677-
'--address ' + address +
678-
' ' + tag_string
678+
IP +
679+
PORT +
680+
MODE +
681+
TYPE +
682+
OFFSET
679683
)
680-
# print 'DEBUG enip _receive cmd shlex list: ', cmd
684+
print 'DEBUG modbus_receive cmd shlex list: ', cmd
681685

682686
try:
683-
client = subprocess.Popen(
684-
cmd,
685-
shell=False,
687+
client = subprocess.Popen(cmd,shell=False,
686688
stdout=subprocess.PIPE)
687689

688690
# client.communicate is blocking
689691
raw_out = client.communicate()
690-
# print 'DEBUG enip _receive raw_out: ', raw_out
692+
print 'DEBUG modbus _receive raw_out: ', raw_out
691693

692694
# value is stored as first tuple element
693695
# between a pair of square brackets
694-
raw_string = raw_out[0]
695-
out = raw_string[(raw_string.find('[') + 1):raw_string.find(']')]
696+
raw_string = raw_out[0].strip()
697+
698+
# NOTE: registers store int
699+
if what[0] == 'HR' or what[0] == 'IR':
700+
out = int(raw_string)
701+
# NOTE: coils and discrete inputs store bool
702+
elif what[0] == 'CO' or what[0] == 'DI':
703+
pass
704+
696705
return out
697706

698707
except Exception as error:
699-
print 'ERROR enip _receive: ', error
708+
print 'ERROR modbus _receive: ', error
700709

701710
# }}}

scripts/pymodbus/synch-client.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454

5555
client.connect()
5656

57+
# TODO: check if asserts are slowing down read/write
5758
if args.mode == 'w':
5859

5960
# NOTE: write_register
@@ -66,4 +67,21 @@
6667
co_write = client.write_coil(args.offset, args.coil)
6768
assert(co_write.function_code < 0x80)
6869

70+
71+
elif args.mode == 'r':
72+
73+
# NOTE: read_holding_registers
74+
if args.type == 'HR':
75+
hr_read = client.read_holding_registers(args.offset, count=1)
76+
assert(hr_read.function_code < 0x80)
77+
print(hr_read.registers[0])
78+
79+
# NOTE: read_holding_registers
80+
elif args.type == 'IR':
81+
ir_read = client.read_input_registers(args.offset, count=1)
82+
assert(ir_read.function_code < 0x80)
83+
print(ir_read.registers[0])
84+
85+
86+
6987
client.close()

tests/protocols_tests.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ def test_init_server(self):
278278
print 'ERROR test_init_server: ', error
279279

280280

281+
@SkipTest
281282
def test_send(self):
282283

283284
client = ModbusProtocol(
@@ -311,5 +312,41 @@ def test_send(self):
311312
ModbusProtocol._stop_server(server)
312313
print 'ERROR test_send: ', error
313314

315+
def test_receive(self):
316+
317+
client = ModbusProtocol(
318+
protocol=CLIENT_PROTOCOL)
319+
320+
ADDRESS = 'localhost:502'
321+
TAGS = (20, 20, 20, 20)
322+
OFFSET = 10
323+
324+
try:
325+
server = ModbusProtocol._start_server(ADDRESS, TAGS)
326+
time.sleep(1.0)
327+
328+
print('TEST: Read to holding registers')
329+
for count in range(0, OFFSET):
330+
what = ('HR', count)
331+
eq_(client._receive(what, ADDRESS), 0)
332+
print('')
333+
334+
print('TEST: Read to input registers')
335+
for count in range(0, OFFSET):
336+
what = ('IR', count)
337+
eq_(client._receive(what, ADDRESS), 0)
338+
print('')
339+
340+
# print('TEST: Read to coils')
341+
# for count in range(0, OFFSET):
342+
# what = ('CO', count)
343+
# client._receive(what, ADDRESS)
344+
# print('')
345+
346+
ModbusProtocol._stop_server(server)
347+
348+
except Exception as error:
349+
ModbusProtocol._stop_server(server)
350+
print 'ERROR test_receive: ', error
314351

315352
# }}}

0 commit comments

Comments
 (0)