当前位置:网站首页>Shell script realizes the reading of serial port and the parsing of message
Shell script realizes the reading of serial port and the parsing of message
2022-07-05 08:10:00 【Nanbolwan】
Recently, I am reading the message from the serial port and judging whether the message content is valid , Originally used C++ Wrote a little program . How board card sftp The service is always disconnected , So consider using shell Scripts do this . use shell Script implementation of these functions faces the following problems :
1. How to set and read the serial port from the command line ?
stty yes Linux A good terminal setting tool under the platform , Various terminal properties can be set . In essence, the serial port is also Linux A terminal in a system . So the first step is to set the baud rate of the serial port :
stty -F /dev/ttyO1 raw speed 9600
Then read the serial port , We use cat The command can easily read and print out the serial port information from the terminal , But in the script, we want to read a line of serial port messages into string variables , So there is the following code :
cat /dev/ttyO1 | while read LINE
do
echo LINE
#do something
...
done
2. How to intercept and compare strings ?
str="hello world"
# Intercept hello
h=${str:0:5}
# first : After that is the position ; the second : The following number indicates the number of characters
# There are many ways to compare strings , Here is one of them
if [ $h == 'hello' ]; then
#do something
...
fi
3. How to be in shell Count in script
shell All variables in the script are strings by default , So when we declare variables, we should explicitly assign values :
num=0
# to num Add 1
num=`expr $num + 1`
# Judge num Is it greater than 10
if [ $num -ge 10 ]; then
#do something
...
fi
4. Script implementation
The script reads the serial port message , Intercept the message header and valid as and judge , After passing the judgment, the counter adds 1, Timer exceeded 10 Program exit after ;
#!/bin/sh
stty -F /dev/ttyO1 raw speed 9600
num=0
cat /dev/ttyO1 | while read LINE
do
head=${LINE:0:6}
valid = ${LINE:17:1}
if [ $head == '$GPRMC' ] && [ $valid == 'A' ]; then
echo $num
num = `expr $num + 1`
if [ $num -ge 10 ]; then
exit
fi
fi
done
边栏推荐
- Pointnet++ classification practice
- 1-stm32 operation environment construction
- 如何将EasyCVR平台RTSP接入的设备数据迁移到EasyNVR中?
- [trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
- Bluetooth hc-05 pairing process and precautions
- Shell script basic syntax
- Consul安装
- Wifi-802.11 negotiation rate table
- Matlab2018b problem solving when installing embedded coder support package for stmicroelectronic
- Hardware and software solution of FPGA key chattering elimination
猜你喜欢
Introduction of air gap, etc
Interview catalogue
Altium designer learning (I)
Network communication process
Several important parameters of LDO circuit design and type selection
The research found that the cross-border e-commerce customer service system has these five functions!
How to select conductive slip ring
H264 (I) i/p/b frame gop/idr/ and other parameters
Semiconductor devices (I) PN junction
Shell script basic syntax
随机推荐
生产中影响滑环质量的因素
Pointnet++ classification practice
How to copy formatted notepad++ text?
C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program
Hardware and software solution of FPGA key chattering elimination
Some errors in configuring the environment
导电滑环磨损快的原因
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
Simple design description of MIC circuit of ECM mobile phone
Live555 push RTSP audio and video stream summary (I) cross compilation
Carrier period, electrical speed, carrier period variation
L'étude a révélé que le système de service à la clientèle du commerce électronique transfrontalier a ces cinq fonctions!
Development tools -- gcc compiler usage
Slist of linked list
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
Sizeof (function name) =?
Take you to understand the working principle of lithium battery protection board
[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
Programming knowledge -- assembly knowledge
Semiconductor devices (I) PN junction