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]
$./connect.sh /dev/ttyUSB0 19200
#Taken from example modified by: ihatetoregister
# On stack exchange, thread:
# Modified by Rafael Karosuo <firstname.lastname@example.org>
# - 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
# $./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
# 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"
case "$3" in
if [ "$4" = "even" ]; then
elif [ "$4" = "odd" ]; then
printf "\nThen stty -F $1 $2 $stopb $par\n";
# 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"
# 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
# Read commands from user, send them to device $1
while [ "$cmd" != "exit" ]
echo -e "\x08$cmd\x0D" > "$1" #strip off the \n that read puts and adds \r for windows like LF
# Terminate background read process
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
ASCII values for:
- LF : 0Ah, line feed "\n"
- CR : 0Dh, carrige return "\r"
- BS : 08h, back space "<-"