pyhys / minimalmodbus

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

If you read the data multiple times, you won't be able to read the data #127

Closed childeyouyu closed 7 months ago

childeyouyu commented 7 months ago
    while True:
        read_data = inst.read_float(registeraddress=1, number_of_registers=2, functioncode=3)
        print(read_data)
        time.sleep(1)

I need to consistently read the data in the device, but frequently, it will not read the data. Like this: minimalmodbus.NoResponseError: No communication with the instrument (no answer)

arthurtaras commented 7 months ago

Increase the sleep time to 50 and see if errors still appear. If not decrease to 25, 10, 5 and so on. Chances are you are overdriving it too fast either on the processor or microcontroller side

childeyouyu commented 7 months ago

@arthurtaras Do you mean 50s? This is too long a wait time for me, and I need to record a lot of data on demand.

j123b567 commented 7 months ago

Did you follow all instructions in Serial communication and Troubleshooting?

arthurtaras commented 7 months ago

@childeyouyu just test it with 10, 5, 2 and see if the frequency of errors goes up or down. Do you have access to the code on the microcontroller? To read more data more frequently you would need to optimize the other side

childeyouyu commented 7 months ago

@childeyouyu just test it with 10, 5, 2 and see if the frequency of errors goes up or down. Do you have access to the code on the microcontroller? To read more data more frequently you would need to optimize the other side

I will try it tomorrow.

j123b567 commented 7 months ago

IMHO longer time.sleep will not help here.

childeyouyu commented 7 months ago

@j123b567 It works, I set inst.serial.timeout to 1, the program reads a thousand lines in a row without the previous error, I will test the program all the way to the official program code, and if available, I will close this issue. So is this due to a problem with my device's performance?

j123b567 commented 7 months ago

@childeyouyu There is nothing particularly wrong with that. Some devices even add a programmable delay before responding. You can try a much smaller number than 1 second, e.g. 0.03 or even 0.01 might do. Setting this to 1 can make the whole communication unnecessarily slow.