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.3KB

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