-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart_cu.sh
executable file
·160 lines (152 loc) · 3.91 KB
/
uart_cu.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
if [ -e /dev/ttyUSB* ] ;then
sudo chmod 777 /dev/ttyUSB*
fi
cu_stat=''
NAMEPATH=${0%/*}/.name$1
USERLOGPATH=${0%/*}/.userlog
echo_info()
{
cd /dev
echo 当前主机连接的设备有:
echo --------------------------
if [ -e /dev/ttyUSB* ] ;then
ls ttyUSB*
else
echo 同学,没有串口连接啦!
fi
echo --------------------------
echo
echo 请使用uart [0-9]+$1串口
echo 例如:uart 0 $2 来$1串口0'('ttyUSB0')'
echo 例如:uart 1 $2 来$1串口1'('ttyUSB1')'
exit 0
}
cu_off()
{
cu_stat=(`ps -aux|grep "cu -l /dev/ttyUSB$1 -s 115200"|awk '{print $2}'`)
# kill `ps -a |grep cu|awk '{print $1}'`
if [ $# == 2 ];then
echo_info 释放 $2
else
if [ ${#cu_stat[*]} == 1 ]
then
echo
echo UART$1未使用 无需释放
echo
else
cu_stat=(`ps -aux|grep "cu -l /dev/ttyUSB$1 -s 115200"|awk '{print $2}'`)
kill ${cu_stat[0]}
kill ${cu_stat[1]}
sleep 2
echo
echo UART$1释放成功
echo
fi
fi
}
def_stat=""
stat=n
name=""
killall()
{
kill ${cu_stat[0]}
kill ${cu_stat[1]}
ret=(`ps -aux|grep "cu -l /dev/ttyUSB$1 -s 115200"|awk '{print $2}'`)
echo 杀除进程
# while [ ${ret}x != ${def_stat}x ]
while [ ${#ret[*]} != 1 ]
do
kill ${ret[0]}
sleep 3
ret=(`ps -aux|grep "cu -l /dev/ttyUSB$1 -s 115200"|awk '{print $2}'`)
done
# if [ ${ret}x == ${def_stat}x ]
if [ ${#ret[*]} == 1 ]
then
echo 开始强占
sleep 1
echo ttyUSB0 115200
cu -l /dev/ttyUSB$1 -s 115200
date >> ${USERLOGPATH}
echo UART$1 ${name} release >> ${USERLOGPATH}
echo >> ${USERLOGPATH}
else
kill ${cu_stat[0]}
kill ${cu_stat[1]}
echo 开始强占...
sleep 2
echo ttyUSB0 115200
cu -l /dev/ttyUSB$1 -s 115200
date >> ${USERLOGPATH}
echo UART$1 ${name} release >> ${USERLOGPATH}
echo >> ${USERLOGPATH}
fi
}
getusrname()
{
if [ ! -e ${NAMEPATH} ];then
echo 无法定位使用者
elif [ $# != 1 ];then
OLD_NAME=`cat ${NAMEPATH}`
echo 当前使用者: ${OLD_NAME}
fi
read -p "who are you:" name
if [ ${name}X == ${def_stat}X ];then
getusrname $1
else
# echo -${OLD_NAME}-
# echo -${name}-
# if [ ${name}X != ${OLD_NAME}X ];then
echo -*********************************
echo 释放串口请使用 ~. 这个.一定要输入
echo 或者在另一处终端使用 uart 0-9 off
echo -*********************************
echo ${name} > ${NAMEPATH}
date >> ${USERLOGPATH}
echo UART$1 ${name} >> ${USERLOGPATH} $2
# fi
fi
}
if [ $# == 0 ];then
echo_info 启动
elif [ $# == 2 -a "$2"=="off" ];then
cu_off $1
cu_off $1 'off'
exit 0
else
cu_stat=(`ps -aux|grep "cu -l /dev/ttyUSB$1 -s 115200"|awk '{print $2}'`)
fi
if [ ${#cu_stat[*]} == 1 ]
then
if [ ! -e "/dev/ttyUSB$1" ]
then
echo -*****************
echo UART$1串口未连接
echo -*****************
else
getusrname $1
echo UART$1连接中。。。
cu -l /dev/ttyUSB$1 -s 115200
date >> ${USERLOGPATH}
echo UART$1 ${name} release >> ${USERLOGPATH}
echo >> ${USERLOGPATH}
fi
else
echo 申请UART$1
echo ------------------注意--------------------
read -p "UART$1被使用中,强占否(yes(y)/no(n)):" stat
case $stat in
y)
getusrname $1 抢占使用
killall $1
;;
yes)
getusrname $1 抢占使用
killall $1
;;
*)
echo 放弃使用
echo 已停止使用串口
;;
esac
fi