forked from byte4geek/SEPLOS_MQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_seplos_ha.sh
132 lines (109 loc) · 2.51 KB
/
query_seplos_ha.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
#!/bin/bash
DEV=/dev/ttyUSB0
ADDR=00
# Get a 4 ASCII digit number and divide by $1, precision $2 ( or 2dp. ) $3 == 1 for signed.
get_div()
{
N=${rdata:$OFFSET:4}
OFFSET=$((OFFSET + 4))
local P=${2:-2}
N=$(printf "%d" 0x$N)
[ "$3" = "1" -a $N -gt 32767 ] && N=$((N - 65536))
N=$(bc <<< "scale = $P; $N / $1")
}
read_serdata()
{
local len rdata tries=2 rd2
while [ "${rdata:0:1}" != "~" ]
do
tries=$((tries - 1))
[ $tries -le 0 ] && { echo "Failed to read start of input char (~), read \"$rdata\"" 1>&2 ; exit 1; }
read -r -t5 rdata <$DEV
done
len=${rdata:10:3}
len=$((0x$len))
while [ ${#rdata} -lt $((len + 17)) ]
do
read -r -t5 rd2 <$DEV
[ -z "$rd2" ] && { echo "Failed to read whole response." ; exit 2; }
rdata="$rdata$rd2"
done
if [ "$RQCODE" = "42" ]
then
OFFSET=19
[ "${rdata:7:2}" != "00" ] && { echo "Error code ${rdata:7:2}"; exit 3; }
local l NCELL=$(printf "%d" 0x${rdata:17:2})
# echo "$NCELL"
for l in $(seq 1 $NCELL)
do
local V=$(printf "%d" 0x${rdata:$OFFSET:4})
OFFSET=$((OFFSET + 4))
echo "${V}"
done
local NTEMPS=$(printf "%d" 0x${rdata:$OFFSET:2})
OFFSET=$((OFFSET + 2))
local TSTR="Cell Temp "
for l in $(seq 1 $NTEMPS)
do
[ $l -eq $((NTEMPS - 1)) ] && TSTR="Environmental Temp"
[ $l -eq $((NTEMPS)) ] && TSTR="Power Temp"
local T=$(printf "%d" 0x${rdata:$OFFSET:4})
OFFSET=$((OFFSET + 4))
T=$(bc <<< "scale = 1; ($T - 2731)/10")
echo "${T}"
done
get_div 100 2 1 # Can't use $() because that creates a subshell
echo "${N}"
get_div 100
echo "${N}"
get_div 100
echo "${N}"
OFFSET=$((OFFSET + 2))
get_div 100
echo "${N}"
get_div 10 1
echo "${N}"
get_div 100
echo "${N}"
get_div 1 0
echo "${N}"
get_div 10 1
echo "${N}"
get_div 100 2
echo "${N}"
else
echo "Response: \"$rdata\""
fi
}
# Todo... calculate length checksum and insert in send string.
stty -F $DEV sane -echo -echoe -echok 19200
SUM=0
export RQCODE=${1:0:2}
RQ="20${ADDR}46$1"
CMD=${RQ:0:8}
DATA=${RQ:8}
LEN=${#DATA}
LEN=$(printf "%03X" $LEN)
LENSUM=$((~(${LEN:0:1} + ${LEN:1:1} + ${LEN:2:1})))
LENSUM=$(printf "%X" $LENSUM)
LENSUM=${LENSUM:0-1:1}
LENSUM=$((0x$LENSUM + 1))
LENSUM=$(printf "%X" $LENSUM)
LENSUM=${LENSUM:0-1:1}
SEND=$CMD$LENSUM$LEN$DATA
for d in $(echo -n "$SEND" | od -An -td1)
do
SUM=$((SUM + $d))
done
SUM=$((~$SUM))
SUM="$(printf "%04X" $SUM)"
SUM="${SUM:0-4:4}"
SUM=$((0x$SUM + 1))
SUM=$(printf "%04X" $SUM)
SUM="${SUM:0-4:4}"
SEND="~$SEND$SUM\r"
#echo "Sending \"$SEND\""
read_serdata &
sleep 0.2
echo -e "$SEND" >$DEV
wait