Skip to content
Advertisement

Loopback/Echo bytes received over serial port

On an embedded Linux system running Busybox I am trying to receive bytes over a serial port and echo back everything received.

The system setup is like this: Linux <-USB-> FTDI chip <-UART-> MCU On the UART line I have a logic analyser monitoring the data between the FTCI chip and the MCU. Both the MCU and Linux have the same UART configuration.

The script I have written runs on the Linux system and is supposed to send back all data it received from the MCU.

So far I have this simple Bash script which is to receive bursts of data 62 bytes long. The timeout is set to 5 seconds as a sort of alive signal.

#!/bin/bash

# Enable debugging
set -x

# Set the baudrate of the port 
stty -F /dev/ttyUSB0 1500000

while true
do 
  # Read 62 bytes with a timout of 5 seconds to variable RESP.
  read -N62 -t5 RESP < /dev/ttyUSB0

  # Print out how many bytes we received
  echo ${#RESP}

  # Send back the data, -n for no trailing new line
  echo -n $RESP > /dev/ttyUSB0
done

There are a few problems I have with this script:

  1. Not all bytes are received consistently. I’ve been testing now for some time an only seen the full number of bytes once.
  2. It outputs only 0xFF values on the UART bus, this is observed through the logic analyser.

What am I missing here in order to receiving the correct data and send it back correctly?

Advertisement

Answer

Through the suggestions made by @sawdust I was able to get a working script. In the end I stopped using the read command. I was unable to get it to work in raw mode. I could not use termios as this in to available on my Busybox system. I got a working setup using dd:

#!/bin/bash

stty -F /dev/ttyUSB0 raw
stty -F /dev/ttyUSB0 1500000

dd if=/dev/ttyUSB0 count=62 of=/dev/ttyUSB0
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement