LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: read/write the interface? (https://www.linuxquestions.org/questions/programming-9/bash-read-write-the-interface-189086/)

J_Szucs 06-02-2004 10:42 PM

Bash: read/write the serial interface?
 
I have a measurement device attached to the standard RS-232C interface of a PC.

Is it possible to use a bash script to send simple commands and read lines of data via that interface?

I also wonder if I could configure the interface (parity, etc...) from the bash script, or it should be done in a system config file (but which one?).

I use bash scripts extensively for other purposes, but this is something new to me.
Could you give me a hint to start me in the right direction?

paulsm4 06-02-2004 11:15 PM

Perhaps your distro might have some/all of:

uucp
tip
rz/xz (zmodem programs)
kermit

I'm honestly not sure what would work best for you.

J_Szucs 06-03-2004 10:58 PM

I had a glance at those programs, but I think some of those are modem-specific, others require that the same program be running at the other end of the connection.

A measuring instrument is neither a modem, nor is it running Linux (at least for the time being...).

It is something much simpler: you send it strings via the serial interface, and you read in its reply strings via the same interface.

By the first sight this seems to be so simple that there must be an easy solution in Linux, too.

jlliagre 06-04-2004 01:48 AM

I guess stty would allow you to set serial line parameters (speed, parity, ...), echo to send data and cat to receive data.

J_Szucs 06-04-2004 03:46 AM

Well, actually I was wondering something like as a possible solution.

echo "commandstring" > /dev/ttyS0
will surely work, and send the command string to the measuring device via the serial port.

But how about receiving the input from the device?

Is this supposed to work?:
cat /dev/ttyS0

I am really in doubt...

philetus 06-04-2004 02:44 PM

$ cat /dev/ttyS0
is supposed to work:

from http://www.technoir.nu/hplx/hplx-l/0208/msg00172.html -

I have now been able to eliminate this error source, by rereading the
setserial man page. ;-)
setserial /dev/ttyS0 baud_base 115200
and configure the LX to 9600 8n1
does the trick. I still don't know why, but now it works. I can verify
it with starting datacomm on the LX, setting it to 9600 8n1, terminal
type TTY and then doing a

echo hello > /dev/ttyS0

on the Linux box. Gives a "hello" on the LX.
In the other direction, if I do a
cat /dev/ttyS0
on the linux box, and enter hello[enter] on the LX, I get a hello on
the linux console.

jlliagre 06-04-2004 02:51 PM

This should work:

(stty raw; cat > data ) < /dev/ttyS0

philetus 06-04-2004 03:09 PM

I am trying to do something similar, and I can write to my rs232 device like this:

$ echo hi >/dev/ttyS0

but when I try to read from it like this:

$ cat </dev/ttyS0

it hangs.

are you having any better luck? what settings are you using for setserial and stty?


All times are GMT -5. The time now is 11:34 AM.