当前位置:网站首页>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
边栏推荐
- Vofa+ software usage record
- Several important parameters of LDO circuit design and type selection
- Shell script basic syntax
- Define in and define out
- How to excavate and research ideas from the paper
- Process communication mode between different hosts -- socket
- Beijing Winter Olympics opening ceremony display equipment record 3
- My-basic application 2: my-basic installation and operation
- C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program
- UEFI development learning 2 - running ovmf in QEMU
猜你喜欢

NTC thermistor application - temperature measurement
![Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev](/img/42/d857452ccfeccbbf1ac34f47e47e2e.jpg)
Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev

Reasons for rapid wear of conductive slip rings

Compilation warning solution sorting in Quartus II

Process communication mode between different hosts -- socket
![C WinForm [exit application] - practice 3](/img/25/30c795cc3fa6931eb1d733719d4ad0.jpg)
C WinForm [exit application] - practice 3
![Halcon's practice based on shape template matching [1]](/img/68/206eed7502fbf108a929aa9365b1ae.jpg)
Halcon's practice based on shape template matching [1]
![Measurement fitting based on Halcon learning [i] fuse Hdev routine](/img/91/34c92065e797c87d6ce5ea13903993.jpg)
Measurement fitting based on Halcon learning [i] fuse Hdev routine

LED display equipment records of the opening ceremony of the Beijing Winter Olympics

生产中影响滑环质量的因素
随机推荐
Programming knowledge -- assembly knowledge
Development tools -- gcc compiler usage
PMSM dead time compensation
Sizeof (function name) =?
Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev
Bluetooth hc-05 pairing process and precautions
Relationship between line voltage and phase voltage, line current and phase current
Circleq of linked list
UEFI development learning 6 - creation of protocol
Soem EtherCAT source code analysis II (list of known configuration information)
[paper reading] the latest transfer ability in deep learning: a survey in 2022
More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
Fundamentals of C language
Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
UEFI development learning 3 - create UEFI program
Detailed explanation of pragma usage
Adaptive filter
Verilog -- state machine coding method
Ble encryption details