Added baud sweep to find_address.py
This commit is contained in:
		@@ -7,27 +7,29 @@ import serial
 | 
			
		||||
 | 
			
		||||
print('---- Looking for device ----')
 | 
			
		||||
slave_address_list = range(1,256)
 | 
			
		||||
baud_list = [4800,115200]
 | 
			
		||||
CO2_addr = 9
 | 
			
		||||
total_devices = 0
 | 
			
		||||
for slave_address in slave_address_list:
 | 
			
		||||
    print(f'Address {slave_address}:\t', end='')
 | 
			
		||||
    try:
 | 
			
		||||
        # modbus init
 | 
			
		||||
        instrument = minimalmodbus.Instrument('/dev/rs485', slave_address, close_port_after_each_call=True)  # port name, slave address (in decimal)
 | 
			
		||||
        instrument.serial.baudrate = 115200
 | 
			
		||||
        instrument.serial.bytesize = 8
 | 
			
		||||
        instrument.serial.parity   = serial.PARITY_EVEN
 | 
			
		||||
        instrument.serial.stopbits = 1
 | 
			
		||||
        instrument.serial.timeout  = 0.05          # seconds
 | 
			
		||||
        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')
 | 
			
		||||
        total_devices += 1
 | 
			
		||||
    except minimalmodbus.NoResponseError:
 | 
			
		||||
        print(f'no response')
 | 
			
		||||
    sleep(0.1)
 | 
			
		||||
    for baud in baud_list:
 | 
			
		||||
        print(f'Address {slave_address} baud {baud}:\t\t', end='')
 | 
			
		||||
        try:
 | 
			
		||||
            # modbus init
 | 
			
		||||
            instrument = minimalmodbus.Instrument('/dev/rs485', slave_address, close_port_after_each_call=True)  # port name, slave address (in decimal)
 | 
			
		||||
            instrument.serial.baudrate = baud
 | 
			
		||||
            instrument.serial.bytesize = 8
 | 
			
		||||
            instrument.serial.parity   = serial.PARITY_EVEN
 | 
			
		||||
            instrument.serial.stopbits = 1
 | 
			
		||||
            instrument.serial.timeout  = 0.05          # seconds
 | 
			
		||||
            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')
 | 
			
		||||
            total_devices += 1
 | 
			
		||||
        except minimalmodbus.NoResponseError:
 | 
			
		||||
            print(f'no response')
 | 
			
		||||
        sleep(0.1)
 | 
			
		||||
print(f'Found {total_devices} devices (tried {len(slave_address_list)} addresses)')
 | 
			
		||||
print('---- DONE ----')
 | 
			
		||||
exit(0)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user