Shell 脚本实现串口操做

前言

命令行下经常使用的串口链接工具备 screenminicom等。python

实际生产测试需求中,经常有串口连通性测试,其对应的最简单的测试方法即:「调用非交互式串口工具登陆串口并发送(命令)接收数据(命令返回)」web

这种状况下,screenminicom需交互模式下使用的工具已不适用(不易使用expect实现,并且须要额外安装工具)。shell

根据测试方法能够知道,实际测试工具所需具有功能并不复杂,因此一般咱们使用 c语言python 调用相关库完成该工具的编写。vim

但秉承能偷懒就偷懒,能用shell自己实现绝对很少**的宗旨,此文探索下 shell 的实现方法。bash

Shell实现

主要的实现思路:stty实现串口波特率等的设置,cat实现内容的读取,echo实现串口输入。微信

实现效果

  • 普通非交互式命令与普通终端操做无区别,如 lscd
  • 在使用交互式命令时,不太友好,但能够实现一些简单操做,如 vim中的上下翻页等
  • 可拓展性好,可将串口登陆退出等操做嵌入脚本中,调用该脚本工具时只用关注命令发送和返回内容解析便可

stty修改串口终端设置

初始状态时,使用cat /dev/ttyUSB0可能会出现刷屏或者乱码,需使用stty进行相关参数设置。一般使用的命令为stty -F /dev/$serial_port raw $port_speed。在该命令以后就能够使用echo/cat进行输入输出操做了。session

可是在有些时候依旧有乱码出现,这时候须要一些额外的参数设置。因为相关参数设置较多,不能保证设置完后达到预期效果,因此须要一个比较取巧的办法:「使用screen或者minicom等初始化设置串口以后的设置」。具体操做以下:多线程

  1. 首先使用 stty获取当前串口默认值,从第一行开始分别是
    1. 串口波特率
    2. Special characters
    3. Special settings
    4. Control settings
    5. Input settings
    6. Output settings
    7. Local settings
[root@localhost ~]# stty -a -F /dev/ttyUSB0
speed 9600 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;
lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
  1. 使用screen /dev/ttyUSB0 115200(或者使用minicom)登入串口,确承认以正常读写后退出并发

  2. 再使用stty获取当前设置,主要查看第一步中 4-7 行app

[root@localhost ~]# stty -a -F /dev/ttyUSB0
speed 115200 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^H; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;
lnext = ^V; flush = ^O; min = 100; time = 2;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread clocal -crtscts
-ignbrk brkint ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon iexten -echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
  1. 比较二者可发现有些设置的不一样,直接将4-7行带入stty命令,便可完成对串口终端的正确设置,前面的1-3行内容也可视状况导入,需根据帮助文档作参数格式改动。

  2. 另,若只为设置串口,不须要human-readable,可以使用stty-readable模式进行设置的导出和导入:

[root@localhost ~]# stty -g -F /dev/ttyUSB0
500:5:cbd:8a3b:3:1c:7f:15:4:0:1:0:11:13:1a:0:12:f:17:16:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0
[root@localhost ~]# stty  -F /dev/ttyUSB0 "406:0:18b2:8a30:3:1c:7f:8:4:2:64:0:11:13:1a:0:12:f:17:16:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0"
stty: /dev/ttyUSB0: unable to perform all requested operations
[root@localhost ~]# stty -a -F /dev/ttyUSB0
speed 115200 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^H; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;
lnext = ^V; flush = ^O; min = 100; time = 2;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread clocal -crtscts
-ignbrk brkint ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon iexten -echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke

脚本实现

[root@localhost ~]# cat /opt/connect_serial.sh
#!/bin/bash

function usage()
{
    cat <<!
    Usage:
    $0 <device> <port speed>
    Example: $0 /dev/ttyS0 115200
!
    exit 255
}

function clean()
{
    # Terminate background read process
    kill $bgPid
    echo
    exit 0
}

# "Ctrl c" to exit
trap clean SIGINT
$# -ne 2 ] && usage

device_name=$1
port_speed=$2
# Set up device
stty -F $device_name $port_speed min 100 time 2 brkint ignpar \
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread clocal -crtscts \
-ignbrk brkint ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl ixon -ixoff \
-iuclc -ixany -imaxbel -iutf8 \
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 \
-isig -icanon iexten -echo echoe echok -echonl -noflsh -xcase -tostop -echoprt \
echoctl echoke

# Let cat read the device $device_name in the background
cat $device_name &

# Capture PID of background process so it is possible to terminate it when done
bgPid=$!

# Read commands from user, send them to device $device_name
echo > $device_name
while read cmd
do
    echo "$cmd" > $device_name
    [ "$cmd" == "exit" -o "$cmd" == "quit" ] && clean
done

实际使用

此处只作交互式演示,若须要作非交互式测试等其余需求,可自行修改输入输出逻辑,或使用expect实现

[root@localhost ~]# /opt/connect_serial.sh /dev/ttyUSB0 115200
000 login: 
UUT000000000000 login: admin
admin
Password: 123456

(audit 139) Sat Jan-01 2000-16:45:08 : [SERIAL] Login from IP:127.0.0.1 user:admin (strlen:70)
[2866 : 2866 INFO]SERIAL Login from IP:127.0.0.1 user:admin

old_active_active_session:0
current_active_sessions:1

# ls
ls
# pwd
pwd
/root
# exit

[root@localhost ~]#


往期精选

shell 多线程及线程数控制实现

tput应用:简易时钟实现

shell脚本一步完成多层ssh跳转时的文件传输


长按关注
                       


以为好看就点一哈!

本文分享自微信公众号 - WriteSimpleDemo(this_is_a_wechat)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。

相关文章
相关标签/搜索