Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pymodbus 2.2.0 #375

Merged
merged 24 commits into from
Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a2d79e1
#357 Support registration of custom requests
dhoomakethu Dec 3, 2018
f07dcee
#368 Fixes write to broadcast address
mdmuhlbaier Jan 10, 2019
2e169b6
Bump version to 2.2.0
dhoomakethu Jan 14, 2019
c410f5b
Merge branch '#357-Custom-Function' into pymodbus-2.2.0
dhoomakethu Jan 14, 2019
826240b
Fix #371 pymodbus repl on python3
dhoomakethu Jan 14, 2019
6e72e44
1. Fix tornado async serial client `TypeError` while processing incom…
dhoomakethu Jan 15, 2019
18fe036
[fix v3] poprawa sprawdzania timeout
MarekLew Jan 6, 2019
964a565
Release candidate for pymodbus 2.2.0
dhoomakethu Jan 16, 2019
6960d9c
Fix #377 when invalid port is supplied and minor updates in logging
dhoomakethu Jan 26, 2019
249ad8f
Merge remote-tracking branch 'upstream/dev' into dev
muhlbaier Jan 31, 2019
60aca50
#368 adds broadcast support for sync client and server
muhlbaier Jan 31, 2019
e07e01e
#368 Fixes minor bug in broadcast support code
muhlbaier Jan 31, 2019
5030514
Fixed erronous CRC handling
JStrbg Jan 16, 2019
1a24c1d
Merge branch 'pull/372' into pymodbus-2.2.0
dhoomakethu Feb 11, 2019
e5c2615
Update Changelog
dhoomakethu Feb 11, 2019
f66f464
Fix test coverage
dhoomakethu Feb 11, 2019
7650421
Fix #387 Transactions failing on 2.2.0rc2.
dhoomakethu Feb 16, 2019
6233706
Task Cancellation and CRC Errors
pazzarpj Dec 12, 2018
89d3909
Cherry pick commit from PR #367 , Update changelog , bump version to …
dhoomakethu Mar 6, 2019
e4f202c
#389 Support passing all serial port parameters to asynchronous server
dhoomakethu Mar 23, 2019
3f48b90
Fix BinaryPayloadDecoder and Builder wrt to coils
dhoomakethu Apr 18, 2019
c919fe4
Misc updates, bump version to 2.2.0
dhoomakethu Apr 18, 2019
3d34820
ReportSlaveIdResponse now tries to get slave id based on server ident…
dhoomakethu Apr 18, 2019
2d8d467
Update missing bcrypt requirement for testing
dhoomakethu Apr 18, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[run]
omit =
pymodbus/repl/*
pymodbus/repl/*
pymodbus/internal/*
34 changes: 33 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
Version 2.1.1
Version 2.2.0
-----------------------------------------------------------
**NOTE: Supports python 3.7, async client is now moved to pymodbus/client/asychronous**
```
from pymodbus.client.asynchronous import ModbusTcpClient
```

* Support Python 3.7
* Fix to task cancellations and CRC errors for async serial clients.
* Fix passing serial settings to asynchronous serial server.
* Fix `AttributeError` when setting `interCharTimeout` for serial clients.
* Provide an option to disable inter char timeouts with Modbus RTU.
* Add support to register custom requests in clients and server instances.
* Fix read timeout calculation in ModbusTCP.
* Fix SQLDbcontext always returning InvalidAddress error.
* Fix SQLDbcontext update failure
* Fix Binary payload example for endianess.
* Fix BinaryPayloadDecoder.to_coils and BinaryPayloadBuilder.fromCoils methods.
* Fix tornado async serial client `TypeError` while processing incoming packet.
* Fix erroneous CRC handling in Modbus RTU framer.
* Support broadcasting in Modbus Client and Servers (sync).
* Fix asyncio examples.
* Improved logging in Modbus Server .
* ReportSlaveIdRequest would fetch information from Device identity instead of hardcoded `Pymodbus`.
* Fix regression introduced in 2.2.0rc2 (Modbus sync client transaction failing)
* Minor update in factory.py, now server logs prints received request instead of only function code

```
# Now
DEBUG:pymodbus.factory:Factory Request[ReadInputRegistersRequest: 4]
# Before
DEBUG:pymodbus.factory:Factory Request[4]

```


Version 2.1.0
-----------------------------------------------------------
Expand Down
5 changes: 1 addition & 4 deletions examples/common/async_asyncio_serial_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ async def start_async_test(client):
# which defaults to `0x00`
# ----------------------------------------------------------------------- #
try:
log.debug("Reading Coils")
rr = client.read_coils(1, 1, unit=UNIT)

# ----------------------------------------------------------------------- #
# example requests
# ----------------------------------------------------------------------- #
Expand Down Expand Up @@ -137,7 +134,7 @@ async def start_async_test(client):
# socat -d -d PTY,link=/tmp/ptyp0,raw,echo=0,ispeed=9600 PTY,
# link=/tmp/ttyp0,raw,echo=0,ospeed=9600
loop, client = ModbusClient(schedulers.ASYNC_IO, port='/tmp/ptyp0',
baudrate=9600, timeout=2, method="rtu")
baudrate=9600, method="rtu")
loop.run_until_complete(start_async_test(client.protocol))
loop.close()

5 changes: 4 additions & 1 deletion examples/common/async_tornado_client_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
# configure the client logging
# ---------------------------------------------------------------------------#
import logging
logging.basicConfig()

FORMAT = ('%(asctime)-15s %(threadName)-15s'
' %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
logging.basicConfig(format=FORMAT)
log = logging.getLogger()
log.setLevel(logging.DEBUG)

Expand Down
8 changes: 6 additions & 2 deletions examples/common/asynchronous_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from pymodbus.transaction import (ModbusRtuFramer,
ModbusAsciiFramer,
ModbusBinaryFramer)
from custom_message import CustomModbusRequest

# --------------------------------------------------------------------------- #
# configure the service logging
Expand Down Expand Up @@ -92,6 +93,8 @@ def run_async_server():
co=ModbusSequentialDataBlock(0, [17]*100),
hr=ModbusSequentialDataBlock(0, [17]*100),
ir=ModbusSequentialDataBlock(0, [17]*100))
store.register(CustomModbusRequest.function_code, 'cm',
ModbusSequentialDataBlock(0, [17] * 100))
context = ModbusServerContext(slaves=store, single=True)

# ----------------------------------------------------------------------- #
Expand All @@ -105,15 +108,16 @@ def run_async_server():
identity.VendorUrl = 'http://github.com/bashwork/pymodbus/'
identity.ProductName = 'Pymodbus Server'
identity.ModelName = 'Pymodbus Server'
identity.MajorMinorRevision = '1.5'
identity.MajorMinorRevision = '2.2.0'

# ----------------------------------------------------------------------- #
# run the server you want
# ----------------------------------------------------------------------- #

# TCP Server

StartTcpServer(context, identity=identity, address=("localhost", 5020))
StartTcpServer(context, identity=identity, address=("localhost", 5020),
custom_functions=[CustomModbusRequest])

# TCP Server with deferred reactor run

Expand Down
2 changes: 1 addition & 1 deletion examples/common/callback_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def run_callback_server():
identity.VendorUrl = 'http://github.com/bashwork/pymodbus/'
identity.ProductName = 'pymodbus Server'
identity.ModelName = 'pymodbus Server'
identity.MajorMinorRevision = '1.0'
identity.MajorMinorRevision = '2.2.0'

# ----------------------------------------------------------------------- #
# run the server you want
Expand Down
2 changes: 1 addition & 1 deletion examples/common/custom_datablock.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def run_custom_db_server():
identity.VendorUrl = 'http://github.com/bashwork/pymodbus/'
identity.ProductName = 'pymodbus Server'
identity.ModelName = 'pymodbus Server'
identity.MajorMinorRevision = '1.0'
identity.MajorMinorRevision = '2.2.0'

# ----------------------------------------------------------------------- #
# run the server you want
Expand Down
46 changes: 37 additions & 9 deletions examples/common/custom_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from pymodbus.pdu import ModbusRequest, ModbusResponse, ModbusExceptions
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
from pymodbus.bit_read_message import ReadCoilsRequest
from pymodbus.compat import int2byte, byte2int
# --------------------------------------------------------------------------- #
# configure the client logging
# --------------------------------------------------------------------------- #
Expand All @@ -40,15 +41,41 @@


class CustomModbusResponse(ModbusResponse):
pass
function_code = 55
_rtu_byte_count_pos = 2

def __init__(self, values=None, **kwargs):
ModbusResponse.__init__(self, **kwargs)
self.values = values or []

def encode(self):
""" Encodes response pdu

:returns: The encoded packet message
"""
result = int2byte(len(self.values) * 2)
for register in self.values:
result += struct.pack('>H', register)
return result

def decode(self, data):
""" Decodes response pdu

:param data: The packet data to decode
"""
byte_count = byte2int(data[0])
self.values = []
for i in range(1, byte_count + 1, 2):
self.values.append(struct.unpack('>H', data[i:i + 2])[0])


class CustomModbusRequest(ModbusRequest):

function_code = 1
function_code = 55
_rtu_frame_size = 8

def __init__(self, address):
ModbusRequest.__init__(self)
def __init__(self, address=None, **kwargs):
ModbusRequest.__init__(self, **kwargs)
self.address = address
self.count = 16

Expand All @@ -74,12 +101,12 @@ def execute(self, context):

class Read16CoilsRequest(ReadCoilsRequest):

def __init__(self, address):
def __init__(self, address, **kwargs):
""" Initializes a new instance

:param address: The address to start reading from
"""
ReadCoilsRequest.__init__(self, address, 16)
ReadCoilsRequest.__init__(self, address, 16, **kwargs)

# --------------------------------------------------------------------------- #
# execute the request with your client
Expand All @@ -90,7 +117,8 @@ def __init__(self, address):


if __name__ == "__main__":
with ModbusClient('127.0.0.1') as client:
request = CustomModbusRequest(0)
with ModbusClient(host='localhost', port=5020) as client:
client.register(CustomModbusResponse)
request = CustomModbusRequest(1, unit=1)
result = client.execute(request)
print(result)
print(result.values)
124 changes: 124 additions & 0 deletions examples/common/custom_message_async_clients.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/usr/bin/env python
"""
Pymodbus Synchronous Client Examples
--------------------------------------------------------------------------

The following is an example of how to use the synchronous modbus client
implementation from pymodbus.

It should be noted that the client can also be used with
the guard construct that is available in python 2.5 and up::

with ModbusClient('127.0.0.1') as client:
result = client.read_coils(1,10)
print result
"""
import struct
# --------------------------------------------------------------------------- #
# import the various server implementations
# --------------------------------------------------------------------------- #
from pymodbus.pdu import ModbusRequest, ModbusResponse, ModbusExceptions
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
from pymodbus.bit_read_message import ReadCoilsRequest
from pymodbus.compat import int2byte, byte2int
# --------------------------------------------------------------------------- #
# configure the client logging
# --------------------------------------------------------------------------- #
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)

# --------------------------------------------------------------------------- #
# create your custom message
# --------------------------------------------------------------------------- #
# The following is simply a read coil request that always reads 16 coils.
# Since the function code is already registered with the decoder factory,
# this will be decoded as a read coil response. If you implement a new
# method that is not currently implemented, you must register the request
# and response with a ClientDecoder factory.
# --------------------------------------------------------------------------- #


class CustomModbusResponse(ModbusResponse):
function_code = 55
_rtu_byte_count_pos = 2

def __init__(self, values=None, **kwargs):
ModbusResponse.__init__(self, **kwargs)
self.values = values or []

def encode(self):
""" Encodes response pdu

:returns: The encoded packet message
"""
result = int2byte(len(self.values) * 2)
for register in self.values:
result += struct.pack('>H', register)
return result

def decode(self, data):
""" Decodes response pdu

:param data: The packet data to decode
"""
byte_count = byte2int(data[0])
self.values = []
for i in range(1, byte_count + 1, 2):
self.values.append(struct.unpack('>H', data[i:i + 2])[0])


class CustomModbusRequest(ModbusRequest):

function_code = 55
_rtu_frame_size = 8

def __init__(self, address=None, **kwargs):
ModbusRequest.__init__(self, **kwargs)
self.address = address
self.count = 16

def encode(self):
return struct.pack('>HH', self.address, self.count)

def decode(self, data):
self.address, self.count = struct.unpack('>HH', data)

def execute(self, context):
if not (1 <= self.count <= 0x7d0):
return self.doException(ModbusExceptions.IllegalValue)
if not context.validate(self.function_code, self.address, self.count):
return self.doException(ModbusExceptions.IllegalAddress)
values = context.getValues(self.function_code, self.address,
self.count)
return CustomModbusResponse(values)

# --------------------------------------------------------------------------- #
# This could also have been defined as
# --------------------------------------------------------------------------- #


class Read16CoilsRequest(ReadCoilsRequest):

def __init__(self, address, **kwargs):
""" Initializes a new instance

:param address: The address to start reading from
"""
ReadCoilsRequest.__init__(self, address, 16, **kwargs)

# --------------------------------------------------------------------------- #
# execute the request with your client
# --------------------------------------------------------------------------- #
# using the with context, the client will automatically be connected
# and closed when it leaves the current scope.
# --------------------------------------------------------------------------- #


if __name__ == "__main__":
with ModbusClient(host='localhost', port=5020) as client:
client.register(CustomModbusResponse)
request = CustomModbusRequest(1, unit=1)
result = client.execute(request)
print(result.values)
Loading