123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- #########################################################################
- # File Name: myserial
- # Author: Dan.Cao
- # mail: caodan2519@gmail.com
- # Created Time: 2014年12月23日 星期二 11时57分14秒
- #
- # Description:
- # manage the ckermit, connect to the serial port
- # sudo permission my be needed by ckermit
- #
- # Date Author Comment
- # 2014/12/23 Dan.Cao first version
- # 2020/06/06 Dan.Cao generate ckermit configuration file
- # support input baudrate
- #########################################################################
- #!/bin/sh
- str=`ls /dev/ttyUSB* 2> /dev/null`
- if [ "${str}x" = "x" ]; then
- echo "not find the serial port!"
- exit 1
- fi
- serial_ports=(`echo $str | cut -d " " --output-delimiter=" " -f 1-`)
- serial_cnt=${#serial_ports[@]}
- echo "find $serial_cnt serial ports:"
- for i in `seq 0 $((serial_cnt - 1))`
- do
- echo -e "\t [$i]: \t $serial_ports[$i]"
- done
- echo -n "please select the port number:"
- read choice
- echo "choice is $choice"
- #1. $1是脚本的第一个参数,这里作为awk命令的第一个参数传入给awk命令。
- #2. 由于没有输入文件作为输入流,因此这里只是在BEGIN块中完成。
- #3. 在awk中ARGV数组表示awk命令的参数数组,ARGV[0]表示命令本身,ARGV[1]表示第一个参数。
- #4. match是awk的内置函数,返回值为匹配的正则表达式在字符串中(ARGV[1])的起始位置,没有找到返回0。
- #5. 正则表达式的写法已经保证了匹配的字符串一定是十进制的正整数,如需要浮点数或负数,仅需修改正则即可。
- #6. awk执行完成后将结果返回给isdigit变量,并作为其初始化值。
- #7. isdigit=`echo $1 | awk '{ if (match($1, "^[0-9]+$") != 0) print "true"; else print "false" }' `
- #8. 上面的写法也能实现该功能,但是由于有多个进程参与,因此效率低于下面的写法。
- isdigit=`awk 'BEGIN { if (match(ARGV[1],"^[0-9]+$") != 0) print "true"; else print "false" }' $choice`
- if [[ $isdigit != "true" ]]; then
- echo "please input a digit number"
- exit 1
- fi
- if [ $choice -ge $serial_cnt ]; then
- echo "input number must less than $serial_cnt"
- exit 1
- fi
- serial_port=${serial_ports[$choice]}
- echo "select port is $serial_port"
- # select Baudrate
- read -p "Enter the baudrate (115200): " baudrate
- if [ "${baudrate}x" == "x" ]; then
- baudrate=115200
- fi
- echo "baudrate=${baudrate}"
- #####################################################################
- # generate configurations
- #serial_port=dd
- # 本来sed替换的分隔符是 '/', 但是会和路径变量serial_port中的'/'冲突,
- # 所以使用冒号':' 代替之前的 '/' 分隔符
- #sed -i 's:set line .*$:set line '"$serial_port"':' ~/.kermrc
- CONFIG_FILE="/tmp/kermrc.${USER}"
- cat > ${CONFIG_FILE} << GEN_KERMRC
- set line ${serial_port}
- set speed ${baudrate}
- set carrier-watch off
- set handshake none
- set flow-control none
- robust
- set file type bin
- set file name lit
- set rec pack 1000
- set send pack 1000
- set window 5
- set session-log TIMESTAMPED-TEXT
- c
- GEN_KERMRC
- # start
- ckermit ${CONFIG_FILE}
|