Skip to content

Commit 9cd2ada

Browse files
Rahul Raghunathdhoomakethu
authored andcommitted
Merged in PYM-3-fix-generatorpy-for-binary-and-port (pull request #5)
PYM-3: Generator script fix and port to Python3 Approved-by: sanjay <sanjay@riptideio.com>
2 parents 5cf449f + 8dc6dde commit 9cd2ada

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

examples/contrib/message-generator.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* binary - `./generate-messages.py -f binary -m tx -b`
1313
'''
1414
from optparse import OptionParser
15+
import codecs as c
1516
#--------------------------------------------------------------------------#
1617
# import all the available framers
1718
#--------------------------------------------------------------------------#
@@ -30,6 +31,7 @@
3031
from pymodbus.mei_message import *
3132
from pymodbus.register_read_message import *
3233
from pymodbus.register_write_message import *
34+
from pymodbus.compat import IS_PYTHON3
3335

3436
#--------------------------------------------------------------------------#
3537
# initialize logging
@@ -51,17 +53,17 @@
5153
WriteSingleRegisterRequest,
5254
WriteSingleCoilRequest,
5355
ReadWriteMultipleRegistersRequest,
54-
56+
5557
ReadExceptionStatusRequest,
5658
GetCommEventCounterRequest,
5759
GetCommEventLogRequest,
5860
ReportSlaveIdRequest,
59-
61+
6062
ReadFileRecordRequest,
6163
WriteFileRecordRequest,
6264
MaskWriteRegisterRequest,
6365
ReadFifoQueueRequest,
64-
66+
6567
ReadDeviceInformationRequest,
6668

6769
ReturnQueryDataRequest,
@@ -97,7 +99,7 @@
9799
WriteSingleRegisterResponse,
98100
WriteSingleCoilResponse,
99101
ReadWriteMultipleRegistersResponse,
100-
102+
101103
ReadExceptionStatusResponse,
102104
GetCommEventCounterResponse,
103105
GetCommEventLogResponse,
@@ -149,13 +151,13 @@
149151
'write_registers' : [0x01] * 8,
150152
'transaction' : 0x01,
151153
'protocol' : 0x00,
152-
'unit' : 0x01,
154+
'unit' : 0xff,
153155
}
154156

155157

156-
#---------------------------------------------------------------------------#
158+
#---------------------------------------------------------------------------#
157159
# generate all the requested messages
158-
#---------------------------------------------------------------------------#
160+
#---------------------------------------------------------------------------#
159161
def generate_messages(framer, options):
160162
''' A helper method to parse the command line options
161163
@@ -168,13 +170,16 @@ def generate_messages(framer, options):
168170
print ("%-44s = " % message.__class__.__name__)
169171
packet = framer.buildPacket(message)
170172
if not options.ascii:
171-
packet = packet.encode('hex') + '\n'
172-
print (packet) # because ascii ends with a \r\n
173+
if not IS_PYTHON3:
174+
packet = packet.encode('hex')
175+
else:
176+
packet = c.encode(packet, 'hex_codec').decode('utf-8')
177+
print ("{}\n".format(packet)) # because ascii ends with a \r\n
173178

174179

175-
#---------------------------------------------------------------------------#
180+
#---------------------------------------------------------------------------#
176181
# initialize our program settings
177-
#---------------------------------------------------------------------------#
182+
#---------------------------------------------------------------------------#
178183
def get_options():
179184
''' A helper method to parse the command line options
180185

0 commit comments

Comments
 (0)