当前位置:网站首页>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
边栏推荐
- Bluetooth hc-05 pairing process and precautions
- UEFI development learning series
- C WinForm [get file path -- traverse folder pictures] - practical exercise 6
- How to copy formatted notepad++ text?
- Record the visual shock of the Winter Olympics and the introduction of the screen 2
- 研究发现,跨境电商客服系统都有这五点功能!
- Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
- After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
- [trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
- Random function usage notes
猜你喜欢

List of linked lists

Nb-iot technical summary

Reasons for rapid wear of conductive slip rings

Introduction of air gap, etc

Working principle and type selection of common mode inductor

Design a clock frequency division circuit that can be switched arbitrarily

Some thoughts on extracting perspectives from ealfa and Ebeta

Create inf module in AMI code

Consul安装

C language # and #
随机推荐
动力电池UL2580测试项目包括哪些
Circleq of linked list
String judgment
Hardware 3 -- function of voltage follower
Wifi-802.11 negotiation rate table
After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
Development tools -- gcc compiler usage
Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world
Network port usage
Basic information commands and functions of kernel development
Cadence simulation encountered "input.scs": can not open input file change path problem
Explication de la procédure stockée pour SQL Server
How to define guid in AMI code
Programming knowledge -- assembly knowledge
Consul安装
Baiwen 7-day smart home learning experience of Internet of things
Some errors in configuring the environment
VESC Benjamin test motor parameters
Beijing Winter Olympics opening ceremony display equipment record 3
导电滑环磨损快的原因