simulator_rpc_client.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 __future__ import print_function
  10. import socket
  11. import modbus_tk.defines
  12. class SimulatorRpcClient(object):
  13. """Make possible to send command to the modbus_tk.Simulator thanks to Remote Process Call"""
  14. def __init__(self, host="127.0.0.1", port=2711, timeout=0.5):
  15. """Constructor"""
  16. self.host = host
  17. self.port = port
  18. self.timeout = timeout
  19. def _rpc_call(self, query):
  20. """send a rpc call and return the result"""
  21. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  22. sock.settimeout(self.timeout)
  23. sock.connect((self.host, self.port))
  24. sock.send(query)
  25. response = sock.recv(1024)
  26. sock.close()
  27. return self._response_to_values(response.strip("\r\n"), query.split(" ")[0])
  28. def _response_to_values(self, response, command):
  29. """extract the return value from the response"""
  30. prefix = command + " done: "
  31. if response.find(prefix) == 0:
  32. return response[len(prefix):]
  33. else:
  34. raise Exception(response)
  35. def add_slave(self, slave_id):
  36. """add a new slave with the given id"""
  37. query = "add_slave %d" % (slave_id)
  38. return self._rpc_call(query)
  39. def remove_slave(self, slave_id):
  40. """add a new slave with the given id"""
  41. query = "remove_slave %d" % (slave_id)
  42. return self._rpc_call(query)
  43. def remove_all_slaves(self):
  44. """add a new slave with the given id"""
  45. query = "remove_all_slaves"
  46. self._rpc_call(query)
  47. def has_slave(self, slave_id):
  48. """add a new slave with the given id"""
  49. query = "has_slave %d" % (slave_id)
  50. if "1" == self._rpc_call(query):
  51. return True
  52. return False
  53. def add_block(self, slave_id, block_name, block_type, starting_address, length):
  54. """add a new modbus block into the slave"""
  55. query = "add_block %d %s %d %d %d" % (slave_id, block_name, block_type, starting_address, length)
  56. return self._rpc_call(query)
  57. def remove_block(self, slave_id, block_name):
  58. """remove the modbus block with the given name and slave"""
  59. query = "remove_block %d %s" % (slave_id, block_name)
  60. self._rpc_call(query)
  61. def remove_all_blocks(self, slave_id):
  62. """remove the modbus block with the given name and slave"""
  63. query = "remove_all_blocks %d" % (slave_id)
  64. self._rpc_call(query)
  65. def set_values(self, slave_id, block_name, address, values):
  66. """set the values of registers"""
  67. query = "set_values %d %s %d" % (slave_id, block_name, address)
  68. for val in values:
  69. query += (" " + str(val))
  70. return self._rpc_call(query)
  71. def get_values(self, slave_id, block_name, address, length):
  72. """get the values of some registers"""
  73. query = "get_values %d %s %d %d" % (slave_id, block_name, address, length)
  74. ret_values = self._rpc_call(query)
  75. return tuple([int(val) for val in ret_values.split(' ')])
  76. def install_hook(self, hook_name, fct_name):
  77. """add a hook"""
  78. query = "install_hook %s %s" % (hook_name, fct_name)
  79. self._rpc_call(query)
  80. def uninstall_hook(self, hook_name, fct_name=""):
  81. """remove a hook"""
  82. query = "uninstall_hook %s %s" % (hook_name, fct_name)
  83. self._rpc_call(query)
  84. if __name__ == "__main__":
  85. modbus_simu = SimulatorRpcClient()
  86. modbus_simu.remove_all_slaves()
  87. print(modbus_simu.add_slave(12))
  88. print(modbus_simu.add_block(12, "toto", modbus_tk.defines.COILS, 0, 100))
  89. print(modbus_simu.set_values(12, "toto", 0, [5, 8, 7, 6, 41]))
  90. print(modbus_simu.get_values(12, "toto", 0, 5))
  91. print(modbus_simu.set_values(12, "toto", 2, [9]))
  92. print(modbus_simu.get_values(12, "toto", 0, 5))
  93. print(modbus_simu.has_slave(12))
  94. print(modbus_simu.add_block(12, "titi", modbus_tk.defines.COILS, 100, 100))
  95. print(modbus_simu.remove_block(12, "titi"))
  96. print(modbus_simu.add_slave(25))
  97. print(modbus_simu.has_slave(25))
  98. print(modbus_simu.add_slave(28))
  99. modbus_simu.remove_slave(25)
  100. print(modbus_simu.has_slave(25))
  101. print(modbus_simu.has_slave(28))
  102. modbus_simu.remove_all_blocks(12)
  103. modbus_simu.remove_all_slaves()
  104. print(modbus_simu.has_slave(28))
  105. print(modbus_simu.has_slave(12))
  106. modbus_simu.install_hook("modbus.Server.before_handle_request", "print_me")