36 lines
1.3 KiB
Python
Executable File
36 lines
1.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
from time import sleep
|
|
from sys import exit
|
|
import minimalmodbus
|
|
import serial
|
|
|
|
|
|
print('---- Looking for device ----')
|
|
slave_address_list = range(200,256)
|
|
baud_list = [19200,115200]
|
|
CO2_addr = 9
|
|
total_devices = 0
|
|
for slave_address in slave_address_list:
|
|
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)
|