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_get.py 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python3
  2. # scripts/examples/simple_tcp_client.py
  3. import fcntl
  4. import sys
  5. import socket
  6. import struct
  7. from serial import Serial
  8. from serial.serialutil import SerialException
  9. from umodbus.client.serial import rtu
  10. import json
  11. from pyheatpump.db import sql
  12. from pyheatpump.conf import config
  13. config.set('heatpump', 'database', '/home/emixam/src/otec/pyHeatpump/db/pyheatpump.db')
  14. s = Serial('/dev/ttyUSB0',19200)
  15. try:
  16. s.open()
  17. except SerialException:
  18. print('already open')
  19. BOOLEAN=1000
  20. FLOAT=500
  21. INT=1250
  22. def read_digital():
  23. global req_adu
  24. global sql
  25. global s
  26. start_address, end_address = sql(
  27. "SELECT start_address, end_address FROM var_type WHERE slabel LIKE 'D'")
  28. res = []
  29. # digital - boolean
  30. req_adu = rtu.read_coils(
  31. slave_id=1,
  32. starting_address=1,
  33. quantity=1)
  34. resp = rtu.send_message(req_adu, s)
  35. address = -1
  36. try:
  37. for address in range(0, BOOLEAN, 1):
  38. res[address+1] = (resp[address] > 0)
  39. except KeyError as e:
  40. print(e)
  41. res[address] = False
  42. except IndexError:
  43. print(address)
  44. return res
  45. def read_analog():
  46. global req_adu
  47. global sql
  48. global s
  49. start_address, end_address = sql(
  50. """
  51. SELECT start_address, end_address FROM var_type
  52. WHERE slabel LIKE 'A';
  53. """
  54. )
  55. # analog - float
  56. for address in range(1, end_address - start_address, 125):
  57. req_adu = rtu.read_holding_registers(
  58. slave_id=1,
  59. starting_address=start_address + address,
  60. quantity=125)
  61. resp = rtu.send_message(req_adu, s)
  62. for n in range(125):
  63. try:
  64. res[address + n] = int(resp[n]) / 10
  65. except Exception as e:
  66. print(e)
  67. res[address + n] = 0.
  68. return res
  69. def read_int():
  70. global req_adu
  71. global sql
  72. global s
  73. res = []
  74. start_address, end_address = 1, 1000
  75. #next(sql(
  76. """
  77. SELECT start_address, end_address FROM var_type
  78. WHERE slabel LIKE 'I'
  79. """
  80. #))
  81. # integer - int
  82. req_adu = rtu.read_holding_registers(
  83. slave_id=1,
  84. starting_address=start_address,
  85. quantity=125)
  86. resp = rtu.send_message(req_adu, s)
  87. for r in resp:
  88. print(r)
  89. return res
  90. read_int()
  91. s.close()
  92. sys.exit(0)