Skip to content

Commit 3b6487c

Browse files
authored
Merge pull request #2 from vinichou94/revert-1-poursuite_python3
Porting to Python3
2 parents 90eda37 + fe57ce7 commit 3b6487c

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

+746
-936
lines changed

.gitignore

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

CHANGELOG.rst

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

examples/common/asynchronous-client.py

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

3837
#---------------------------------------------------------------------------#
3938
# specify slave to query

examples/common/callback-server.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
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
1918

2019
#---------------------------------------------------------------------------#
2120
# import the python libraries we need
@@ -45,7 +44,7 @@ def __init__(self, devices, queue):
4544
self.devices = devices
4645
self.queue = queue
4746

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

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=502, framer=ModbusFramer)
42+
client = ModbusClient('localhost', port=5020, 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: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
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
1110

1211
#---------------------------------------------------------------------------#
1312
# configure the client logging
@@ -37,7 +36,7 @@
3736
# - an 8 bit bitstring [0,1,0,1,1,0,1,0]
3837
#---------------------------------------------------------------------------#
3938
builder = BinaryPayloadBuilder(endian=Endian.Little)
40-
builder.add_string(b'abcdefgh')
39+
builder.add_string('abcdefgh')
4140
builder.add_32bit_float(22.34)
4241
builder.add_16bit_uint(0x1234)
4342
builder.add_8bit_int(0x12)
@@ -65,17 +64,17 @@
6564
decoder = BinaryPayloadDecoder.fromRegisters(result.registers, endian=Endian.Little)
6665
decoded = {
6766
'string': decoder.decode_string(8),
68-
'float': decoder.decode_32bit_float(),
67+
'float': decoder.decode_32bit_float(),
6968
'16uint': decoder.decode_16bit_uint(),
70-
'8int': decoder.decode_8bit_int(),
71-
'bits': decoder.decode_bits(),
69+
'8int': decoder.decode_8bit_int(),
70+
'bits': decoder.decode_bits(),
7271
}
7372

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

8079
#---------------------------------------------------------------------------#
8180
# 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, port=5020)
57+
client = ModbusTcpClient(host)
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: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
that can be used in the pymodbus library. Below is a
77
simple binary coded decimal builder and decoder.
88
'''
9-
import unittest
109
from struct import pack, unpack
1110
from pymodbus.constants import Endian
1211
from pymodbus.interfaces import IPayloadBuilder
@@ -24,7 +23,7 @@ def convert_to_bcd(decimal):
2423
while decimal > 0:
2524
nibble = decimal % 10
2625
bcd += nibble << place
27-
decimal //= 10
26+
decimal /= 10
2827
place += 4
2928
return bcd
3029

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

80-
def to_string(self):
79+
def __str__(self):
8180
''' Return the payload buffer as a string
8281
8382
:returns: The payload buffer as a string
8483
'''
85-
return b''.join(self._payload)
84+
return ''.join(self._payload)
8685

8786
def reset(self):
8887
''' Reset the payload buffer
@@ -97,10 +96,10 @@ def build(self):
9796
9897
:returns: The payload buffer as a list
9998
'''
100-
string = self.to_string()
99+
string = str(self)
101100
length = len(string)
102-
string = string + (b'\x00' * (length % 2))
103-
return [string[i:i+2] for i in range(0, length, 2)]
101+
string = string + ('\x00' * (length % 2))
102+
return [string[i:i+2] for i in xrange(0, length, 2)]
104103

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

220219

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-
259220
#---------------------------------------------------------------------------#
260221
# Exported Identifiers
261222
#---------------------------------------------------------------------------#

examples/contrib/message-generator.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
The following is an example of how to generate example encoded messages
77
for the supplied modbus format:
88
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`
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`
1313
'''
14-
from __future__ import print_function
15-
import binascii
1614
from optparse import OptionParser
1715
#--------------------------------------------------------------------------#
1816
# import all the available framers
@@ -167,12 +165,11 @@ def generate_messages(framer, options):
167165
messages = _request_messages if options.messages == 'tx' else _response_messages
168166
for message in messages:
169167
message = message(**_arguments)
170-
print("%-44s = " % message.__class__.__name__, end = '')
168+
print "%-44s = " % message.__class__.__name__,
171169
packet = framer.buildPacket(message)
172170
if not options.ascii:
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
171+
packet = packet.encode('hex') + '\n'
172+
print packet, # because ascii ends with a \r\n
176173

177174

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

223220
framer = lookup = {
224221
'tcp': ModbusSocketFramer,

0 commit comments

Comments
 (0)