#!/usr/bin/env python3
from time import sleep
from sys import exit
import minimalmodbus
import serial

slave_address = 247
instrument = minimalmodbus.Instrument('/dev/ttyUSB0', slave_address, close_port_after_each_call=True)  # port name, slave address (in decimal)

instrument.serial.baudrate = 19200
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

# holding register numbers
LED_on_register = 0
LED_brightness_register = 1
LED_smooth_register = 2
CO2_alert_limit_1_register = 3
CO2_alert_limit_2_register = 4
SCD4x_temperature_offset_register = 5
MODBUS_address_register = 6
baudrate_register = 7
# values
LED_on = 1
LED_brightness = 10
LED_smooth = 1
CO2_alert_limit_1 = 1500
CO2_alert_limit_2 = 3000
SCD4x_temperature_offset = 0
MODBUS_address = 247
baudrate = 0
registers = [LED_on, LED_brightness, LED_smooth, CO2_alert_limit_1, CO2_alert_limit_2, SCD4x_temperature_offset, MODBUS_address, baudrate]
# write to holding registers
print('---- Writing to multiple holding registers ----')
instrument.write_registers(LED_on_register, registers)
print('---- DONE ----')
exit(0)
