Added baud sweep to find_address.py

This commit is contained in:
Duke NUCem 2021-10-20 20:04:49 +02:00
parent 8776a84678
commit a5c9eb180d

View File

@ -7,27 +7,29 @@ import serial
print('---- Looking for device ----') print('---- Looking for device ----')
slave_address_list = range(1,256) slave_address_list = range(1,256)
baud_list = [4800,115200]
CO2_addr = 9 CO2_addr = 9
total_devices = 0 total_devices = 0
for slave_address in slave_address_list: for slave_address in slave_address_list:
print(f'Address {slave_address}:\t', end='') for baud in baud_list:
try: print(f'Address {slave_address} baud {baud}:\t\t', end='')
# modbus init try:
instrument = minimalmodbus.Instrument('/dev/rs485', slave_address, close_port_after_each_call=True) # port name, slave address (in decimal) # modbus init
instrument.serial.baudrate = 115200 instrument = minimalmodbus.Instrument('/dev/rs485', slave_address, close_port_after_each_call=True) # port name, slave address (in decimal)
instrument.serial.bytesize = 8 instrument.serial.baudrate = baud
instrument.serial.parity = serial.PARITY_EVEN instrument.serial.bytesize = 8
instrument.serial.stopbits = 1 instrument.serial.parity = serial.PARITY_EVEN
instrument.serial.timeout = 0.05 # seconds instrument.serial.stopbits = 1
instrument.mode = minimalmodbus.MODE_RTU # rtu or ascii mode instrument.serial.timeout = 0.05 # seconds
instrument.clear_buffers_before_each_transaction = True instrument.mode = minimalmodbus.MODE_RTU # rtu or ascii mode
## instrument.clear_buffers_before_each_transaction = True
CO2 = instrument.read_register(CO2_addr, 1, functioncode=4) * 10 ##
print('DEVICE RESPONDED') CO2 = instrument.read_register(CO2_addr, 1, functioncode=4) * 10
total_devices += 1 print('DEVICE RESPONDED')
except minimalmodbus.NoResponseError: total_devices += 1
print(f'no response') except minimalmodbus.NoResponseError:
sleep(0.1) print(f'no response')
sleep(0.1)
print(f'Found {total_devices} devices (tried {len(slave_address_list)} addresses)') print(f'Found {total_devices} devices (tried {len(slave_address_list)} addresses)')
print('---- DONE ----') print('---- DONE ----')
exit(0) exit(0)