Skip to content

Commit a7e8623

Browse files
committed
1. #162, creating universal distribution for py2 and py3
2. Merge PR #152 , create compatible versions
1 parent 82524cf commit a7e8623

Some content is hidden

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

56 files changed

+817
-637
lines changed

.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ build/
44
dist/
55
pymodbus.egg-info/
66
.coverage
7-
<<<<<<< HEAD
87
.vscode
98
.idea
109
.noseids
11-
=======
12-
.idea/
1310

14-
>>>>>>> 7f1153560c70941a73c7c74725e61b380096c0a7
11+
.idea/

examples/common/modbus-payload.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#---------------------------------------------------------------------------#
2020
# We are going to use a simple client to send our requests
2121
#---------------------------------------------------------------------------#
22-
client = ModbusClient('127.0.0.1')
22+
client = ModbusClient('127.0.0.1', port=5020)
2323
client.connect()
2424

2525
#---------------------------------------------------------------------------#
@@ -43,7 +43,7 @@
4343
builder.add_bits([0,1,0,1,1,0,1,0])
4444
payload = builder.build()
4545
address = 0x01
46-
result = client.write_registers(address, payload, skip_encode=True)
46+
result = client.write_registers(address, payload, skip_encode=True, unit=1)
4747

4848
#---------------------------------------------------------------------------#
4949
# If you need to decode a collection of registers in a weird layout, the
@@ -60,7 +60,7 @@
6060
#---------------------------------------------------------------------------#
6161
address = 0x01
6262
count = 8
63-
result = client.read_input_registers(address, count)
63+
result = client.read_input_registers(address, count, unit=1)
6464
decoder = BinaryPayloadDecoder.fromRegisters(result.registers, endian=Endian.Little)
6565
decoded = {
6666
'string': decoder.decode_string(8),

examples/common/performance.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
from time import time
1414
from multiprocessing import log_to_stderr
1515
from pymodbus.client.sync import ModbusTcpClient
16+
from pymodbus.client.sync import ModbusSerialClient
1617

1718
#---------------------------------------------------------------------------#
1819
# choose between threads or processes
1920
#---------------------------------------------------------------------------#
2021
#from multiprocessing import Process as Worker
2122
from threading import Thread as Worker
22-
23+
from threading import Lock
24+
_thread_lock = Lock()
2325
#---------------------------------------------------------------------------#
2426
# initialize the test
2527
#---------------------------------------------------------------------------#
@@ -29,8 +31,8 @@
2931
# * cycles - the total number of requests to send
3032
# * host - the host to send the requests to
3133
#---------------------------------------------------------------------------#
32-
workers = 1
33-
cycles = 10000
34+
workers = 10
35+
cycles = 1000
3436
host = '127.0.0.1'
3537

3638

@@ -54,10 +56,12 @@ def single_client_test(host, cycles):
5456

5557
try:
5658
count = 0
57-
client = ModbusTcpClient(host)
59+
# client = ModbusTcpClient(host, port=5020)
60+
client = ModbusSerialClient(method="rtu", port="/dev/ttyp0", baudrate=9600)
5861
while count < cycles:
59-
result = client.read_holding_registers(10, 1).getRegister(0)
60-
count += 1
62+
with _thread_lock:
63+
result = client.read_holding_registers(10, 1, unit=1).getRegister(0)
64+
count += 1
6165
except: logger.exception("failed to run test successfully")
6266
logger.debug("finished worker: %d" % os.getpid())
6367

@@ -76,3 +80,4 @@ def single_client_test(host, cycles):
7680
any(p.join() for p in procs) # wait for the workers to finish
7781
stop = time()
7882
print "%d requests/second" % ((1.0 * cycles) / (stop - start))
83+
print "time taken to complete %s cycle by %s workers is %s seconds" % (cycles, workers, stop-start)

examples/common/synchronous-client-ext.py

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#---------------------------------------------------------------------------#
1111
# import the various server implementations
1212
#---------------------------------------------------------------------------#
13-
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
13+
# from pymodbus.client.sync import ModbusTcpClient as ModbusClient
1414
#from pymodbus.client.sync import ModbusUdpClient as ModbusClient
15-
#from pymodbus.client.sync import ModbusSerialClient as ModbusClient
15+
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
1616

1717
#---------------------------------------------------------------------------#
1818
# configure the client logging
@@ -32,7 +32,8 @@
3232
# It should be noted that you can supply an ipv4 or an ipv6 host address for
3333
# both the UDP and TCP clients.
3434
#---------------------------------------------------------------------------#
35-
client = ModbusClient('127.0.0.1')
35+
client = ModbusClient(method='rtu', port="/dev/ttyp0")
36+
# client = ModbusClient('127.0.0.1', port=5020)
3637
client.connect()
3738

3839
#---------------------------------------------------------------------------#
@@ -65,35 +66,35 @@
6566
#---------------------------------------------------------------------------#
6667
# information requests
6768
#---------------------------------------------------------------------------#
68-
rq = ReadDeviceInformationRequest()
69+
rq = ReadDeviceInformationRequest(unit=1)
6970
rr = client.execute(rq)
7071
#assert(rr == None) # not supported by reference
7172
assert(rr.function_code < 0x80) # test that we are not an error
72-
assert(rr.information[0] == 'proconX Pty Ltd') # test the vendor name
73-
assert(rr.information[1] == 'FT-MBSV') # test the product code
74-
assert(rr.information[2] == 'EXPERIMENTAL') # test the code revision
73+
assert(rr.information[0] == 'Pymodbus') # test the vendor name
74+
assert(rr.information[1] == 'PM') # test the product code
75+
assert(rr.information[2] == '1.0') # test the code revision
7576

76-
rq = ReportSlaveIdRequest()
77+
rq = ReportSlaveIdRequest(unit=1)
7778
rr = client.execute(rq)
78-
assert(rr == None) # not supported by reference
79+
# assert(rr == None) # not supported by reference
7980
#assert(rr.function_code < 0x80) # test that we are not an error
8081
#assert(rr.identifier == 0x00) # test the slave identifier
8182
#assert(rr.status == 0x00) # test that the status is ok
8283

83-
rq = ReadExceptionStatusRequest()
84+
rq = ReadExceptionStatusRequest(unit=1)
8485
rr = client.execute(rq)
8586
#assert(rr == None) # not supported by reference
86-
assert(rr.function_code < 0x80) # test that we are not an error
87-
assert(rr.status == 0x55) # test the status code
87+
#assert(rr.function_code < 0x80) # test that we are not an error
88+
#assert(rr.status == 0x55) # test the status code
8889

89-
rq = GetCommEventCounterRequest()
90+
rq = GetCommEventCounterRequest(unit=1)
9091
rr = client.execute(rq)
91-
assert(rr == None) # not supported by reference
92+
#assert(rr == None) # not supported by reference
9293
#assert(rr.function_code < 0x80) # test that we are not an error
9394
#assert(rr.status == True) # test the status code
9495
#assert(rr.count == 0x00) # test the status code
9596

96-
rq = GetCommEventLogRequest()
97+
rq = GetCommEventLogRequest(unit=1)
9798
rr = client.execute(rq)
9899
#assert(rr == None) # not supported by reference
99100
#assert(rr.function_code < 0x80) # test that we are not an error
@@ -105,68 +106,68 @@
105106
#---------------------------------------------------------------------------#
106107
# diagnostic requests
107108
#---------------------------------------------------------------------------#
108-
rq = ReturnQueryDataRequest()
109+
rq = ReturnQueryDataRequest(unit=1)
109110
rr = client.execute(rq)
110-
assert(rr == None) # not supported by reference
111+
# assert(rr == None) # not supported by reference
111112
#assert(rr.message[0] == 0x0000) # test the resulting message
112113

113-
rq = RestartCommunicationsOptionRequest()
114+
rq = RestartCommunicationsOptionRequest(unit=1)
114115
rr = client.execute(rq)
115116
#assert(rr == None) # not supported by reference
116117
#assert(rr.message == 0x0000) # test the resulting message
117118

118-
rq = ReturnDiagnosticRegisterRequest()
119+
rq = ReturnDiagnosticRegisterRequest(unit=1)
119120
rr = client.execute(rq)
120121
#assert(rr == None) # not supported by reference
121122

122-
rq = ChangeAsciiInputDelimiterRequest()
123+
rq = ChangeAsciiInputDelimiterRequest(unit=1)
123124
rr = client.execute(rq)
124125
#assert(rr == None) # not supported by reference
125126

126-
rq = ForceListenOnlyModeRequest()
127+
rq = ForceListenOnlyModeRequest(unit=1)
127128
client.execute(rq) # does not send a response
128129

129130
rq = ClearCountersRequest()
130131
rr = client.execute(rq)
131132
#assert(rr == None) # not supported by reference
132133

133-
rq = ReturnBusCommunicationErrorCountRequest()
134+
rq = ReturnBusCommunicationErrorCountRequest(unit=1)
134135
rr = client.execute(rq)
135136
#assert(rr == None) # not supported by reference
136137

137-
rq = ReturnBusExceptionErrorCountRequest()
138+
rq = ReturnBusExceptionErrorCountRequest(unit=1)
138139
rr = client.execute(rq)
139140
#assert(rr == None) # not supported by reference
140141

141-
rq = ReturnSlaveMessageCountRequest()
142+
rq = ReturnSlaveMessageCountRequest(unit=1)
142143
rr = client.execute(rq)
143144
#assert(rr == None) # not supported by reference
144145

145-
rq = ReturnSlaveNoResponseCountRequest()
146+
rq = ReturnSlaveNoResponseCountRequest(unit=1)
146147
rr = client.execute(rq)
147148
#assert(rr == None) # not supported by reference
148149

149-
rq = ReturnSlaveNAKCountRequest()
150+
rq = ReturnSlaveNAKCountRequest(unit=1)
150151
rr = client.execute(rq)
151152
#assert(rr == None) # not supported by reference
152153

153-
rq = ReturnSlaveBusyCountRequest()
154+
rq = ReturnSlaveBusyCountRequest(unit=1)
154155
rr = client.execute(rq)
155156
#assert(rr == None) # not supported by reference
156157

157-
rq = ReturnSlaveBusCharacterOverrunCountRequest()
158+
rq = ReturnSlaveBusCharacterOverrunCountRequest(unit=1)
158159
rr = client.execute(rq)
159160
#assert(rr == None) # not supported by reference
160161

161-
rq = ReturnIopOverrunCountRequest()
162+
rq = ReturnIopOverrunCountRequest(unit=1)
162163
rr = client.execute(rq)
163164
#assert(rr == None) # not supported by reference
164165

165-
rq = ClearOverrunCountRequest()
166+
rq = ClearOverrunCountRequest(unit=1)
166167
rr = client.execute(rq)
167168
#assert(rr == None) # not supported by reference
168169

169-
rq = GetClearModbusPlusRequest()
170+
rq = GetClearModbusPlusRequest(unit=1)
170171
rr = client.execute(rq)
171172
#assert(rr == None) # not supported by reference
172173

examples/common/synchronous-client.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#---------------------------------------------------------------------------#
1919
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
2020
#from pymodbus.client.sync import ModbusUdpClient as ModbusClient
21-
#from pymodbus.client.sync import ModbusSerialClient as ModbusClient
21+
# from pymodbus.client.sync import ModbusSerialClient as ModbusClient
2222

2323
#---------------------------------------------------------------------------#
2424
# configure the client logging
@@ -55,9 +55,9 @@
5555
#
5656
# client = ModbusClient('localhost', retries=3, retry_on_empty=True)
5757
#---------------------------------------------------------------------------#
58-
client = ModbusClient('localhost', port=502)
58+
client = ModbusClient('localhost', port=5020)
5959
#client = ModbusClient(method='ascii', port='/dev/pts/2', timeout=1)
60-
#client = ModbusClient(method='rtu', port='/dev/pts/2', timeout=1)
60+
# client = ModbusClient(method='rtu', port='/dev/ttyp0', timeout=1)
6161
client.connect()
6262

6363
#---------------------------------------------------------------------------#
@@ -67,7 +67,7 @@
6767
# individual request. This can be done by specifying the `unit` parameter
6868
# which defaults to `0x00`
6969
#---------------------------------------------------------------------------#
70-
rr = client.read_coils(1, 1, unit=0x02)
70+
rr = client.read_coils(1, 1, unit=0x01)
7171

7272
#---------------------------------------------------------------------------#
7373
# example requests
@@ -81,39 +81,46 @@
8181
# Keep both of these cases in mind when testing as the following will
8282
# _only_ pass with the supplied async modbus server (script supplied).
8383
#---------------------------------------------------------------------------#
84-
rq = client.write_coil(1, True)
85-
rr = client.read_coils(1,1)
84+
rq = client.write_coil(0, True, unit=1)
85+
rr = client.read_coils(0, 1, unit=1)
8686
assert(rq.function_code < 0x80) # test that we are not an error
8787
assert(rr.bits[0] == True) # test the expected value
8888

89-
rq = client.write_coils(1, [True]*8)
90-
rr = client.read_coils(1,8)
89+
rq = client.write_coils(1, [True]*8, unit=1)
90+
rr = client.read_coils(1, 8, unit=1)
9191
assert(rq.function_code < 0x80) # test that we are not an error
9292
assert(rr.bits == [True]*8) # test the expected value
9393

94-
rq = client.write_coils(1, [False]*8)
95-
rr = client.read_discrete_inputs(1,8)
94+
rq = client.write_coils(1, [False]*8, unit=1)
95+
rr = client.read_coils(1, 8, unit=1)
9696
assert(rq.function_code < 0x80) # test that we are not an error
9797
assert(rr.bits == [False]*8) # test the expected value
9898

99-
rq = client.write_register(1, 10)
100-
rr = client.read_holding_registers(1,1)
99+
100+
rr = client.read_discrete_inputs(0, 8, unit=1)
101+
assert(rq.function_code < 0x80) # test that we are not an error
102+
103+
rq = client.write_register(1, 10, unit=1)
104+
rr = client.read_holding_registers(1, 1, unit=1)
101105
assert(rq.function_code < 0x80) # test that we are not an error
102106
assert(rr.registers[0] == 10) # test the expected value
103107

104-
rq = client.write_registers(1, [10]*8)
105-
rr = client.read_input_registers(1,8)
108+
rq = client.write_registers(1, [10]*8, unit=1)
109+
rr = client.read_holding_registers(1, 8, unit=1)
106110
assert(rq.function_code < 0x80) # test that we are not an error
107111
assert(rr.registers == [10]*8) # test the expected value
108112

113+
rr = client.read_input_registers(1, 8, unit=1)
114+
assert(rq.function_code < 0x80) # test that we are not an error
115+
109116
arguments = {
110117
'read_address': 1,
111118
'read_count': 8,
112119
'write_address': 1,
113120
'write_registers': [20]*8,
114121
}
115-
rq = client.readwrite_registers(**arguments)
116-
rr = client.read_input_registers(1,8)
122+
rq = client.readwrite_registers(unit=1, **arguments)
123+
rr = client.read_holding_registers(1, 8, unit=1)
117124
assert(rq.function_code < 0x80) # test that we are not an error
118125
assert(rq.registers == [20]*8) # test the expected value
119126
assert(rr.registers == [20]*8) # test the expected value

examples/common/synchronous-server.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,16 @@
9696
identity = ModbusDeviceIdentification()
9797
identity.VendorName = 'Pymodbus'
9898
identity.ProductCode = 'PM'
99-
identity.VendorUrl = 'http://github.com/bashwork/pymodbus/'
99+
identity.VendorUrl = 'http://github.com/riptideio/pymodbus/'
100100
identity.ProductName = 'Pymodbus Server'
101101
identity.ModelName = 'Pymodbus Server'
102102
identity.MajorMinorRevision = '1.0'
103103

104-
#---------------------------------------------------------------------------#
104+
#---------------------------------------------------------------------------#
105105
# run the server you want
106106
#---------------------------------------------------------------------------#
107107
# Tcp:
108-
StartTcpServer(context, identity=identity, address=("localhost", 5020))
108+
# StartTcpServer(context, identity=identity, address=("localhost", 5020))
109109

110110
# Udp:
111111
#StartUdpServer(context, identity=identity, address=("localhost", 502))
@@ -114,4 +114,4 @@
114114
#StartSerialServer(context, identity=identity, port='/dev/pts/3', timeout=1)
115115

116116
# RTU:
117-
#StartSerialServer(context, framer=ModbusRtuFramer, identity=identity, port='/dev/pts/3', timeout=.005)
117+
StartSerialServer(context, framer=ModbusRtuFramer, identity=identity, port='/dev/ptyp0', timeout=.005, baudrate=9600)

0 commit comments

Comments
 (0)