Short guide to Serial communications in Linux

本文详细介绍了在Linux环境下使用串口进行数据采集的方法,包括串口设备识别、配置、数据读写、解析及后台运行。还讨论了如何使用R语言直接记录数据,以及为何选择Linux作为数据日志收集平台的原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Table of Contents

  • 1 Finding serial ports

  • 2 Serial port settings

  • 3 Reading from and writing to serial port

  • 4 Parsing data

  • 5 Running process in the background

  • 6 Plotting data

  • 7 Terminal software

  • 8 Logging serial data using R

  • 9 Why use Linux for datalogging?

1 Finding serial ports

Serial ports appear in Linux as device files, which means you can access them conviniently with same command as e.g. text files.

  1. Normal serial ports appear as /dev/ttyS#, so first is /dev/ttyS0, second /dev/ttyS1 and so on.

  2. USB to serial adapters should work out of the box (from kernel 2.4 on I think) without any extra drivers and they appear as/dev/ttyUSB# ie. /dev/ttyUSB0

2 Serial port settings

Serial port settings can be changed using stty command. (See ”man stty”) Example: Set serial port /dev/ttyS0 baud rate to 57600 and odd parity

stty -F /dev/ttyS0 57600 parodd

3 Reading from and writing to serial port

You can send commands to serial port using command echo:

echo "command" > /dev/ttyUSB0

And read data from serial port using cat :

cat /dev/ttyUSB0

redirect output to a file:

cat /dev/ttyUSB0 > file.txt

4 Parsing data

A good option for parsing data is to use awk or gawk. Awk (gawk is GNU Awk) is a programming language, that is very good in modifying text files and usually doesn’t need any loops because it handles all rows in a file by default. You can pipe data from serial port to gawk for parsing. The gawk manual can be found at http://www.gnu.org/software/gawk/manual/gawk.pdf

Gawk example: Print (to terminal) current time in H:M:S format and first three characters from each row of data read from serial port.

cat /dev/ttyUSB0 | gawk ’{print strftime("%T"), substr($1 ,1,3)}’

5 Running process in the background

You can start backround data logging process (a single command or a shell script) using the command nohup. The process runs even if you close the terminal or logout from the computer. Example:

nohup cat /dev/ttyS0 | gawk ’{substr($1 ,1,3)}’ > result.txt

Kill the datalogging process (Note that you will kill all cat processes so you might want not use the command if you are not the only user or if you are logging from several ports and only want to stop one) :

killall cat

Less brutal alternetive is to find the process ID using ”ps ax” and killing the specific process:

kill -9 PID

6 Plotting data

What if you wan’t to plot your data in real time? I use program called kst http://kst.kde.org/ that can plot updating data from any text file. Works nicely and allows me to start plotting the data any time and turn off obviously useless plotting while I’m away from the computer. Kst is a kde app, but it works fine with also other WM’s and DE’s (I use openbox).

7 Terminal software

Use minicom or cutecom as terminal software. (like hyperterminal in Windows)

8 Logging serial data using R

You can also log data directly into R statistical software, at least with slow measurements. This is of course convenient for analysing purposes. The logging is done as follows using the scan command:

R example: reading in 6 comma separated numbers from /dev/stty0 into variable data (the command is broken into two lines):

data<-scan(file="/dev/ttyS0",n=6,
what=list(c1=(1),c2=(1),c3=(2),c4=(3),c5=(4),c6=(5)), sep=’,’)

9 Why use Linux for datalogging?

You can accomplish simple datalogging apps with just a few lines of text and parsing the data is very simple with gawk. Logging data from terminal takes very little system resources. For Example: I’m currently logging data from serial port at 220hz, parsing the data with gawk in realtime from hex to decimal format, converting the output of three channels of A/D converter to voltage data, adding a time stamp and saving datat to disk. And the whole process takes less than 1% CPU power (Celeron 2,66 Ghz) and 1600K memory! This means that you can also use old hardware, especially if you don’t need a GUI at all. You don’t have to worry about rebooting the computer logging the data anymore either and remote access is secure and convenient with ssh (even from your mobile if you wish). 3


转载于:https://my.oschina.net/u/115346/blog/391726

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值