How to use commands for RS-232 serial communication and packet parsing in Linux

Source: Internet
Author: User

The article starts at the vast first Sen blog

1. Get the serial number

In the Linux system everything is a file, so the serial port number is no exception, are in the form of device files. This means that we can access them using commands that access the text files.

A. The general serial port is/dev/ttys# in the format of display, so the first connection of the serial port is/DEV/TTYS0, the second connection of the serial port is/dev/ttys1 ... And so on

B. USB to serial port adaptation, no additional drivers, they will be displayed as/dev/ttyusb#, such as/dev/ttyusb0

2. Configure Serial Port Properties

We can use the Stty command to change the configuration serial port properties (see "Man sty" for details), such as we set the serial port/DEV/TTYS0 baud rate to 57600 and odd parity, the command is as follows

Stty-f/DEV/TTYS0 57600 parodd
3. Serial data read and write operation

Use echo to send data to the serial port, such as

echo "command" >/dev/ttyusb0

You can use cat to read data in a string, such as

Cat/dev/ttyusb0

Read the data and save it to a txt text file, such as

cat/dev/ttyusb0 > File.txt
4. Parsing data

When it comes to parsing data, we can use a powerful text analysis tool, awk, for the lookup of grep, and for the editing of SED, awk is particularly powerful in its analysis of data and generating reports. To put it simply, awk reads the file line-by-row, using spaces as the default delimiter to slice each row, and then perform various analytical processing of the cut.

AWK has 3 different versions: AWK, Nawk, and gawk, which are not specifically described, generally referred to as the GNU version of awk, Gawk,gawk.

Please refer to Http://www.gnu.org/software/gawk/manual/gawk.pdf for detailed instructions.

Below use Gawk to lift a chestnut, in the terminal in h:m:s format output the current time and from the serial port of each line of the first 3 characters, the command is as follows:

cat/dev/ttyusb0 | Gawk ' {print strftime ("%T"), substr ($ 1, 3)} '
5. Running in the background

Can be said that the serial data read and write operation is set to the background, through a simple command or shell script can be implemented. Commands can be used nohup. Even if you close the terminal or log out of the account, the process is still in progress, such as

Nohup CAT/DEV/TTYS0 | Gawk ' {substr ($ 1,3)} ' > Result.txt

Use the following command to delete a process, but all cat processes are deleted.

Killall Cat

We can use "PS ax" to see the current running process, select the one you want to delete, note the PID, delete it, as follows

Kill-9 PID
6. Drawing data

If you want to draw the data obtained in real-time, I recommend a software called KST (http://kst.kde.org/), it can get the text file data in real-time and draw out, you can visit the official website to see detailed instructions.

How to use commands for RS-232 serial communication and packet parsing in Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.