Advertisement
j0h

modbusRelay

j0h
Dec 24th, 2024
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.29 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. #eletechsup ETD8A12
  3. from pymodbus.client.serial import ModbusSerialClient as ModbusClient
  4. from pymodbus.exceptions import ModbusIOException
  5. from time import sleep
  6. '''
  7. Functions I care about:
  8. read_coils(address,count,slave)
  9. read_holding_registers(address,count,slave) # has no twin
  10. write_coil(address, value[bool],slave)
  11. write_registers(address,value,slave)
  12.  
  13. where address is the starting integer address,
  14. count is the number of addresses to do a function to,
  15. and slave is the slave ID.
  16.  
  17. other notes: rtu is the default, of the serial class,
  18. but every time I try to specify it directly, it chokes.
  19. '''
  20.  
  21. #current issue: wtf is the baud? Well, the docs suggest 11520, but I dont read errors at 9600
  22. NReg = 10
  23. # Configure Modbus client
  24. client = ModbusClient(
  25. #    method="rtu",
  26.     port="/dev/ttyUSB0",
  27.     baudrate=9600,
  28.     #baudrate=115200,
  29.     #baudrate=19200,
  30.     parity="N",
  31.     stopbits=1,
  32.     bytesize=8,
  33.     timeout=1
  34. )
  35.  
  36. # Connect to the device and read holding registers
  37. if client.connect():
  38.     print("Connected to Modbus device.")
  39.     sleep(0.1)
  40.     # Iterate through registers
  41.     for register in range(1, NReg):  # Example: Registers 1 to 10
  42.         try:
  43.             result = client.read_holding_registers(address=register, count=1, slave=1)
  44.             if not result.isError():
  45.                 print(f"Register {register}: {result.registers[0]}")
  46.             else:
  47.                 print(f"Error reading register {register}: {result}")
  48.         except ModbusIOException as e:
  49.             print(f"Modbus IO Exception at register {register}: {e}")
  50.     client.close()
  51. else:
  52.     print("Failed to connect to Modbus device.")
  53. #write registers    
  54. client.connect()
  55. result = client.readwrite_registers(address=0, value=10, slave=1) #write 10 registers starting at 0
  56.  
  57. # Write coils
  58. if client.connect():
  59.     print("Connected to Modbus device.")
  60.     sleep(0.1)
  61.     # Iterate through registers
  62.     for register in range(1, NReg):  # Example: Registers 1 to 10
  63.         try:
  64.             #result = client.write_holding_registers(address=register, count=1, slave=1)
  65.             result = client.write_register(address=register, value=1, slave=1)
  66.             if not result.isError():
  67.                 print(f"Register {register}: {result.registers[0]}")
  68.             else:
  69.                 print(f"Error reading register {register}: {result}")
  70.         except ModbusIOException as e:
  71.             print(f"Modbus IO Exception at register {register}: {e}")
  72.     client.close()
  73. else:
  74.     print("Failed to connect to Modbus device.")
  75.  
  76. print("###### should be all ONES now #######")
  77. client.connect()
  78. # Connect to the device
  79. if client.connect():
  80.     print("Connected to Modbus device.")
  81.     sleep(0.1)
  82.     # Iterate through registers
  83.     for register in range(1, NReg):  # Example: Registers 1 to 10
  84.         try:
  85.             result = client.read_holding_registers(address=register, count=1, slave=1)
  86.             if not result.isError():
  87.                 print(f"Register {register}: {result.registers[0]}")
  88.             else:
  89.                 print(f"Error reading register {register}: {result}")
  90.         except ModbusIOException as e:
  91.             print(f"Modbus IO Exception at register {register}: {e}")
  92.     client.close()
  93. else:
  94.     print("Failed to connect to Modbus device.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement