0

So I am trying to send a serial command to an embedded device and get a response for our test automation, I tried using cat as the following code, and gets stuck on the cat command, same goes for dd, and I also tried the solution posted in this link to no avail. I want to send a command, receive a reply, and do something with the reply non blocking. Maybe Bash has something similar to Select() in C? Or if anybody has a more elegant solution I would appreciate it immensely :)

#!/bin/bash
#tty=/dev/tps21
stty -F /dev/tps21 speed 38400 cs8 cread clocal -cstopb -parenb >> /dev/null
echo 1
echo 'loglevel off' > /dev/tps21
echo 2
cat < /dev/tps21 >> /dev/null #Gets stuck here
echo 3
echo 'shell <username> <password> /opt/tools/eeprom/read.sh wifi_apn' > /dev/tps21
echo 4
echo 5
apn_raw="$(cat < /dev/tps21)" #Gets Stuck here
echo 6
apn="$(echo "$apn_raw" | sed -n 3p)"
echo 7
echo "${apn}"
echo 8
echo 'shell <username> <password> /opt/tools/eeprom/read.sh wifi_pass' > /dev/tps21
pass_raw="$(cat -v < /dev/tps21)" #Gets Stuck here
pass="$(echo "$pass_raw" | sed -n 3p)"
echo "${pass}"
nmcli --nocheck d $apn connect $apn password $pass iface wlan0
Community
  • 1
  • 1
  • `cat` will read from its input until the input indicates end-of-file. If that indication is never generated, it will happily sit there forever waiting for additional input. So, basically, it's working as designed. If you want to read a limited amount of data, then perhaps `dd` would be better than `cat`... – twalberg Jul 18 '16 at 20:46
  • Thank you very much I am coming to that realization, but `dd` is not useful in my application since I don't know how many bytes I am reading, I might be getting some log messages afterwards – Nas Makkiya Jul 18 '16 at 21:37

1 Answers1

0

The easiest solution I found was to just use timeout XX cat and that works out perfectly for my application