Added software reset

This commit is contained in:
mj
2022-01-02 13:15:00 +01:00
parent 771fd24f93
commit 5feb12310c
4 changed files with 29 additions and 4 deletions

View File

@@ -8,7 +8,7 @@ f'''{argv[0]} ADDR [BAUDRATE] [{{read [REGISTER ...] | write VALUE REGISTER}}]
where:
- ADDR is Modbus address (use 0 for broadcast; note that only write can be broadcast)
- BAUDRATE is (optional) baud rate; default value is 19200
- action can be "read", "write" or can be omitted (defaults to "read all")
- action can be "read", "write", "reset" or it can be omitted completely (defaults to "read all")
- REGISTER can be either human-friendly register name (e.g. "CO2") or register number (30001)
- read may have list of REGISTER arguments (combining both register numbers and names)
- VALUE is 16-bit integer in decimal format
@@ -78,7 +78,7 @@ while arg_index < len(argv):
# sanity check
if not action:
action = 'all'
if action != 'read' and action != 'write' and action != 'all':
if action != 'read' and action != 'write' and action != 'all' and action != 'reset':
print(f'Unknown action {action}')
exit(-4)
if action == 'write' and value == None:
@@ -140,4 +140,11 @@ elif action == 'write':
except ValueError:
print(f'Register number {reg_number} cannot be written')
exit(-9)
exit(0)
elif action == 'reset':
if not s.reset():
# reset should throw NoResponse; if it doesn't, reset failed
print('Device reset failed!')
else:
print('---- Device reset ----')
exit(0)

View File

@@ -27,6 +27,8 @@ class Sensor():
'SCD4x_temperature_offset': 40006, \
'MODBUS_address': 40007, \
'baudrate': 40008 }
reset_register = 40100
reset_magic_number = 0xABCD
# readout and error counters
readout_total = 0
readout_error_invalid_response = 0 # checksum error: bus transmission corrupted?
@@ -49,6 +51,12 @@ class Sensor():
def close(self):
self.serial.serial.close()
self.serial = None
def reset(self):
try:
self.write_register(self.reset_register, self.reset_magic_number)
return False # got answer => failed to reset
except minimalmodbus.NoResponseError:
return True # no answer => reset successful
# High level read functions
@property
def CO2(self):