Skip to content

Commit 90eda37

Browse files
authored
Merge pull request #1 from vinichou94/poursuite_python3
Purcue python3
2 parents 8103978 + 291fe83 commit 90eda37

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+936
-746
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ build/
44
dist/
55
pymodbus.egg-info/
66
.coverage
7+
*__pycache__

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Version 1.3.0
2+
------------------------------------------------------------
3+
4+
* Porting to Python 3
5+
16
Version 1.2.0
27
------------------------------------------------------------
38

examples/common/asynchronous-client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030
# helper method to test deferred callbacks
3131
#---------------------------------------------------------------------------#
3232
def dassert(deferred, callback):
33-
def _assertor(value): assert(value)
33+
def _assertor(value, message=None):
34+
assert value, message
3435
deferred.addCallback(lambda r: _assertor(callback(r)))
35-
deferred.addErrback(lambda _: _assertor(False))
36+
deferred.addErrback(lambda e: _assertor(False, e))
3637

3738
#---------------------------------------------------------------------------#
3839
# specify slave to query

examples/common/callback-server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pymodbus.datastore import ModbusSparseDataBlock
1616
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
1717
from pymodbus.transaction import ModbusRtuFramer, ModbusAsciiFramer
18+
from pymodbus.compat import iterkeys
1819

1920
#---------------------------------------------------------------------------#
2021
# import the python libraries we need
@@ -44,7 +45,7 @@ def __init__(self, devices, queue):
4445
self.devices = devices
4546
self.queue = queue
4647

47-
values = {k:0 for k in devices.iterkeys()}
48+
values = {k:0 for k in iterkeys(devices)}
4849
values[0xbeef] = len(values) # the number of devices
4950
super(CallbackDataBlock, self).__init__(values)
5051

examples/common/changing-framers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#---------------------------------------------------------------------------#
4040
# Initialize the client
4141
#---------------------------------------------------------------------------#
42-
client = ModbusClient('localhost', port=5020, framer=ModbusFramer)
42+
client = ModbusClient('localhost', port=502, framer=ModbusFramer)
4343
client.connect()
4444

4545
#---------------------------------------------------------------------------#

examples/common/custom-message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,5 @@ def __init__(self, address):
8383
with ModbusClient('127.0.0.1') as client:
8484
request = CustomModbusRequest(0)
8585
result = client.execute(request)
86-
print result
86+
print(result)
8787

examples/common/modbus-payload.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pymodbus.payload import BinaryPayloadDecoder
88
from pymodbus.payload import BinaryPayloadBuilder
99
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
10+
from pymodbus.compat import iteritems
1011

1112
#---------------------------------------------------------------------------#
1213
# configure the client logging
@@ -36,7 +37,7 @@
3637
# - an 8 bit bitstring [0,1,0,1,1,0,1,0]
3738
#---------------------------------------------------------------------------#
3839
builder = BinaryPayloadBuilder(endian=Endian.Little)
39-
builder.add_string('abcdefgh')
40+
builder.add_string(b'abcdefgh')
4041
builder.add_32bit_float(22.34)
4142
builder.add_16bit_uint(0x1234)
4243
builder.add_8bit_int(0x12)
@@ -64,17 +65,17 @@
6465
decoder = BinaryPayloadDecoder.fromRegisters(result.registers, endian=Endian.Little)
6566
decoded = {
6667
'string': decoder.decode_string(8),
67-
'float': decoder.decode_32bit_float(),
68+
'float': decoder.decode_32bit_float(),
6869
'16uint': decoder.decode_16bit_uint(),
69-
'8int': decoder.decode_8bit_int(),
70-
'bits': decoder.decode_bits(),
70+
'8int': decoder.decode_8bit_int(),
71+
'bits': decoder.decode_bits(),
7172
}
7273

73-
print "-" * 60
74-
print "Decoded Data"
75-
print "-" * 60
76-
for name, value in decoded.iteritems():
77-
print ("%s\t" % name), value
74+
print("-" * 60)
75+
print("Decoded Data")
76+
print("-" * 60)
77+
for name, value in iteritems(decoded):
78+
print(("%s\t" % name), value)
7879

7980
#---------------------------------------------------------------------------#
8081
# close the client

examples/common/performance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def single_client_test(host, cycles):
5454

5555
try:
5656
count = 0
57-
client = ModbusTcpClient(host)
57+
client = ModbusTcpClient(host, port=5020)
5858
while count < cycles:
5959
result = client.read_holding_registers(10, 1).getRegister(0)
6060
count += 1
@@ -75,4 +75,4 @@ def single_client_test(host, cycles):
7575
any(p.start() for p in procs) # start the workers
7676
any(p.join() for p in procs) # wait for the workers to finish
7777
stop = time()
78-
print "%d requests/second" % ((1.0 * cycles) / (stop - start))
78+
print("%d requests/second" % ((1.0 * cycles) / (stop - start)))

