bash脚本读取设备串口数据

shell脚本读取串口步骤

1.读取串口
2.过滤字符
3.字符分割
4.得到结果

date=`date +%Y-%m-%d@%H:%M:%S`
cat /dev/ttymxc3 | while read LINE  //读取串口一行行数据到LINE
do
 # echo ${LINE}
  head="GPGGA"
  result=$(echo $LINE | grep "${head}") //通过grep筛选出带GPGGA的一行行字符
  if [[ "$result" != "" ]];
   then result_split=${result#*E} //分割字符E,截取E后面的字符
        result_data=$date":"$result
        echo $result_data >>result.log //将result_data 重定向输出到result.log文件
        if [[ ${result_split:1:1} = 4 ]] //取字符串的第一个字符并判断
         then echo "rtk固定解正常"
        fi
        else echo -e "reset ephem ionutc almanac position\r\n" > /dev/ttymxc3 & //重启
                 read resp < /dev/ttymxc3 //读取串口数据至resp
        	  if [[ "$resp" =~ .*"OK".* ]]; then //判断resp字符
	          echo reset OK.
	  	  break
	          fi   
	fi
done
	
	      
        

猜你喜欢

转载自blog.csdn.net/qq_37967853/article/details/131240830