Reputation: 15
I am trying to build a logger that reads the real time data using raspberry pi and the modbus rtu. so I am able to read the real time values i.e. every second data. But I am only able to read it for a limited amount of time, say half hour or so. But I want to read the incoming data all day , all year. what's happening is after it reads for sometime and there is a communication error or modbus rtu error such as value error r I/O error. the code doesn't further execute and hence stops reading the data. what are the bugs that can be involved in such cases? what are all the coding errors that may occur which is not letting me read data for 24 hours? please help!
Upvotes: 1
Views: 1681
Reputation: 1923
You should wrap your part of the code where you read the data, into a "try-except" block. Then your program can print an error message instead of crashing.
I have written about this in the documentation of my MinimalModbus Python library: https://minimalmodbus.readthedocs.org/en/master/usage.html#handling-communication-errors
For example, instead of using:
print(instrument.read_register(4143))
Use:
try:
print(instrument.read_register(4143))
except IOError:
print("Failed to read from instrument")
except ValueError:
print("Instrument response is invalid")
You can read more on exceptions in the Python Tutorial: Handling Exceptions.
Upvotes: 2