Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/ljean/modbus-tk
Browse files Browse the repository at this point in the history
  • Loading branch information
ljean committed Oct 3, 2019
2 parents 19dbe02 + 5d6b76a commit 4ea9dd5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -35,6 +35,7 @@ Features
------------------------------------
* Modbus TCP support for writing masters and slaves
* Modbus RTU support for writing masters and slaves (requires pyserial)
* Modbus RTU over TCP support
* Can be customized with hook mechanism (simulate errors, timeouts...)
* ready-to use simulator with RPC interface
* Defines very easily your own memory blocks
Expand Down
1 change: 1 addition & 0 deletions modbus_tk/__init__.py
Expand Up @@ -29,6 +29,7 @@
* Vincent Prince
* kcl93
* https://github.com/Slasktra
* travijuu
Please let us know if your name is missing!
Expand Down
2 changes: 2 additions & 0 deletions modbus_tk/hooks.py
Expand Up @@ -49,6 +49,8 @@ def install_hook(name, fct):
modbus_tcp.TcpServer.before_send((server, sock, response)) returns modified response or None
modbus_tcp.TcpServer.on_error((server, sock, excpt))
modbus_rtu_over_tcp.RtuOverTcpMaster.after_recv((master, response))
modbus.Master.before_send((master, request)) returns modified request or None
modbus.Master.after_send((master))
modbus.Master.after_recv((master, response)) returns modified response or None
Expand Down
38 changes: 38 additions & 0 deletions modbus_tk/modbus_rtu_over_tcp.py
@@ -0,0 +1,38 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Modbus TestKit: Implementation of Modbus protocol in python
(C)2009 - Luc Jean - luc.jean@gmail.com
(C)2009 - Apidev - http://www.apidev.fr
This is distributed under GNU LGPL license, see license.txt
"""

from modbus_tk.hooks import call_hooks
from modbus_tk.modbus_rtu import RtuQuery
from modbus_tk.modbus_tcp import TcpMaster
from modbus_tk.utils import to_data


class RtuOverTcpMaster(TcpMaster):
"""Subclass of TcpMaster. Implements the Modbus RTU over TCP MAC layer"""

def _recv(self, expected_length=-1):
"""Receive the response from the slave"""
response = to_data('')
length = 255
while len(response) < length:
rcv_byte = self._sock.recv(1)
if rcv_byte:
response += rcv_byte
if expected_length >= 0 and len(response) >= expected_length:
break
retval = call_hooks("modbus_rtu_over_tcp.RtuOverTcpMaster.after_recv", (self, response))
if retval is not None:
return retval
return response

def _make_query(self):
"""Returns an instance of a Query subclass implementing the modbus RTU protocol"""
return RtuQuery()

0 comments on commit 4ea9dd5

Please sign in to comment.