Skip to content

Commit 42b6ed2

Browse files
committed
ModbusProtocol add cmd_path to _start_server and fix receive bug
recieve is wrong !! use receive
1 parent 9a6f775 commit 42b6ed2

File tree

8 files changed

+35
-39
lines changed

8 files changed

+35
-39
lines changed

examples/swat-s1/plc1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ def main_loop(self):
8181

8282
# TODO: use it when implement raw water tank
8383
# read from PLC2 (constant value)
84-
fit201 = float(self.recieve(FIT201_2, PLC2_ADDR))
84+
fit201 = float(self.receive(FIT201_2, PLC2_ADDR))
8585
print "DEBUG PLC1 - receive fit201: %f" % fit201
8686
self.send(FIT201_1, fit201, PLC1_ADDR)
8787

8888
# # read from PLC3
89-
lit301 = float(self.recieve(LIT301_3, PLC3_ADDR))
89+
lit301 = float(self.receive(LIT301_3, PLC3_ADDR))
9090
print "DEBUG PLC1 - receive lit301: %f" % lit301
9191
self.send(LIT301_1, lit301, PLC1_ADDR)
9292

examples/swat-s1/plc2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def main_loop(self):
4242
print "DEBUG PLC2 - get fit201: %f" % fit201
4343

4444
self.send(FIT201_2, fit201, PLC2_ADDR)
45-
# fit201 = self.recieve(FIT201_2, PLC2_ADDR)
45+
# fit201 = self.receive(FIT201_2, PLC2_ADDR)
4646
# print "DEBUG PLC2 - receive fit201: ", fit201
4747

4848
time.sleep(PLC_PERIOD_SEC)

examples/toy/plc1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def main_loop(self, sleep=0.5):
4242
count = 0
4343
END = 6e6
4444
while(True):
45-
rec_s31 = self.recieve(SENSOR3_1, PLC1_ADDR)
45+
rec_s31 = self.receive(SENSOR3_1, PLC1_ADDR)
4646
# print 'DEBUG: toy plc1 receive SENSOR3_1: ', rec_s31
4747
get_s32 = self.get(SENSOR3_2)
4848
print 'DEBUG: toy plc1 get SENSOR3_2: ', get_s32

minicps/devices.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ def send(self, what, value, address, **kwargs):
243243
if type(what) is not tuple:
244244
raise TypeError('Parameter must be a tuple.')
245245
else:
246-
return self._protocol._send(what, value, address)
246+
return self._protocol._send(what, value, address, **kwargs)
247247

248-
def recieve(self, what, address, **kwargs):
248+
def receive(self, what, address, **kwargs):
249249
"""Receive (read) a value from another network host.
250250
251251
``kwargs`` dict is used to pass extra key-value pair according to the
@@ -254,13 +254,13 @@ def recieve(self, what, address, **kwargs):
254254
:param tuple what: field[s] identifier[s]
255255
:param str address: ``ip[:port]``
256256
257-
:returns: recv value or ``TypeError`` if ``what`` is not a ``tuple``
257+
:returns: received value or ``TypeError`` if ``what`` is not a ``tuple``
258258
"""
259259

260260
if type(what) is not tuple:
261261
raise TypeError('Parameter must be a tuple.')
262262
else:
263-
return self._protocol._receive(what, address)
263+
return self._protocol._receive(what, address, **kwargs)
264264

265265

266266
# TODO: rename pre_loop and main_loop?

minicps/protocols.py

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def __init__(self, protocol):
6363

6464
try:
6565
from minicps import __file__
66-
self._minicps_path = __file__[:-12]
66+
self._minicps_path = __file__[:-12] # XXX: remove trailing chars
6767
except Exception as error:
6868
print 'ERROR Protocol __init__ set _minicps_path: ', error
6969

@@ -518,22 +518,27 @@ def __init__(self, protocol):
518518
# TODO: implement it
519519

520520

521+
# TODO: still not sure about the tags API
521522
@classmethod
522-
def _start_server(cls, address, tags, cmd_path, mode=1):
523+
def _start_server(cls, cmd_path, address='localhost:502',
524+
tags=(20, 20, 20, 20), mode=1):
523525
"""Start a pymodbus modbus server.
524526
525527
The command used to start the server is generated by
526528
``_start_server_cmd``.
527529
528-
Notice that the client has to manage the new process,
529-
eg:kill it after use.
530+
Consistency between modbus server key-values and state key-values has to
531+
be guaranteed by the client.
530532
531-
:address: to serve
532533
:cmd_path: path to the script to start a server
534+
:address: ip:port
533535
:tags: ordered tuple of ints representing the numbers of discrete
534536
inputs, coils, input registers, and holding registers to be init.
535537
Current pymodbus servers only support ModbusSequentialDataBlock.
536538
:mode: int greater than 1, typically set by the constructor
539+
540+
:returns: list of strings generated with shlex.split,
541+
passable to subprocess.Popen object
537542
"""
538543

