Adriano Carvalho1 1
1 Answer
I am developing an orientation controller. I have a development board which communicates with the sensor(a compass) through I2C. Because the board is pretty limited (no OS), I developed a simple program toreceive things like: (1) 'get 0' to read the sensor's register 0; (2) 'set 0 10' to set the sensor's register 0 withthe value 10. For each of these cases the board returns: (1) 'Done: 10.' (register 0 has the value 10); (2)'Done.'; and (3) 'error: ...' in case of error. With this, I am trying to develop a shell script (bash) to sendcommands and retrieve data in order to understand the sensor and develop the controller.
My problem is with the following code:
# read device output in the background.
head -n 1 /dev/ttyUSB0 &
head=$!
# (#1): without the following stmt I get:
# head: cannot open `/dev/ttyUSB0' for reading: : Protocol error
sleep 0.1
# send command to the device.
echo "get 0" > /dev/ttyUSB0
# (#2) wait for head.
while kill -0 $head 2>/dev/null ; do : ; done
I guess (#1) is caused by a read/write conflict between 'head' and 'echo', but I don't know why and I have noidea on how to solve it.
Another issue is in (#2) where I would like to use a timeout. I've tried something like:
timeout 1 bash -c "while kill -0 $head 2>/dev/null ; do : ; done"
But I get: Timeout: aborting command ``bash'' with signal 9 and the program gets stuck.
By the way, before the code above is executed I do initialize the serial port with:
stty -F /dev/ttyUSB0 9600 cs8 -cstopb
EDIT: I don't want an interactive terminal. I want to use this routine as necessary. This routine is thenecessary foundation of the controller (read/write sensor's registers) which later will be implemented in theboard.
bash port serial
edited Jun 26 at 13:01 asked Jun 26 at 9:57
Have you seen the following post? unix.stackexchange.com/questions/22545/… – jpe Jun 26 at 10:15
Why don't you just wait $head ? – Dennis Williamson Jun 26 at 11:25
@jpe: I don't want an interactive shell. I want to use this routine only when necessary. – Adriano Carvalho Jun26 at 12:52
feedback
To solve (#1) I modified the routine to use a fd:
# $1: the device filename, eg. /dev/ttyS0
# $2: number of lines to read before exit.
exec 3<>$1
Serial port control through a shell script
bash - Serial port control through a shell script - Stack Overflow http://stackoverflow.com/questions/11204860/serial-port-control-throu...
1 de 2 18/09/2012 9:01
Adriano Carvalho1 1
head -n "$2" 0<&3 &
wait_pid=$!
cat - 1>&3
wait $wait_pid
exec 3>&-
EDIT: To solve (#2), instead of providing the routine with timeout support I delegate that responsibility to thecaller. However, in case of timeout we need to clean up. For that I've added the following afterwait_pid=$! :
trap="if kill -0 $wait_pid ; then kill -TERM $wait_pid ; fi"
trap "$trap" SIGINT SIGKILL SIGTERM
edited Jun 27 at 8:20 answered Jun 27 at 7:34
feedback
Not the answer you're looking for? Browse other questions tagged bash port serial
or ask your own question.
question feed
bash - Serial port control through a shell script - Stack Overflow http://stackoverflow.com/questions/11204860/serial-port-control-throu...
2 de 2 18/09/2012 9:01