diff --git a/socat-at-bridge/atcmd b/socat-at-bridge/atcmd index 8efb075..8d272a4 100644 --- a/socat-at-bridge/atcmd +++ b/socat-at-bridge/atcmd @@ -1,27 +1,17 @@ #!/bin/bash DEVICE=/dev/ttyOUT2 +BAUD=115200 # Function to setup device communication parameters setup_device() { - stty -F $DEVICE cs8 115200 ignbrk -brkint -icrnl -imaxbel \ + stty -F $DEVICE cs8 $BAUD ignbrk -brkint -icrnl -imaxbel \ -opost -onlcr -isig -icanon -iexten -echo -echoe -echok \ -echoctl -echoke noflsh -ixon -crtscts } -# Function to read from device until "OK" or "ERROR" is found -read_response() { - { - while IFS= read -r line; do - echo -e "\033[0;32m$line\033[0m" - if [[ "$line" == *"OK" || "$line" == *"ERROR" ]]; then - break - fi - done < "$DEVICE" - } & - READ_PID=$! - wait $READ_PID -} +# Prepare the device for communication +setup_device echo -e "\033[0;36mType 'exit' to end the session.\033[0m" while true; do @@ -30,10 +20,36 @@ while true; do if [[ "$user_input" == "exit" ]]; then echo -e "\033[0;32mExiting...\033[0m" - exit 0 + break fi - setup_device + # Clear the device buffer before sending a new command + echo -n > $DEVICE + + # Send the AT command echo -e "$user_input\r" > $DEVICE - read_response + + # Use a temporary file to capture the command output + tmpfile=$(mktemp) + + # Start reading the device output to the temporary file + cat $DEVICE > "$tmpfile" & + CAT_PID=$! + + # Monitor the output file for "OK" or "ERROR" + while ! grep -qe "OK" -e "ERROR" "$tmpfile"; do + sleep 1 + done + + # Kill the `cat` process after capturing the response + kill $CAT_PID + wait $CAT_PID 2>/dev/null + + # Display the response + cat "$tmpfile" | while IFS= read -r line; do + echo -e "\033[0;32m$line\033[0m" + done + + # Clean up + rm "$tmpfile" done