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?
Perhaps your distro might have some/all of:
rz/xz (zmodem programs)
I'm honestly not sure what would work best for you.
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.
I guess stty would allow you to set serial line parameters (speed, parity, ...), echo to send data and cat to receive data.
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?:
I am really in doubt...
$ 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
on the linux box, and enter hello[enter] on the LX, I get a hello on
the linux console.
This should work:
(stty raw; cat > data ) < /dev/ttyS0
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
are you having any better luck? what settings are you using for setserial and stty?
|All times are GMT -5. The time now is 12:09 AM.|