modbus_rtu_over_tcp.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Modbus TestKit: Implementation of Modbus protocol in python
  5. (C)2009 - Luc Jean - luc.jean@gmail.com
  6. (C)2009 - Apidev - http://www.apidev.fr
  7. This is distributed under GNU LGPL license, see license.txt
  8. """
  9. from modbus_tk.hooks import call_hooks
  10. from modbus_tk.modbus_rtu import RtuQuery
  11. from modbus_tk.modbus_tcp import TcpMaster
  12. from modbus_tk.utils import to_data
  13. class RtuOverTcpMaster(TcpMaster):
  14. """Subclass of TcpMaster. Implements the Modbus RTU over TCP MAC layer"""
  15. def _recv(self, expected_length=-1):
  16. """Receive the response from the slave"""
  17. response = to_data('')
  18. length = 255
  19. while len(response) < length:
  20. rcv_byte = self._sock.recv(1)
  21. if rcv_byte:
  22. response += rcv_byte
  23. if expected_length >= 0 and len(response) >= expected_length:
  24. break
  25. retval = call_hooks("modbus_rtu_over_tcp.RtuOverTcpMaster.after_recv", (self, response))
  26. if retval is not None:
  27. return retval
  28. return response
  29. def _make_query(self):
  30. """Returns an instance of a Query subclass implementing the modbus RTU protocol"""
  31. return RtuQuery()