pyhys / minimalmodbus

Easy-to-use Modbus RTU and Modbus ASCII implementation for Python.
Apache License 2.0
309 stars 145 forks source link

If I want to read different device addresses in the same serial port, can I do it? #129

Closed childeyouyu closed 8 months ago

childeyouyu commented 8 months ago

The code is as follows:

    for i in range(1, 6 * 2, 2):
        try:
            inst = minimalmodbus.Instrument('COM3', 1)
            inst.serial.baudrate = 9600
            inst.serial.parity = "N"
            inst.serial.bytesize = 8
            inst.serial.stopbits = 1
            inst.close_port_after_each_call = True
            inst.serial.timeout = 1
            temp = inst.read_float(registeraddress=i, number_of_registers=2, functioncode=3)
            print(temp)
        except Exception as e:
            print(i, e)

    for i in range(2, 6 * 2, 2):
        try:
            inst = minimalmodbus.Instrument('COM3', 2)
            inst.serial.baudrate = 9600
            inst.serial.parity = "N"
            inst.serial.bytesize = 8
            inst.serial.stopbits = 1
            inst.close_port_after_each_call = True
            inst.serial.timeout = 1
            temp = inst.read_float(registeraddress=i, number_of_registers=2, functioncode=3)
        except Exception as e:
            print(i, e)

Since I don't have a device at hand that can perform this operation, but the demand is to perform this function, I would like to get some answers.

j123b567 commented 8 months ago

Yes https://minimalmodbus.readthedocs.io/en/stable/usage.html#using-multiple-instruments

childeyouyu commented 8 months ago

Yes https://minimalmodbus.readthedocs.io/en/stable/usage.html#using-multiple-instruments

thanks