Skip to content

Commit 951950e

Browse files
committed
Work on count
1 parent cb931eb commit 951950e

File tree

2 files changed

+47
-30
lines changed

2 files changed

+47
-30
lines changed

minicps/protocols.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -735,17 +735,35 @@ def _receive(self, what, address='localhost:502', **kwargs):
735735

736736
# NOTE: coils and discrete inputs store 8 bools
737737
elif what[0] == 'CO' or what[0] == 'DI':
738-
out = []
739-
bools = raw_string[1:-1].split(',')
740738
# print 'DEBUG modbus _receive bools: ', bools
741-
for b in bools:
742-
if b.strip() == 'False':
743-
out.append(False)
744-
elif b.strip() == 'True':
745-
out.append(True)
739+
740+
# NOTE: pymodbus always returns at least a list of 8 bools
741+
bools = raw_string[1:-1].split(',')
742+
743+
# NOTE: single read returns a bool
744+
if count == 1:
745+
if bools[0] == 'False':
746+
out = False
747+
elif bools[0] == 'True':
748+
out = True
746749
else:
747750
raise TypeError('CO or DI values must be bool.')
748751

752+
# NOTE: multiple reads returns a list of bools
753+
else:
754+
out = []
755+
i = 0
756+
for b in bools:
757+
if i >= count:
758+
break
759+
elif b.strip() == 'False':
760+
out.append(False)
761+
elif b.strip() == 'True':
762+
out.append(True)
763+
else:
764+
raise TypeError('CO or DI values must be bool.')
765+
i += 1
766+
749767
return out
750768

751769
except Exception as error:

tests/protocols_tests.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,15 @@ def test_send(self):
273273
time.sleep(1.0)
274274

275275
print('TEST: Write to holding registers')
276-
for count in range(0, OFFSET):
277-
what = ('HR', count)
278-
client._send(what, count, ADDRESS)
276+
for offset in range(0, OFFSET):
277+
what = ('HR', offset)
278+
client._send(what, offset, ADDRESS)
279279
print('')
280280

281281
coil = True
282282
print('TEST: Write to coils')
283-
for count in range(0, OFFSET):
284-
what = ('CO', count)
283+
for offset in range(0, OFFSET):
284+
what = ('CO', offset)
285285
client._send(what, coil, ADDRESS)
286286
coil = not coil
287287
print('')
@@ -306,27 +306,27 @@ def test_receive(self):
306306
time.sleep(1.0)
307307

308308
print('TEST: Read holding registers')
309-
for count in range(0, OFFSET):
310-
what = ('HR', count)
309+
for offset in range(0, OFFSET):
310+
what = ('HR', offset)
311311
eq_(client._receive(what, ADDRESS), 0)
312312
print('')
313313

314314
print('TEST: Read input registers')
315-
for count in range(0, OFFSET):
316-
what = ('IR', count)
315+
for offset in range(0, OFFSET):
316+
what = ('IR', offset)
317317
eq_(client._receive(what, ADDRESS), 0)
318318
print('')
319319

320320
print('TEST: Read discrete inputs')
321-
for count in range(0, OFFSET):
322-
what = ('DI', count)
323-
eq_(client._receive(what, ADDRESS), [False] * 8)
321+
for offset in range(0, OFFSET):
322+
what = ('DI', offset)
323+
eq_(client._receive(what, ADDRESS), False)
324324
print('')
325325

326326
print('TEST: Read coils inputs')
327-
for count in range(0, OFFSET):
328-
what = ('CO', count)
329-
eq_(client._receive(what, ADDRESS), [False] * 8)
327+
for offset in range(0, OFFSET):
328+
what = ('CO', offset)
329+
eq_(client._receive(what, ADDRESS), False)
330330
print('')
331331

332332
ModbusProtocol._stop_server(server)
@@ -343,14 +343,14 @@ def test_client_server(self):
343343
# NOTE: same instance used as server and client
344344
modbus = ModbusProtocol(
345345
protocol=TestModbusProtocol.CLIENT_SERVER_PROTOCOL)
346-
time.sleep(1.0)
346+
time.sleep(1.4)
347347

348348
print('TEST: Write and read coils')
349349
what = ('CO', 0)
350350
value = True
351351
modbus._send(what, value, ADDRESS)
352352
what = ('CO', 0)
353-
eq_(modbus._receive(what, ADDRESS)[0], True)
353+
eq_(modbus._receive(what, ADDRESS), True)
354354
print('')
355355

356356
print('TEST: Write and read holding registers')
@@ -363,7 +363,7 @@ def test_client_server(self):
363363

364364
print('TEST: Read discrete inputs (init to False)')
365365
what = ('DI', 0)
366-
eq_(modbus._receive(what, ADDRESS), [False] * 8)
366+
eq_(modbus._receive(what, ADDRESS), False)
367367
print('')
368368

369369
print('TEST: Read input registers (init to 0)')
@@ -401,11 +401,10 @@ def test_receive_count(self):
401401
eq_(client._receive(what, ADDRESS, count=1), 0)
402402
print('')
403403

404-
# print('TEST: Read discrete inputs')
405-
# for count in range(0, OFFSET):
406-
# what = ('DI', count)
407-
# eq_(client._receive(what, ADDRESS), [False] * 8)
408-
# print('')
404+
print('TEST: Read discrete inputs, count=2')
405+
what = ('DI', 0)
406+
eq_(client._receive(what, ADDRESS, count=2), [False] * 2)
407+
print('')
409408

410409
# print('TEST: Read coils inputs')
411410
# for count in range(0, OFFSET):

0 commit comments

Comments
 (0)