Read data from USB to 485 converter, send out a line from a file, repeat.
by Bill_Blessing from LinuxQuestions.org on (#52WTD)
I'm using Ubuntu 18.04.4 by way of background. I'm trying to create a small bash script that will read from a USB port and, if that reading matches a predefined string, output a line from a file. This can go on until EOF is reached. The reason that I would like something like this is that it will allow me to run some basic tests on our equipment.
There I can loop on reading any line from the USB - I'll make it more specific later - but get hung up on reading from a file. There are many, many examples of reading files line by line, but none quite like what I'm trying to do.
Here is my attempt at a shell script:
Code:#!/bin/bash
echo Test comms
stty -F /dev/ttyUSB0 19200 raw -echo #CONFIGURE SERIAL PORT
inFile="~/Documents/test/messages.txt"
IFS= read -r -d '' inText <"$inFile"
while read INPUT </dev/ttyUSB0
do
echo $inText
inText=$(( inText + 1 ))
doneI plan on reading up more on shell scripts, but for now this would be a big help.


There I can loop on reading any line from the USB - I'll make it more specific later - but get hung up on reading from a file. There are many, many examples of reading files line by line, but none quite like what I'm trying to do.
Here is my attempt at a shell script:
Code:#!/bin/bash
echo Test comms
stty -F /dev/ttyUSB0 19200 raw -echo #CONFIGURE SERIAL PORT
inFile="~/Documents/test/messages.txt"
IFS= read -r -d '' inText <"$inFile"
while read INPUT </dev/ttyUSB0
do
echo $inText
inText=$(( inText + 1 ))
doneI plan on reading up more on shell scripts, but for now this would be a big help.