| Date: Fri, 25 Feb 2005 13:19:35 +0000
| From: P at draigBrady.com
|
| newboid at vodafone.ie wrote:
| > I am quite new to linux and I was wondering if there
| > is to get a string from a serial port from the shell?
| >
| > I have tried using:
| > cat < /dev/ttyS0
| > and I can see the string from the serial port, but I don't
| > seen to see a way of terminating the cat command [ ... ]
if that serial line is connected to a terminal,
try pressing ^D (control-D). that is nominally
means EOF (end-of-file) on a serial line, and
will terminate the cat(1).
^D-is-EOF is not assured because it depends on
the mode/settings of ttyS0. but yer basic problem
here is `cat' reads until EOF, and it is not seeing
an EOF. so it reads "forever".
| If you know exactly how much data there will be you could do
| [ I have incorporated Pádraig's later correction -blf]:
|
| REPLY=`dd if=/dev/ttyS0 bs=1 count=$number_of_bytes`
|
| If it's line oriented data then I suppose you could do:
|
| while read /dev/ttyS0
| echo received: $REPLY
| done
not quite. syntax error. missing crunch (<).
instead, use either of the following:
while read </dev/ttyS0; do while read; do
echo received: "$REPLY" echo received: "$REPLY"
done done </dev/ttyS0
I _think_ in this case it is a matter of personal style
which to use, but in general the right-hand form (with
the entire loop's stdin redirected) is what you want to
use (esp. when, e.g., using `read' to read a file (think
about it: how often is the file openned?)).
but, if the cat(1) is not seeing an EOF, then the above
`while'-loops are not quite right either (albeit close).
they both also loop until an EOF. ;-(
if you need to loop, but without an EOF to terminate,
then do a `break' upon some condition. for example:
# read lines from ttyS0 until a line containing
# only the word "plover" is read, then stop.
while read the_line; do
[ "X$the_line" = Xplover ] && break
... process $the_line here ...
done </dev/ttyS0
(I personally do not like $REPLY, so have switched
to using an explicit shell variable to contain the
line of data from /dev/ttyS0.) it is pehaps less
obscure to write it as:
while
read the_line && [ "X$the_line" != Xplover ]
do
... process $the_line here ...
done </dev/ttyS0
in any case, please note that the behaviour can and
will vary depending on whether or not ttyS0 is in
canonical mode. briefly (and slightly simplified),
when in canonical mode, reading data from the tty
hangs until <Enter> is pressed; and then, only the
characters up to and including that newline can be
read. (the entire line does not have to be read
all at once).
consider Pádraig's `dd' suggestion:
REPLY=$( dd if=/dev/ttyS0 bs=1 count=15 )
(I also do not like the `...` notation, and so have
used the $(...) functional equivalent.) the above
does 15 read(2)s, each of 1 byte. that means, if
ttyS0 is in canonical mode, the input line (including
newline) must contain at least 15 characters, else
the following read hangs waiting for another newline.
turning it around to be 1 read of at most 15 bytes:
REPLY=$( dd if=/dev/ttyS0 bs=15 count=1 )
requires only one newline (in canonical mode).
one final note about the `dd' solution. normally,
`dd' always prints messages of the form:
1+2 records in
3+4 records out
to stderr. this can be very annoying (but in the
correct circumstances, also very useful); the usual
work-around is to bin stderr:
dd ... 2>/dev/null
albeit I do not like doing that either as you then
lose useful error message from `dd'. grrr.... ;-\
cheers!
-blf-
--
Experienced (20+ yrs) kernel/software Eng: | Brian Foster Montpellier,
• Unix, embedded, &tc; • Linux; • doc; | blf at utvinternet.ie FRANCE
• IDL, automated testing, process, &tc. | Stop E$$o (ExxonMobile)!
Résumé (CV) http://www.blf.utvinternet.ie | http://www.stopesso.com
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!