Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reading serial port with BASH only

21,297 views
Skip to first unread message

Tomáš Kavalek

unread,
Apr 8, 2009, 12:06:23 PM4/8/09
to
Hello,
I have one big problem with reading serial port in BASH only. If I
read this port via Python and pySerial, everything is good, but I'm
not able to read it via BASH. My device is RFID reading module. When
the card is read, the ID is sent via serial port, but without CR, so
I'm not able to use anything from this:

1) cat /dev/ttyS0
2) while 1; do
read ID < /dev/ttyS0
echo ID
done

Plese, help, I don't know what to do. This script is for embbed device
http://www.atmel.com/dyn/products/tools_card.asp?tool_id=4057.

John Reiser

unread,
Apr 8, 2009, 2:14:06 PM4/8/09
to
> ... the ID is sent via serial port, but without CR, ...

Consult documentation "man stty" or "info stty" regarding "raw" mode.
Probably something like this is necessary:
< /dev/ttyS0 ( stty raw; read ID; echo ID )

--

Jens Thoms Toerring

unread,
Apr 9, 2009, 10:02:05 AM4/9/09
to
Tomáš Kavalek <tomas....@gmail.com> wrote:
> Hello,
> I have one big problem with reading serial port in BASH only. If I
> read this port via Python and pySerial, everything is good, but I'm
> not able to read it via BASH. My device is RFID reading module. When
> the card is read, the ID is sent via serial port, but without CR,

Don't you mean LF? That's what should end a line.

> so I'm not able to use anything from this:

> 1) cat /dev/ttyS0
> 2) while 1; do
> read ID < /dev/ttyS0

Do you know how many characters you must read? In that case using
the '-n' option for read should do the trick, e.g. if it's 8 use

read -n 8 ID < /dev/ttyS0

I guess that you also have to specify the number of chars to read
with pySerial's read() method (the readline() method would require
a '\n' at the end which doesn't seem to exist).

Just out of curiosity: how do you set up the communication para-
meters (baud rate, parity, number of stop bits etc.) for the
serial port using bash?
Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de

John Reiser

unread,
Apr 9, 2009, 10:40:36 AM4/9/09
to
> I guess that you also have to specify the number of chars to read
> with pySerial's read() method (the readline() method would require
> a '\n' at the end which doesn't seem to exist).

"The big key on the right" of a keyboard (perhaps labeled as Enter
or Return) sends '\r'. By default, the input canonicalization software
of a UNIX-like system changes '\r' to '\n' on tty connections.
This is the same software which echoes characters from stdin to stdout
(so that a typist can see them), and processes Backspace (erase previous
character), Ctrl-C (generate SIGINT), etc. The documentation refers
to this as "cooked" mode; the other mode is "raw" mode, which does
almost no processing at all (no echo, no erase, no signals, etc.)
The programming interface to this is through ioctl(); the command-line
interface is through /bin/stty.

> Just out of curiosity: how do you set up the communication para-
> meters (baud rate, parity, number of stop bits etc.) for the
> serial port using bash?

Use /bin/stty on an open fd (such as stdin), then don't close
that fd before using it (because the parameters will be reset
to the defaults by the operating system at the next open().)
Thus the usage
< /dev/ttyS0 ( stty ...; <commands>... )
where the shell opens /dev/ttyS0 once, the stty sets the parameters
for stdin, and the following commands use stdin without re-opening it.

--

Bernhard Agthe

unread,
Apr 14, 2009, 4:20:07 AM4/14/09
to
Hi,

Tomáš Kavalek wrote:
> I have one big problem with reading serial port in BASH only. If I

Took me about one hour to get it working ;-) OK, lets start by setting
the port's parameters:

stty -F /dev/ttyS0 ispeed 9600 ospeed 9600 -ignpar cs8
-cstopb -echo

In my case 9600 baud, no parity, one stop bit.

Then I wrote the data I need to send into a file (using hexedit) and
send the file's contents:

cat commando0.dat > /dev/ttyS0

For reading, I must refer you to the other posts ;-)

Have fun!

Grant Edwards

unread,
Apr 14, 2009, 10:31:09 AM4/14/09
to
On 2009-04-14, Bernhard Agthe <dark...@gmx.net> wrote:

> Tom???? Kavalek wrote:
>> I have one big problem with reading serial port in BASH only. If I
>
> Took me about one hour to get it working ;-) OK, lets start by setting
> the port's parameters:
>
> stty -F /dev/ttyS0 ispeed 9600 ospeed 9600 -ignpar cs8
> -cstopb -echo
>
> In my case 9600 baud, no parity, one stop bit.
>
> Then I wrote the data I need to send into a file (using hexedit) and
> send the file's contents:
>
> cat commando0.dat > /dev/ttyS0

One thing you might want to be aware of is that closing/opening
the serial port device has side effects (e.g. DTR changing
state). Depending on the connected device, you _may_ need to
avoid things like DTR toggling in the midst of the
conversation. There are a couple ways to do that:

* You can run all the command within a shell with stdin/stdout
redirected to the serial port:

(stty -F /dev/ttyS0 ispeed 9600 ospeed 9600 -ignpar cs8 -cstopb -echo
[shell read/write commands]
) </dev/ttyS0 >/dev/ttyS0

* You can write a small program that opens the serial port and
holds it open (without doing an read/write calls on it). As
long as at least one process has the device open, others can
open/close it without causing side effects like DTR
toggling.

--
Grant Edwards grante Yow! I'm in direct contact
at with many advanced fun
visi.com CONCEPTS.

Tomáš Kavalek

unread,
Apr 22, 2009, 9:26:28 AM4/22/09
to
Hi all,
the solutions was very simple, and here it is:

#!/bin/bash

# Port setting
stty -F /dev/ttyS1 raw speed 9600

# Loop
while [ 1 ]; do
READ=`dd if=/dev/ttyS1 count=1`
echo $READ
done

Tomáš Kavalek

unread,
Apr 22, 2009, 9:28:31 AM4/22/09
to

Juan Antonio Bacallado

unread,
Feb 17, 2022, 9:51:10 AM2/17/22
to
Hi Tomas,

really works to read from serial, could you help me I want to do something on bash to read and act like a daemon all the time like keyboard input, read what nfc near and enter character... And also bash script to write on it, i have tried

WRITE=`dd if=code of=/dev/ttyS0 count=1`

in code file is the number for example 051755F

thank you in advance.
0 new messages