You need to be sure to have the correct read write permits on the device, you could see it with:
$ls -l /dev/[serial device]
I rely on the script you found and made some modifications.
For the development systems I've used by now, they used to need:
- None parity and
- One stop bit
Those values are the default ones in the script.
So in order to connect, you can use it as simple as follows:
./connect.sh /dev/[serial device] [baud speed]
Example:
$./connect.sh /dev/ttyUSB0 19200
Script:
#!/bin/bash
# connect.sh
#Taken from example modified by: ihatetoregister
# On stack exchange, thread:
# http://unix.stackexchange.com/questions/22545/how-to-connect-to-a-serial-port-as-simple-as-using-ssh
# Modified by Rafael Karosuo <[email protected]>
# - parity enabling and amount of stop bits
# - no execution without minimum params
# - exit code for stty
# - bgPid fix, used $! instead of $? to take the PID of cat proc in background.
# - exit command to end the program
# - CR termination and strip of NL added by READ command, in order to make $cmd\r\n format instead of \n$cmd\n
# Usage:
# $./connect.sh <device> <port speed> [# Stop bits] [parity]
# Stop bits 1|2
# Parity even | odd
# If no last two params, then default values stopbits=1, parity=disab
# Example:
# connect.sh /dev/ttyS0 9600 1 even, this will use 1 stop bit and even parity
# connect.sh /dev/ttyS0 9600, this will take default values for parity and stopbit
#Check if at least port and baud params provided
if [ -z "$1" ] || [ -z "$2" ]; then
printf "\nusage: ./connect.sh <device> <port speed> [# Stop bits 1|2] [parity even|odd]\n\tNeed to provide at least port and baud speed parameters.\n\texample:connect.sh /dev/ttyS0 9600\n\n"
exit 1;
else
case "$3" in
2) stopb="cstopb";;
*) stopb="-cstopb";;
esac
if [ "$4" = "even" ]; then
par="-parodd"
elif [ "$4" = "odd" ]; then
par="parodd"
else
par="-parity"
fi
printf "\nThen stty -F $1 $2 $stopb $par\n";
fi
# Set up device
stty -F "$1" "$2" "$stopb" "$par" -icrnl
# Check if error ocurred
if [ "$?" -ne 0 ]; then
printf "\n\nError ocurred, stty exited $?\n\n"
exit 1;
fi
# Let cat read the device $1 in the background
cat -v "$1" &
# Capture PID of background process so it is possible to terminate it when done
bgPid="$!"
# Read commands from user, send them to device $1
while [ "$cmd" != "exit" ]
do
read cmd
echo -e "\x08$cmd\x0D" > "$1" #strip off the \n that read puts and adds \r for windows like LF
done
# Terminate background read process
kill "$bgPid"
P.S.: You need to know which kind of line feed is using your receiver system since this will determine how you'll need to send the commands in my case I needed a Windows like LF, means that I need to send
command\r
ASCII values for:
- LF : 0Ah, line feed "\n"
- CR : 0Dh, carrige return "\r"
- BS : 08h, back space "<-"