Projet de remplacement du "RPiPasserelle" d'Otec.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

modbus_get.py 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python
  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. s = Serial('/dev/ttyUSB0',19200)
  12. try:
  13. s.open()
  14. except SerialException:
  15. print('already open')
  16. BOOLEAN=1000
  17. FLOAT=500
  18. INT=1250
  19. try:
  20. res = {'A':{},'D':{},'I':{}}
  21. # digital - boolean
  22. req_adu = rtu.read_coils(
  23. slave_id=1,
  24. starting_address=1,
  25. quantity=1)
  26. resp = rtu.send_message(req_adu, s)
  27. address = -1
  28. try:
  29. for address in range(0, BOOLEAN, 1):
  30. res['D'][address+1] = (resp[address] > 0)
  31. except KeyError as e:
  32. print(e)
  33. res['D'][address] = False
  34. except IndexError:
  35. print(address)
  36. # analog - float
  37. for address in range(0, FLOAT, 125):
  38. req_adu = rtu.read_holding_registers(
  39. slave_id=1,
  40. starting_address=1+address,
  41. quantity=125)
  42. resp = rtu.send_message(req_adu, s)
  43. for n in range(125):
  44. try:
  45. res['A'][address + n] = int(resp[n]) / 10
  46. except Exception as e:
  47. print(e)
  48. res['A'][address + n] = 0.
  49. #res['A'][ str(address + var) ] = int(resp[var]) / 10.
  50. # integer - int
  51. for address in range(1, INT, 125):
  52. req_adu = rtu.read_coils(
  53. slave_id=1,
  54. starting_address=address+5002,
  55. quantity=125)
  56. resp = rtu.send_message(req_adu, s)
  57. for n in range(125):
  58. try:
  59. res['I'][address + n] = int(resp[n])
  60. except KeyError as e:
  61. print(e)
  62. res['I'][address + n] = 0
  63. except KeyboardInterrupt:
  64. print("Quitting")
  65. pass
  66. s.close()
  67. sys.exit(0)