examples/contrib/bcd-payload.py

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
that can be used in the pymodbus library. Below is a
77
simple binary coded decimal builder and decoder.
88
'''
9+
import unittest
910
from struct import pack, unpack
1011
from pymodbus.constants import Endian
1112
from pymodbus.interfaces import IPayloadBuilder
@@ -23,7 +24,7 @@ def convert_to_bcd(decimal):
2324
while decimal > 0:
2425
nibble = decimal % 10
2526
bcd += nibble << place
26-
decimal /= 10
27+
decimal //= 10
2728
place += 4
2829
return bcd
2930

@@ -76,12 +77,12 @@ def __init__(self, payload=None, endian=Endian.Little):
7677
self._payload = payload or []
7778
self._endian = endian
7879

79-
def __str__(self):
80+
def to_string(self):
8081
''' Return the payload buffer as a string
8182
8283
:returns: The payload buffer as a string
8384
'''
84-
return ''.join(self._payload)
85+
return b''.join(self._payload)
8586

8687
def reset(self):
8788
''' Reset the payload buffer
@@ -96,10 +97,10 @@ def build(self):
9697
9798
:returns: The payload buffer as a list
9899
'''
99-
string = str(self)
100+
string = self.to_string()
100101
length = len(string)
101-
string = string + ('\x00' * (length % 2))
102-
return [string[i:i+2] for i in xrange(0, length, 2)]
102+
string = string + (b'\x00' * (length % 2))
103+
return [string[i:i+2] for i in range(0, length, 2)]
103104

104105
def add_bits(self, values):
105106
''' Adds a collection of bits to be encoded
@@ -217,6 +218,44 @@ def decode_string(self, size=1):
217218
return self._payload[self._pointer - size:self._pointer]
218219

219220

221+
#---------------------------------------------------------------------------#
222+
# Fixture
223+
#---------------------------------------------------------------------------#
224+
class BcdPayloadUtilityTests(unittest.TestCase):
225+
226+
def setUp(self):
227+
'''
228+
Initializes the test environment and builds request/result
229+
encoding pairs
230+
'''
231+
self.bitstring = [True, False, False, False, True, False, False, False]
232+
self.payload = \
233+
b'\x01\x02\x00\x03\x00\x00\x00\x04\x00\x00\x00\x00' \
234+
b'\x00\x00\x00\xff\xfe\xff\xfd\xff\xff\xff\xfc\xff' \
235+
b'\xff\xff\xff\xff\xff\xff\x00\x00\xa0\x3f\x00\x00' \
236+
b'\x00\x00\x00\x00\x19\x40\x74\x65\x73\x74\x11'
237+
238+
def testPayloadBuilder(self):
239+
''' Test basic bit message encoding/decoding '''
240+
builder = BcdPayloadBuilder(endian=Endian.Little)
241+
builder.add_number(1)
242+
builder.add_string(b'test')
243+
builder.add_bits(self.bitstring)
244+
self.assertEqual(self.payload, builder.to_string())
245+
246+
def testPayloadDecoder(self):
247+
''' Test basic bit message encoding/decoding '''
248+
decoder = BcdPayloadDecoder(self.payload)
249+
self.assertEqual(1, decoder.decode_int())
250+
self.assertEqual(b'test', decoder.decode_string(4))
251+
self.assertEqual(self.bitstring, decoder.decode_bits())
252+
253+
#---------------------------------------------------------------------------#
254+
# unit tests
255+
#---------------------------------------------------------------------------#
256+
if __name__ == "__main__":
257+
unittest.main()
258+
220259
#---------------------------------------------------------------------------#
221260
# Exported Identifiers
222261
#---------------------------------------------------------------------------#

examples/contrib/message-generator.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
The following is an example of how to generate example encoded messages
77
for the supplied modbus format:
88
9-
* tcp - `./generate-messages.py -f tcp -m rx -b`
10-
* ascii - `./generate-messages.py -f ascii -m tx -a`
11-
* rtu - `./generate-messages.py -f rtu -m rx -b`
12-
* binary - `./generate-messages.py -f binary -m tx -b`
9+
* tcp - `./message-generator.py -f tcp -m rx -b`
10+
* ascii - `./message-generator.py -f ascii -m tx -a`
11+
* rtu - `./message-generator.py -f rtu -m rx -b`
12+
* binary - `./message-generator.py -f binary -m tx -b`
1313
'''
14+
from __future__ import print_function
15+
import binascii
1416
from optparse import OptionParser
1517
#--------------------------------------------------------------------------#
1618
# import all the available framers
@@ -165,11 +167,12 @@ def generate_messages(framer, options):
165167
messages = _request_messages if options.messages == 'tx' else _response_messages
166168
for message in messages:
167169
message = message(**_arguments)
168-
print "%-44s = " % message.__class__.__name__,
170+
print("%-44s = " % message.__class__.__name__, end = '')
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+
packet = binascii.b2a_hex(packet).decode('utf8') + '\n'
174+
else: packet = packet.decode()
175+
print(packet, end='') # because ascii ends with a \r\n
173176

174177

175178
#---------------------------------------------------------------------------#
@@ -213,9 +216,9 @@ def main():
213216
if option.debug:
214217
try:
215218
modbus_log.setLevel(logging.DEBUG)
216-
logging.basicConfig()
217-
except Exception, e:
218-
print "Logging is not supported on this system"
219+
logging.basicConfig()
220+
except Exception as e:
221+
print("Logging is not supported on this system")
219222

220223
framer = lookup = {
221224
'tcp': ModbusSocketFramer,

0 commit comments

Comments
 (0)