Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- #eletechsup ETD8A12
- from pymodbus.client.serial import ModbusSerialClient as ModbusClient
- from pymodbus.exceptions import ModbusIOException
- from time import sleep
- '''
- Functions I care about:
- read_coils(address,count,slave)
- read_holding_registers(address,count,slave) # has no twin
- write_coil(address, value[bool],slave)
- write_registers(address,value,slave)
- where address is the starting integer address,
- count is the number of addresses to do a function to,
- and slave is the slave ID.
- other notes: rtu is the default, of the serial class,
- but every time I try to specify it directly, it chokes.
- '''
- #current issue: wtf is the baud? Well, the docs suggest 11520, but I dont read errors at 9600
- NReg = 10
- # Configure Modbus client
- client = ModbusClient(
- # method="rtu",
- port="/dev/ttyUSB0",
- baudrate=9600,
- #baudrate=115200,
- #baudrate=19200,
- parity="N",
- stopbits=1,
- bytesize=8,
- timeout=1
- )
- # Connect to the device and read holding registers
- if client.connect():
- print("Connected to Modbus device.")
- sleep(0.1)
- # Iterate through registers
- for register in range(1, NReg): # Example: Registers 1 to 10
- try:
- result = client.read_holding_registers(address=register, count=1, slave=1)
- if not result.isError():
- print(f"Register {register}: {result.registers[0]}")
- else:
- print(f"Error reading register {register}: {result}")
- except ModbusIOException as e:
- print(f"Modbus IO Exception at register {register}: {e}")
- client.close()
- else:
- print("Failed to connect to Modbus device.")
- #write registers
- client.connect()
- result = client.readwrite_registers(address=0, value=10, slave=1) #write 10 registers starting at 0
- # Write coils
- if client.connect():
- print("Connected to Modbus device.")
- sleep(0.1)
- # Iterate through registers
- for register in range(1, NReg): # Example: Registers 1 to 10
- try:
- #result = client.write_holding_registers(address=register, count=1, slave=1)
- result = client.write_register(address=register, value=1, slave=1)
- if not result.isError():
- print(f"Register {register}: {result.registers[0]}")
- else:
- print(f"Error reading register {register}: {result}")
- except ModbusIOException as e:
- print(f"Modbus IO Exception at register {register}: {e}")
- client.close()
- else:
- print("Failed to connect to Modbus device.")
- print("###### should be all ONES now #######")
- client.connect()
- # Connect to the device
- if client.connect():
- print("Connected to Modbus device.")
- sleep(0.1)
- # Iterate through registers
- for register in range(1, NReg): # Example: Registers 1 to 10
- try:
- result = client.read_holding_registers(address=register, count=1, slave=1)
- if not result.isError():
- print(f"Register {register}: {result.registers[0]}")
- else:
- print(f"Error reading register {register}: {result}")
- except ModbusIOException as e:
- print(f"Modbus IO Exception at register {register}: {e}")
- client.close()
- else:
- print("Failed to connect to Modbus device.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement