myserial 3.0 KB

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