Projet de remplacement du "RPiPasserelle" d'Otec.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

modbus.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/usr/bin/env python3
  2. import os
  3. from serial import Serial
  4. from umodbus.client.serial import rtu
  5. import umodbus
  6. from pyheatpump.config import config
  7. from pyheatpump.logger import logger_init
  8. logger = logger_init()
  9. serial_conn = None
  10. def connect():
  11. global serial_conn
  12. if serial_conn is None:
  13. real_serial_port = os.path.realpath(
  14. config.get('heatpump', 'serial_port'))
  15. print('Connecting to serial port *{}* ({})'.format(
  16. config.get('heatpump', 'serial_port'),
  17. real_serial_port))
  18. serial_conn = Serial(
  19. port=real_serial_port,
  20. baudrate=config.get('heatpump', 'baudrate'),
  21. bytesize=8,
  22. parity='N',
  23. stopbits=1,
  24. timeout=20)
  25. if serial_conn.open is False:
  26. print('Opening serial port')
  27. serial_conn.open()
  28. return serial_conn
  29. def read_coils(start, end):
  30. global serial_con
  31. connect()
  32. res = []
  33. address = -1
  34. qty = -1
  35. logger.info('read_coils: [%s, %s]', start, end)
  36. try:
  37. for address in range(start, end + 1, 125):
  38. qty = 125 if (end - address) >= 125 else (end - address)
  39. if not qty:
  40. break
  41. req_adu = rtu.read_coils(
  42. slave_id=1,
  43. starting_address=address,
  44. quantity=qty)
  45. response = rtu.send_message(req_adu, serial_conn)
  46. res.extend(response)
  47. except umodbus.exceptions.IllegalDataAddressError as e:
  48. print(e)
  49. print(f'{address} {qty}')
  50. logger.debug('read_coils [%s, %s] result: %s', start, end, res)
  51. return res
  52. def read_holding_registers(start, end):
  53. global serial_conn
  54. connect()
  55. res = []
  56. address = -1
  57. qty = -1
  58. logger.debug('read_holding_registers [%s, %s]',
  59. start, end
  60. )
  61. try:
  62. for address in range(start, end + 1, 125):
  63. qty = 125 if (end - address) >= 125 else (end - address)
  64. if not qty:
  65. break
  66. req_adu = rtu.read_holding_registers(
  67. slave_id=1,
  68. starting_address=address,
  69. quantity=qty)
  70. response = rtu.send_message(req_adu, serial_conn)
  71. res.extend(response)
  72. except umodbus.exceptions.IllegalDataAddressError as e:
  73. print(e)
  74. print(f'{address} {qty}')
  75. logger.debug('read_holding_registers [%s, %s] result: %s', start, end, res)
  76. return res
  77. def write_coil(var_value):
  78. global serial_conn
  79. connect()
  80. logger.debug('write_coil address: %s, value: %s',
  81. var_value.address, var_value.value
  82. )
  83. try:
  84. req_adu = rtu.write_single_coil(
  85. slave_id=1,
  86. address=var_value.address,
  87. value=var_value.value)
  88. response = rtu.send_message(req_adu, serial_conn)
  89. logger.debug('write_coil address: %s, response: %s',
  90. var_value.address, response)
  91. if response != var_value.value:
  92. return False
  93. return True
  94. except Exception as e:
  95. raise e
  96. def write_holding_register(var_value):
  97. global serial_conn
  98. connect()
  99. logger.debug('write_holding_register address: %s, value: %s',
  100. var_value.address, var_value.value
  101. )
  102. try:
  103. req_adu = rtu.write_single_register(
  104. slave_id=1,
  105. address=var_value.address,
  106. value=var_value.value)
  107. response = rtu.send_message(req_adu, serial_conn)
  108. logger.debug('write_holding_register, addres: %s, response: %s',
  109. var_value.address, response)
  110. if response != var_value.value:
  111. return False
  112. return True
  113. except Exception as e:
  114. raise e
  115. if __name__ == '__main__':
  116. response = read_holding_registers(1, 10)
  117. print(len(response))
  118. response = read_coils(90, 100)
  119. print(len(response))
  120. """
  121. Example codeo
  122. write_coil(VariableValue(**{
  123. 'type':'D',
  124. 'address':91,
  125. 'value':1}))
  126. resp = read_coils(91, 93)
  127. print(f'91 : {response[0]} - 92 : {response[1]}')
  128. write_coil(VariableValue(**{
  129. 'type':'D',
  130. 'address':91,
  131. 'value':0}))
  132. response = read_coils(91, 93)
  133. print(f'91 : {response[0]} - 92 : {response[1]}')
  134. response = read_holding_registers(240, 242)
  135. print(response)
  136. write_holding_register(VariableValue(**{
  137. 'type':'D',
  138. 'address':91,
  139. 'value':1})
  140. """