Update atcmd
This commit is contained in:
@@ -1,48 +1,40 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Define constants for device and baud rate
|
DEVICE=/dev/ttyOUT2
|
||||||
DEVICE="/dev/ttyOUT2"
|
|
||||||
BAUD=115200
|
BAUD=115200
|
||||||
|
|
||||||
# Function to clean up before exit
|
# Setup device
|
||||||
cleanup() {
|
stty -F $DEVICE cs8 $BAUD ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts
|
||||||
# Ensure microcom process is killed
|
|
||||||
[[ -n $microcom_pid ]] && kill $microcom_pid &> /dev/null
|
|
||||||
# Remove temporary file if it exists
|
|
||||||
[[ -f "$tmpfile" ]] && rm "$tmpfile"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Setup trap to clean up on script exit
|
|
||||||
trap cleanup EXIT
|
|
||||||
|
|
||||||
|
# Main loop
|
||||||
echo "Type 'exit' to end the session."
|
echo "Type 'exit' to end the session."
|
||||||
while true; do
|
while true; do
|
||||||
echo "Enter AT Command:"
|
echo -n "Enter AT Command: "
|
||||||
read user_input
|
read user_input
|
||||||
|
|
||||||
# Exit if user types 'exit'
|
# Exit condition
|
||||||
if [[ "$user_input" == "exit" ]]; then
|
if [[ "$user_input" == "exit" ]]; then
|
||||||
echo "Exiting..."
|
echo "Exiting..."
|
||||||
exit 0
|
break
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Send command to device via microcom
|
# Send command to device
|
||||||
tmpfile=$(mktemp /tmp/microcom_output.XXXXXX)
|
echo -e "$user_input\r" > $DEVICE &
|
||||||
# Start microcom with output redirected to temp file in background
|
|
||||||
(echo "$user_input" && cat) | microcom -s $BAUD $DEVICE > "$tmpfile" 2>&1 &
|
|
||||||
microcom_pid=$!
|
|
||||||
|
|
||||||
# Wait for "OK" or "ERROR" in output
|
# Read output in the background
|
||||||
while : ; do
|
(cat $DEVICE > /tmp/response & PID=$!
|
||||||
if grep -qE "OK|ERROR" "$tmpfile"; then
|
# Wait for "OK" or "ERROR" to appear in the output
|
||||||
kill $microcom_pid &> /dev/null
|
while ! grep -qe "OK" -e "ERROR" /tmp/response; do
|
||||||
wait $microcom_pid 2>/dev/null
|
sleep 0.5
|
||||||
cat "$tmpfile" # Display the output
|
done
|
||||||
break
|
|
||||||
fi
|
|
||||||
sleep 1 # Check periodically
|
|
||||||
done
|
|
||||||
|
|
||||||
# Cleanup for the next iteration
|
# Kill the background reading process
|
||||||
cleanup
|
kill $PID
|
||||||
|
|
||||||
|
# Print the response
|
||||||
|
cat /tmp/response
|
||||||
|
# Cleanup
|
||||||
|
> /tmp/response
|
||||||
|
) &
|
||||||
|
wait $!
|
||||||
done
|
done
|
||||||
|
|||||||
Reference in New Issue
Block a user