Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux serial port listener and interpreter?

I'm using a serial device for a project, and what I'm trying to accomplish PC side, is listening for a command sent by the serial device, interpreting the query, running some code depending on the query, and transmitting back the result.

To be honest I tried out using PHP as the listener, and it works, unfortunately the infinite loop required to make the script act as a receiver, loads the CPU to 25%. So it's not really the best option.

I'm using cygwin right now, I'd like to create a bash script using linux native commands.

I can receive data by using:

cat /dev/ttyS2

And send a response with:

echo "command to send" > /dev/ttyS2

My question is, how do I make an automated listener to be able to receive and send data? The main issue I have, is actually how do I stop the cat /dev/ttyS2 command once information was received, put it into a variable which then I could compare with a switch, or a series of if else then blocks. Afterwards send back a response and start the cycle all over again?

Thanks

like image 612
Matt Avatar asked Oct 20 '12 17:10

Matt


People also ask

How does Linux assign serial ports?

Linux uses ttySx for a serial port device name. For example, COM1 (DOS/Windows name) is ttyS0, COM2 is ttyS1, and so on. USB based serial ports might use a name such as ttySUSB0. All these devices are located under /dev/ directory.

How do I access UART in Linux?

Serial port (UART) access from userspace on Linux is provided through TTY devices under /dev. Since BSP 5, we provide standard Toradex names by family, such as /dev/apalis-uart1, /dev/colibri-uarta and verdin-uart1, to enhance pin compatibility on a software level.

What is TTY port in Linux?

When it comes to Linux, TTY is an abstract device in UNIX and Linux. Sometimes it refers to a physical input device such as a serial port, and sometimes it refers to a virtual TTY where it allows users to interact with the system (reference).

How do I find the interpreter in Linux?

echo $0 – Another reliable and simple method to get the current shell interpreter name on Linux or Unix-like systems. readlink /proc/$$/exe – Another option to get the current shell name reliably on Linux operating systems. cat /etc/shells – List pathnames of valid login shells currently installed.


2 Answers

Is this not what you're looking for?

while read -r line < /dev/ttyS2; do
  # $line is the line read, do something with it
  # which produces $result
  echo $result > /dev/ttyS2
done

It's possible that reopening the serial device on every line has some side-effect, in which case you could try:

while read -r line; do
  # $line is the line read, do something with it
  # which produces $result
  echo $result > /dev/ttyS2
done < /dev/ttyS2

You could also move the output redirection, but I suspect you will have to turn off stdout buffering.

like image 179
rici Avatar answered Oct 21 '22 22:10

rici


To remain fairly system independent, use a cross platform programming language: like Python, use a cross platform serial library like : pySerial and do the processing inside a script. I have used pySerial and I could run the script cross platform with almost no changes in source code. By using BASH you're limiting yourself a fair little.

like image 25
Aniket Inge Avatar answered Oct 21 '22 22:10

Aniket Inge