#!/bin/bash

DEVICE=/dev/ttyOUT2
BAUD=115200

# Configure device with stty
stty -F $DEVICE $BAUD raw -echo

echo -e "\033[0;36mType 'exit' to end the session.\033[0m"
while true; do
    echo -en "\033[0;36mEnter AT Command: \033[0m"
    read user_input

    if [[ "$user_input" == "exit" ]]; then
        echo -e "\033[0;32mExiting...\033[0m"
        exit 0
    fi

    # Send the AT command to the device
    echo -e "$user_input\r" > $DEVICE

    # Prepare to capture the response
    {
        # Keep reading until we find a line ending with OK or ERROR
        while IFS= read -r line; do
            echo -e "\033[0;32m$line\033[0m"
            if [[ "$line" == *OK || "$line" == *ERROR ]]; then
                break
            fi
        done
    } < $DEVICE
done
