Projet de remplacement du "RPiPasserelle" d'Otec.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

modbus.py 4.2KB

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