0% found this document useful (0 votes)
280 views

Serial Port Control Through A Shell Script - Stack Overflow

The document describes a shell script written to control a serial port for communicating with a sensor device. The script runs into two issues: 1) a read/write conflict when reading the serial port output and sending commands, and 2) needing a timeout when waiting for the output. To solve issue 1, the script is modified to use a file descriptor rather than reading directly from the serial port. To solve issue 2, the script delegates timeout responsibility to the caller and adds cleanup code to kill the process if a timeout occurs.

Uploaded by

Antonio Dx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
280 views

Serial Port Control Through A Shell Script - Stack Overflow

The document describes a shell script written to control a serial port for communicating with a sensor device. The script runs into two issues: 1) a read/write conflict when reading the serial port output and sending commands, and 2) needing a timeout when waiting for the output. To solve issue 1, the script is modified to use a file descriptor rather than reading directly from the serial port. To solve issue 2, the script delegates timeout responsibility to the caller and adds cleanup code to kill the process if a timeout occurs.

Uploaded by

Antonio Dx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

bash - Serial port control through a shell script - Stack Overflow

http://stackoverflow.com/questions/11204860/serial-port-control-throu...

Serial port control through a shell script

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 to receive things like: (1) 'get 0' to read the sensor's register 0; (2) 'set 0 10' to set the sensor's register 0 with the 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 send commands 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/tt !"#$% for reading: : &rotocol error sleep 0.1 # send command to the device. echo "get 0" > /dev/ttyUSB0 # (#') wait for head. while kill -0 $head >/dev/n!ll " 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 no idea on how to solve it. Another issue is in (#2) where I would like to use a timeout. I've tried something like: ti$eo!t 1 %ash -c "&hile kill -0 $head >/dev/n!ll " do # " done"

But I get: 'i$eo!t# a%o(ting co$$and ))%ash** &ith signal + and the program gets stuck. By the way, before the code above is executed I do initialize the serial port with: stty -, /dev/ttyUSB0 +-00 cs. -cstop% EDIT: I don't want an interactive terminal. I want to use this routine as necessary. This routine is the necessary foundation of the controller (read/write sensor's registers) which later will be implemented in the board.
bash port serial

edited Jun 26 at 13:01

asked Jun 26 at 9:57 Adriano Carvalho 1 1

Have you seen the following post? unix.stackexchange.com/questions/22545/ jpe Jun 26 at 10:15 Why don't you just &ait $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 Jun 26 at 12:52

feedback

1 Answer
To solve (#1) I modified the routine to use a fd: # (1: the device filename) eg. /dev/tt "$ # (': number of lines to read before e*it. e/ec 01>$1

1 de 2

18/09/2012 9:01

bash - Serial port control through a shell script - Stack Overflow

http://stackoverflow.com/questions/11204860/serial-port-control-throu...

head -n "$ " 01&0 & &ait2pid=$! cat - 1>&0 &ait $&ait2pid e/ec 0>&EDIT: To solve (#2), instead of providing the routine with timeout support I delegate that responsibility to the caller. However, in case of timeout we need to clean up. For that I've added the following after &ait2pid=$! : t(ap="i3 kill -0 $&ait2pid " then kill -'456 $&ait2pid " 3i" t(ap "$t(ap" S7879' S78:7;; S78'456
edited Jun 27 at 8:20 answered Jun 27 at 7:34 Adriano Carvalho 1 1

feedback

Not the answer you're looking for? Browse other questions tagged bash port serial or ask your own question.

question feed

2 de 2

18/09/2012 9:01

You might also like