Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Sep 9, 2023
2 parents 46bcb7a + 1c7dd05 commit e5a82d5
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 8 deletions.
4 changes: 4 additions & 0 deletions API_changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ PyModbus - API changes.
Version 3.6.0 (future)
-------------

-------------
Version 3.5.2
-------------
No changes.

-------------
Version 3.5.1
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
All these version were not possible without volunteers!
`AUTHORS` contains a complete list for each major version.

version 3.5.2
----------------------------------------------------------
- server tracer example. (#1773)
- sync connect missing. (#1772)
- simulator future problem. (#1771)

version 3.5.1
----------------------------------------------------------
- Always close socket on error (reset_sock). (#1767)
Expand Down
7 changes: 4 additions & 3 deletions MAKE_RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ Making a release.
------------------------------------------------------------
Prepare/make release on dev.
------------------------------------------------------------
* Make pull request "prepare v3.4.x", with the following:
* Make pull request "prepare v3.5.x", with the following:
* Update pymodbus/__init__.py with version number (__version__ X.Y.Zpre)
* Update README.rst "Supported versions"
* Control / Update API_changes.rst
* Update CHANGELOG.rst
* Add commits from last release, but kselectively !
* Add commits from last release, but selectively !
git log --oneline v3.5.1..HEAD > commit.log
git log --pretty="%an" v3.5.1..HEAD | sort -uf >> AUTHORS
git log --pretty="%an" v3.0.0..HEAD | sort -uf >> AUTHORS
update AUTHORS
* Commit, push and merge.
* Checkout master locally
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Supported versions

Version `2.5.3 <https://github.com/pymodbus-dev/pymodbus/releases/tag/v2.5.3>`_ is the last 2.x release (python >= 2.7, no longer supported).

Version `3.5.0 <https://github.com/pymodbus-dev/pymodbus/releases/tag/v3.5.0>`_ is the current release (Tested with Python >= 3.8).
Version `3.5.2 <https://github.com/pymodbus-dev/pymodbus/releases/tag/v3.5.2>`_ is the current release (Tested with Python >= 3.8).

.. important::
All API changes after 3.0.0 are documented in `API_changes.rst <https://github.com/pymodbus-dev/pymodbus/blob/dev/API_changes.rst>`_
Expand Down
8 changes: 8 additions & 0 deletions doc/source/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ Callback Server example
^^^^^^^^^^^^^^^^^^^^^^^
.. literalinclude:: ../../examples/server_callback.py

Server tracer example
^^^^^^^^^^^^^^^^^^^^^
.. literalinclude:: ../../examples/server_hook.py

Custom Message client
^^^^^^^^^^^^^^^^^^^^^
.. literalinclude:: ../../examples/client_custom_msg.py
Expand Down Expand Up @@ -107,6 +111,10 @@ Examples contributions
These examples are supplied by users of pymodbus.
The pymodbus team thanks for sharing the examples.

Solar
^^^^^
.. literalinclude:: ../../examples/contrib/solar.py

Redis datastore
^^^^^^^^^^^^^^^
.. literalinclude:: ../../examples/contrib/redis_datastore.py
Expand Down
87 changes: 87 additions & 0 deletions examples/server_hook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env python3
"""Pymodbus Server With request/response manipulator.
This is an example of using the builtin request/response tracer to
manipulate the messages to/from the modbus server
"""
import asyncio
import logging

from pymodbus import pymodbus_apply_logging_config
from pymodbus.datastore import (
ModbusSequentialDataBlock,
ModbusServerContext,
ModbusSlaveContext,
)
from pymodbus.server import ModbusTcpServer
from pymodbus.transaction import ModbusSocketFramer


class Manipulator:
"""A Class to run the server.
Using a class allows the easy use of global variables, but
are not strictly needed
"""

message_count: int = 1
server: ModbusTcpServer = None

def server_request_tracer(self, request, *_addr):
"""Trace requests.
All server requests passes this filter before being handled.
"""
print(f"---> REQUEST: {request}")

def server_response_manipulator(self, response):
"""Manipulate responses.
All server responses passes this filter before being sent.
The filter returns:
- response, either original or modified
- skip_encoding, signals whether or not to encode the response
"""
if not self.message_count:
print(f"---> RESPONSE: {response}")
self.message_count = 3
else:
print("---> RESPONSE: NONE")
response.should_respond = False
self.message_count -= 1
return response, False

async def setup(self):
"""Prepare server."""
pymodbus_apply_logging_config(logging.DEBUG)
datablock = ModbusSequentialDataBlock(0x00, [17] * 100)
context = ModbusServerContext(
slaves=ModbusSlaveContext(
di=datablock, co=datablock, hr=datablock, ir=datablock
),
single=True,
)
self.server = ModbusTcpServer(
context,
ModbusSocketFramer,
None,
("127.0.0.1", 5020),
request_tracer=self.server_request_tracer,
response_manipulator=self.server_response_manipulator,
)

async def run(self):
"""Attach Run server"""
await self.server.serve_forever()


async def main():
"""Run example."""
server = Manipulator()
await server.setup()
await server.run()


if __name__ == "__main__":
asyncio.run(main(), debug=True) # pragma: no cover
2 changes: 1 addition & 1 deletion pymodbus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
from pymodbus.logging import pymodbus_apply_logging_config


__version__ = "3.5.1"
__version__ = "3.5.2"
__version_full__ = f"[pymodbus, version {__version__}]"
2 changes: 1 addition & 1 deletion pymodbus/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def execute(self, request: ModbusRequest = None) -> ModbusResponse:
:raises ConnectionException: Check exception text.
"""
if self.use_sync:
if not self.connected:
if not self.connect():
raise ConnectionException(f"Failed to connect[{self!s}]")
return self.transaction.execute(request)
if not self.transport:
Expand Down
3 changes: 1 addition & 2 deletions pymodbus/server/simulator/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@ async def run_forever(self, only_start=False):
Log.info("HTTP server started on ({}:{})", self.http_host, self.http_port)
if only_start:
return
while True:
await self.serving()
await self.serving

async def stop(self):
"""Stop modbus and http servers."""
Expand Down

0 comments on commit e5a82d5

Please sign in to comment.