Skip to content

Commit

Permalink
Merged in PYM-3-fix-generatorpy-for-binary-and-port (pull request #5)
Browse files Browse the repository at this point in the history
PYM-3: Generator script fix and port to Python3

Approved-by: sanjay <[email protected]>
  • Loading branch information
Rahul Raghunath authored and dhoomakethu committed Nov 21, 2017
2 parents 5cf449f + 8dc6dde commit 9cd2ada
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions examples/contrib/message-generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* binary - `./generate-messages.py -f binary -m tx -b`
'''
from optparse import OptionParser
import codecs as c
#--------------------------------------------------------------------------#
# import all the available framers
#--------------------------------------------------------------------------#
Expand All @@ -30,6 +31,7 @@
from pymodbus.mei_message import *
from pymodbus.register_read_message import *
from pymodbus.register_write_message import *
from pymodbus.compat import IS_PYTHON3

#--------------------------------------------------------------------------#
# initialize logging
Expand All @@ -51,17 +53,17 @@
WriteSingleRegisterRequest,
WriteSingleCoilRequest,
ReadWriteMultipleRegistersRequest,

ReadExceptionStatusRequest,
GetCommEventCounterRequest,
GetCommEventLogRequest,
ReportSlaveIdRequest,

ReadFileRecordRequest,
WriteFileRecordRequest,
MaskWriteRegisterRequest,
ReadFifoQueueRequest,

ReadDeviceInformationRequest,

ReturnQueryDataRequest,
Expand Down Expand Up @@ -97,7 +99,7 @@
WriteSingleRegisterResponse,
WriteSingleCoilResponse,
ReadWriteMultipleRegistersResponse,

ReadExceptionStatusResponse,
GetCommEventCounterResponse,
GetCommEventLogResponse,
Expand Down Expand Up @@ -149,13 +151,13 @@
'write_registers' : [0x01] * 8,
'transaction' : 0x01,
'protocol' : 0x00,
'unit' : 0x01,
'unit' : 0xff,
}


#---------------------------------------------------------------------------#
#---------------------------------------------------------------------------#
# generate all the requested messages
#---------------------------------------------------------------------------#
#---------------------------------------------------------------------------#
def generate_messages(framer, options):
''' A helper method to parse the command line options
Expand All @@ -168,13 +170,16 @@ def generate_messages(framer, options):
print ("%-44s = " % message.__class__.__name__)
packet = framer.buildPacket(message)
if not options.ascii:
packet = packet.encode('hex') + '\n'
print (packet) # because ascii ends with a \r\n
if not IS_PYTHON3:
packet = packet.encode('hex')
else:
packet = c.encode(packet, 'hex_codec').decode('utf-8')
print ("{}\n".format(packet)) # because ascii ends with a \r\n


#---------------------------------------------------------------------------#
#---------------------------------------------------------------------------#
# initialize our program settings
#---------------------------------------------------------------------------#
#---------------------------------------------------------------------------#
def get_options():
''' A helper method to parse the command line options
Expand Down

0 comments on commit 9cd2ada

Please sign in to comment.