Skip to content

Commit 5cf449f

Browse files
Rahul Raghunathdhoomakethu
authored andcommitted
Merged in PYM-2-message-parserpy-fix-and-port (pull request #4)
PYM-2 message parser fix and port Approved-by: sanjay <sanjay@riptideio.com>
2 parents 2bbb5e5 + 673de54 commit 5cf449f

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

examples/contrib/message-parser.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,24 @@
1111
* rtu
1212
* binary
1313
'''
14-
#---------------------------------------------------------------------------#
14+
#---------------------------------------------------------------------------#
1515
# import needed libraries
1616
#---------------------------------------------------------------------------#
1717
from __future__ import print_function
1818
import sys
1919
import collections
2020
import textwrap
2121
from optparse import OptionParser
22+
import codecs as c
23+
2224
from pymodbus.utilities import computeCRC, computeLRC
2325
from pymodbus.factory import ClientDecoder, ServerDecoder
2426
from pymodbus.transaction import ModbusSocketFramer
2527
from pymodbus.transaction import ModbusBinaryFramer
2628
from pymodbus.transaction import ModbusAsciiFramer
2729
from pymodbus.transaction import ModbusRtuFramer
30+
from pymodbus.compat import byte2int, int2byte, IS_PYTHON3
31+
2832

2933
#--------------------------------------------------------------------------#
3034
# Logging
@@ -33,9 +37,9 @@
3337
modbus_log = logging.getLogger("pymodbus")
3438

3539

36-
#---------------------------------------------------------------------------#
40+
#---------------------------------------------------------------------------#
3741
# build a quick wrapper around the framers
38-
#---------------------------------------------------------------------------#
42+
#---------------------------------------------------------------------------#
3943
class Decoder(object):
4044

4145
def __init__(self, framer, encode=False):
@@ -52,7 +56,10 @@ def decode(self, message):
5256
5357
:param message: The messge to decode
5458
'''
55-
value = message if self.encode else message.encode('hex')
59+
if IS_PYTHON3:
60+
value = message if self.encode else c.encode(message, 'hex_codec')
61+
else:
62+
value = message if self.encode else message.encode('hex')
5663
print("="*80)
5764
print("Decoding Message %s" % value)
5865
print("="*80)
@@ -64,7 +71,7 @@ def decode(self, message):
6471
print("%s" % decoder.decoder.__class__.__name__)
6572
print("-"*80)
6673
try:
67-
decoder.addToFrame(message.encode())
74+
decoder.addToFrame(message)
6875
if decoder.checkFrame():
6976
decoder.advanceFrame()
7077
decoder.processIncomingPacket(message, self.report)
@@ -86,7 +93,7 @@ def report(self, message):
8693
:param message: The message to print
8794
'''
8895
print("%-15s = %s" % ('name', message.__class__.__name__))
89-
for k,v in message.__dict__.iteritems():
96+
for (k, v) in message.__dict__.items():
9097
if isinstance(v, dict):
9198
print("%-15s =" % k)
9299
for kk,vv in v.items():
@@ -102,9 +109,9 @@ def report(self, message):
102109
print("%-15s = %s" % ('documentation', message.__doc__))
103110

104111

105-
#---------------------------------------------------------------------------#
112+
#---------------------------------------------------------------------------#
106113
# and decode our message
107-
#---------------------------------------------------------------------------#
114+
#---------------------------------------------------------------------------#
108115
def get_options():
109116
''' A helper method to parse the command line options
110117
@@ -136,6 +143,10 @@ def get_options():
136143
help="The file containing messages to parse",
137144
dest="file", default=None)
138145

146+
parser.add_option("-t", "--transaction",
147+
help="If the incoming message is in hexadecimal format",
148+
action="store_true", dest="transaction", default=False)
149+
139150
(opt, arg) = parser.parse_args()
140151

141152
if not opt.message and len(arg) > 0:
@@ -150,8 +161,19 @@ def get_messages(option):
150161
:returns: The message iterator to parse
151162
'''
152163
if option.message:
164+
if option.transaction:
165+
msg = ""
166+
for segment in option.message.split():
167+
segment = segment.replace("0x", "")
168+
segment = "0" + segment if len(segment) == 1 else segment
169+
msg = msg + segment
170+
option.message = msg
171+
153172
if not option.ascii:
154-
option.message = option.message.decode('hex')
173+
if not IS_PYTHON3:
174+
option.message = option.message.decode('hex')
175+
else:
176+
option.message = c.decode(option.message.encode(), 'hex_codec')
155177
yield option.message
156178
elif option.file:
157179
with open(option.file, "r") as handle:

pymodbus/server/sync.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ def handle(self):
102102
if data:
103103
if _logger.isEnabledFor(logging.DEBUG):
104104
_logger.debug(" ".join([hex(byte2int(x)) for x in data]))
105-
unit_address = byte2int(data[0])
105+
if not isinstance(self.framer, ModbusBinaryFramer):
106+
unit_address = byte2int(data[0])
107+
else:
108+
unit_address = byte2int(data[1])
106109
if unit_address in self.server.context:
107110
self.framer.processIncomingPacket(data, self.execute)
108111
except Exception as msg:

pymodbus/transaction.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ def processIncomingPacket(self, data, callback):
461461

462462
def _process(self, callback, error=False):
463463
"""
464-
Process incoming packets irrespective error condition
464+
Process incoming packets irrespective error condition
465465
"""
466466
data = self.getRawFrame() if error else self.getFrame()
467467
result = self.decoder.decode(data)
@@ -487,7 +487,7 @@ def resetFrame(self):
487487

488488
def getRawFrame(self):
489489
"""
490-
Returns the complete buffer
490+
Returns the complete buffer
491491
"""
492492
return self.__buffer
493493

@@ -922,7 +922,7 @@ def __init__(self, decoder):
922922
'''
923923
self.__buffer = b''
924924
self.__header = {'crc':0x0000, 'len':0, 'uid':0x00}
925-
self.__hsize = 0x02
925+
self.__hsize = 0x01
926926
self.__start = b'\x7b' # {
927927
self.__end = b'\x7d' # }
928928
self.__repeat = [b'}'[0], b'{'[0]] # python3 hack

0 commit comments

Comments
 (0)