#!/bin/bash # Define Modbus communication parameters DEVICE="/dev/ttyUSB0" BAUD="19200" PARITY="none" ADDRESS="1" START_COIL="1" COIL_COUNT="11" # Function to read coils and return as an array read_coins() { output=$(mbpoll $DEVICE -m rtu -b $BAUD -P $PARITY -a $ADDRESS -r $START_COIL -c $COIL_COUNT -t 0 -1 -o 2 | grep -E '\[\s*[0-9]+\]:') coils=() while IFS= read -r line; do coil=$(echo $line | awk '{print $2}') coils+=($coil) done <<< "$output" echo ${coils[@]} } # Function to write coils write_coins() { local values=$@ mbpoll $DEVICE -m rtu -b $BAUD -P $PARITY -a $ADDRESS -r $START_COIL -t 0 -1 -o 2 $values } # Read current state of the first 11 coils before_coins=$(read_coins) echo "Before: $before_coins" # Toggle the 11th coil coin_array=($before_coins) if [ ${coin_array[10]} -eq 0 ]; then coin_array[10]=1 else coin_array[10]=0 fi # Write to the coils write_coins ${coin_array[@]} # Output the current state of the coils after_coins=$(read_coins) echo "After: $after_coins" # Reporting to console if [ ${coin_array[10]} -eq 1 ]; then echo "Manual boost was turned ON." else echo "Manual boost was turned OFF." fi