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 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.conf import config
  8. serial_conn = None
  9. def connect():
  10. global serial_conn
  11. if serial_conn is None:
  12. print('Connecting to serial port *{}*'.format(
  13. config.get('heatpump', 'serial_port')))
  14. serial_conn = Serial(
  15. port=config.get('heatpump', 'serial_port'),
  16. baudrate=config.get('heatpump', 'baudrate'),
  17. bytesize=8,
  18. parity='N',
  19. stopbits=1)
  20. if serial_conn.open is False:
  21. print('Opening serial port')
  22. serial_conn.open()
  23. return serial_conn
  24. def read_coils(start, end):
  25. global serial_con
  26. connect()
  27. res = []
  28. try:
  29. for address in range(start, end + 1, 125):
  30. qty = 125 if (end - address) >= 125 else (end - address)
  31. if not qty:
  32. break
  33. print(start, end, address, qty)
  34. req_adu = rtu.read_coils(
  35. slave_id=1,
  36. starting_address=address,
  37. quantity=qty)
  38. resp = rtu.send_message(req_adu, serial_conn)
  39. res.extend(resp)
  40. except umodbus.exceptions.IllegalDataAddressError as e:
  41. print(e)
  42. return res
  43. def read_holding_registers(start, end):
  44. global serial_conn
  45. connect()
  46. res = []
  47. try:
  48. for address in range(start, end + 1, 125):
  49. qty = 125 if (end - address) >= 125 else (end - address)
  50. if not qty:
  51. break
  52. print(start, end, address, qty)
  53. req_adu = rtu.read_holding_registers(
  54. slave_id=1,
  55. starting_address=address,
  56. quantity=qty)
  57. resp = rtu.send_message(req_adu, serial_conn)
  58. res.extend(resp)
  59. except umodbus.exceptions.IllegalDataAddressError as e:
  60. print(e)
  61. return res
  62. if __name__ == '__main__':
  63. resp = read_holding_registers(1, 5000)
  64. #pprint(resp)
  65. print(len(resp))
  66. resp = read_coils(1, 2000)
  67. #pprint(resp)
  68. print(len(resp))