diff --git a/socat-at-bridge/atcmd b/socat-at-bridge/atcmd index bdbed10..4b40604 100644 --- a/socat-at-bridge/atcmd +++ b/socat-at-bridge/atcmd @@ -2,39 +2,27 @@ DEVICE=/dev/ttyOUT2 BAUD=115200 +TIMEOUT_DURATION=5 # Seconds to wait for a response # Setup device stty -F $DEVICE cs8 $BAUD ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts -# Main loop 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 - # Exit condition if [[ "$user_input" == "exit" ]]; then echo -e "\033[0;32mExiting...\033[0m" - break + exit 0 fi - # Send command to device - echo -e "$user_input\r" > $DEVICE & + # Send command to the device + echo -e "$user_input\r" > $DEVICE - # Read output in the background - (cat $DEVICE > /tmp/response & PID=$! - # Wait for "OK" or "ERROR" to appear in the output - while ! grep -qe "OK" -e "ERROR" /tmp/response; do - sleep 0.5 - done + # Use timeout to read the device's output and ensure it doesn't hang + response=$(timeout $TIMEOUT_DURATION cat $DEVICE) - # Print the response in green - echo -en "\033[0;32m" - cat /tmp/response - echo -en "\033[0m" + echo -en "\033[0;32m$response\033[0m" - # Cleanup - > /tmp/response - ) & - wait $! done