539544
try:
@@ -546,25 +551,10 @@ def _start_server(cls, address, tags, cmd_path, mode=1):
546551
print 'ERROR modbus _start_server: ', error
547552

548553

549-
# TODO: still not sure about the tags API
550554
@classmethod
551555
def _start_server_cmd(cls, cmd_path, address='localhost:502',
552-
tags=(10, 10, 10, 10), mode=1):
553-
# ('CO1', 1, 'CO'), ('HR1', 1, 'HR'))):
554-
"""Build a subprocess.Popen cmd string for pycomm server.
555-
556-
Consistency between modbus server key-values and state key-values has to
557-
be guaranteed by the client.
558-
559-
:address: to serve
560-
:cmd_path: path to the script to start a server
561-
:tags: ordered tuple of ints representing the numbers of discrete
562-
inputs, coils, input registers, and holding registers to be init.
563-
Current pymodbus servers only support ModbusSequentialDataBlock.
564-
565-
:returns: list of strings generated with shlex.split,
566-
passable to subprocess.Popen object
567-
"""
556+
tags=(20, 20, 20, 20), mode=1):
557+
"""Build a subprocess.Popen cmd string for pycomm server."""
568558

569559
if sys.platform.startswith('linux'):
570560
SHELL = '/bin/bash -c '

scripts/pox/l2_pairs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def _handle_PacketIn(event):
283283

284284
# define an action object to be appended to the actions list
285285
# the action tell the switch to use event.port as output port
286-
# event.port was the port on which the switch recieved the packet
286+
# event.port was the port on which the switch received the packet
287287
# that generates the PackeIn
288288
action = of.ofp_action_output(port=event.port) # then forward packet to event.port
289289

@@ -309,7 +309,7 @@ def _handle_PacketIn(event):
309309

310310
def _handle_PortStatus(event):
311311
"""
312-
Controller recieve info from a datapath about
312+
Controller received info from a datapath about
313313
port changes.
314314
315315
@@ -350,7 +350,7 @@ def launch(std_flood_port=True):
350350
351351
Datapath mgmt can be programmed using event
352352
handling. Each event (in this case PacketIn)
353-
triggers an handler that recieve an Event obj
353+
triggers an handler that received an Event obj
354354
as argument. The Event obj contains a reference
355355
to the relevant Connection obj. Indeed the handler
356356
can use Event obj to retrieve information about

tests/devices_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from nose.plugins.skip import SkipTest
1414

1515
# NOTE: currently testing only set and get
16-
# TODO: find a way to test also send and recieve outside network emulaiton
16+
# TODO: find a way to test also send and received outside network emulaiton
1717

1818
class TestDevice():
1919

tests/protocols_tests.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,16 @@ class TestModbusProtocol():
231231
else:
232232
raise OSError
233233

234+
from minicps import __file__ as minicps_path
235+
minicps_path = minicps_path[:-12]
236+
SERVER_CMD_PATH = sys.executable + ' ' + minicps_path + \
237+
'pymodbus/servers.py ' # NOTE: ending whitespace
238+
234239
def test_server_start_stop(self):
235240

236241
try:
237-
server = ModbusProtocol._start_server('localhost:502', self.TAGS)
242+
server = ModbusProtocol._start_server(self.SERVER_CMD_PATH,
243+
'localhost:502', self.TAGS)
238244
ModbusProtocol._stop_server(server)
239245

240246
except Exception as error:
@@ -279,7 +285,7 @@ def test_send(self):
279285
OFFSET = 10
280286

281287
try:
282-
server = ModbusProtocol._start_server(ADDRESS, TAGS)
288+
server = ModbusProtocol._start_server(self.SERVER_CMD_PATH, ADDRESS, TAGS)
283289
time.sleep(1.0)
284290

285291
print('TEST: Write to holding registers')
@@ -313,7 +319,7 @@ def test_receive(self):
313319
OFFSET = 10
314320

315321
try:
316-
server = ModbusProtocol._start_server(ADDRESS, TAGS)
322+
server = ModbusProtocol._start_server(self.SERVER_CMD_PATH, ADDRESS, TAGS)
317323
time.sleep(1.0)
318324

319325
print('TEST: Read holding registers')
@@ -407,7 +413,7 @@ def test_receive_count(self):
407413
TAGS = (20, 20, 20, 20)
408414

409415
try:
410-
server = ModbusProtocol._start_server(ADDRESS, TAGS)
416+
server = ModbusProtocol._start_server(self.SERVER_CMD_PATH, ADDRESS, TAGS)
411417
time.sleep(1.0)
412418

413419
print('TEST: Read holding registers, count=3')
@@ -446,7 +452,7 @@ def test_client_server_count(self):
446452
TAGS = (50, 50, 50, 50)
447453

448454
try:
449-
server = ModbusProtocol._start_server(ADDRESS, TAGS)
455+
server = ModbusProtocol._start_server(self.SERVER_CMD_PATH, ADDRESS, TAGS)
450456
time.sleep(1.0)
451457

452458
print('TEST: Write and Read holding registers, offset=4, count=3')

0 commit comments

Comments
 (0)