-
Notifications
You must be signed in to change notification settings - Fork 5
/
merc230.sh
495 lines (423 loc) · 17.3 KB
/
merc230.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/bin/sh
#
# Чтение данных из счетчиков электроэнергии
# Merciry 230
#
# 2015-07-07 version 1
# 2016-05-15 version 2
# kohan pavel e-mail: [email protected] ICQ:92819905
#
#
# version 2
#
RETRY_QUERY=1
MIN_BLOCK_SIZE=4
MAX_BLOCK_SIZE=255
START_BLOCK_DATA=1
T2_CORRECT=5812281
T3_CORRECT=1596500
# T3 2015-10-05 08:20:00 2885216 - 1228915 = 1656301
# T2 2015-10-05 08:20:00 7425400 - 1480530 = 5944970
# miliWATT !!
WATT_HOUR_CORRECTION_T2=5923
WATT_HOUR_CORRECTION_T3=5000
PASS_R=`StrToHex "${PASS_READ}"`
PASS_W=`StrToHex "${PASS_WRITE}"`
# modes list
MODES='all_time 0
curr_year 1
prior_year 2
month 3
today 4
yesterday 5
all_time_phase 6'
# tarifs list
ZONES='T_ALL 00
T1 01
T2 02
T3 03
T4 04
LOST 05
T_NEW 06'
#
POWERS='P 0
Q 1
S 2'
#PHAZA
PHAZAS='all 0
1 1
2 2
3 3'
# commands list
# alias command size answer parser format description
COMMANDS='test `form_com x${CSN}x00` 4 Тестирование связи
openread `form_com x${CSN}x01x01${PASS_R}` 4 Открытие соединения на чтение
openwrite `form_com x${CSN}x01x02${PASS_W}` 4 Открытие соединения на запись
close `form_com x${CSN}x02` 4 Завершение сеанса
kwatthour `form_com x${CSN}x05x${mode}${month}x${zona}` ${com_size} Опрос накопленной энергии params: mode[]:zona[]
amper `form_com x${CSN}x08x16x2${phaza}` ${com_size} Сила тока A (А) по фазам params: phaza[]
coefw `form_com x${CSN}x08x16x30` 15 Коэффициент мощности (С) по фазам
freq `form_com x${CSN}x08x16x40` 6 Частота Гц
freq2 `form_com x${CSN}x08x16x40` 6 "1,3,2#d#Aprintf(\"%0.2f\",^b0/100)#" Частота Гц
coin `form_com x${CSN}x08x16x5${phaza}` ${com_size} Угол между фазами params: phaza[]
power `form_com x${CSN}x08x16x0${mode}` ${com_size} Мощность P (Вт) по фазам params: POWERS[]:phaza[]
volt `form_com x${CSN}x08x16x1${phaza}` ${com_size} Напряжение U (В) params: phaza[]
null `form_com x${CSN}${mode}` ${MAX_BLOCK_SIZE} "${OutParser}" Произвольная команда
serialnum `form_com x${CSN}x08x00` ${MAX_BLOCK_SIZE} "d1,02d2,02d3,02d4#Sn\0=\0\t|02d5,02d6#Date\0\t#0#02d7#20\t" Серийный номер счетчика и даты выпуска
version `form_com x${CSN}x08x03` ${MAX_BLOCK_SIZE} "d1#version\0\t.#0#d2#\t.#0#d3" Версия ПО
datetime `form_com x${CSN}x04x00` ${MAX_BLOCK_SIZE} "3#0#2#_\t_#0#1#0#4#\0\t\0#0#5#\t.#0#6#\t.#0#7#\t\0#0#8" Дата время по счетчику
kwatthour_t2 `form_com x${CSN}x05x00x02` 19 Учетный счетчик, Т2
kwatthour_t3 `form_com x${CSN}x05x00x03` 19 Учетный счетчик, Т3
kwatthour_af `form_com x${CSN}x05x${mode}${month}x${zona}` ${com_size} Внутренний счетчик params: mode[]:month[]:zona[]
kwatthour_phaza `form_com x${CSN}x05x60x${zona}` ${com_size} "$((${phaza} * 4 - 2)),$((${phaza} * 4 - 3)),$((${phaza} * 4)),$((${phaza} * 4 - 1))#d" Накопленная энергия по указанной фазе params: zona[]:phaza[]
power_phaza `form_com x${CSN}x08x16x00` 15 "$((3 + ${phaza} * 3 - 2)),$((3 + ${phaza} * 3)),$((3 + ${phaza} * 3 - 1))#d#a3fffff" Мощность P (Вт) по указанной фазе params: phaza[]'
SetCounterSN()
{
COUNTER_SN=${COUNTER_SN:='00000000'}
CSN=`echo "${COUNTER_SN}" | awk '{printf "%02x", substr($0,length($0)-1,2)}'`
}
Dev_Enulator()
{
#echo "com=${1}" >&2
local command="`echo "${1}" | cut -d"x" -f3 | cut -c1-2`"
local command2="`echo "${1}" | cut -d"x" -f4 | cut -c1-2`"
local answer=""
case $command in
# Тестирование связи
# 11 00 |0D E0
# 11 00 |0D E0 - ответ : Ok
'00') answer="`form_com x${CSN}x00`" ;;
'08') {
case $command2 in
# Частота Гц
# 11 08 16 40 |8B 4A
# 11 00 8C 13 |20 15 - ответ : 50,04
'16') answer="`form_com x${CSN}x00x8Cx13`" ;;
# Серийный номер счетчика и даты выпуска
# 11 08 00 |26 05
# 11 09 1B 4C 11 02 08 0B |E9 15 - ответ : 9277617 02082011
'00') answer="`form_com x${CSN}x09x1Bx4Cx11x02x08x0B`" ;;
# Версия ПО
# 11 08 03 |66 04
# 11 02 03 01 |65 E8 - ответ : 2.3.1
'03') answer="`form_com x${CSN}x02x03x01`" ;;
esac
} ;;
# Дата время по счетчику
# 11 04 00 |23 05
# 11 13 58 13 02 17 05 16 00 |32 CA - ответ : 13_58_13 02 17.05.16 00
'04') answer="`form_com x${CSN}x13x58x13x02x17x05x16x00`" ;;
esac
echo "answer=${answer}" >&2
HexToChar "${answer}" > "${2}"
}
SetParams()
{
# определить имя команды
com_string=${1}
[ -z "${com_string}" ] && com_string="kwatthour"
com_name="`echo ${com_string} | cut -d":" -f1`"
[ -z "$com_name" ] && com_name=$com_string
com="${com_name} `CheckParameter "command" "${com_name}" "${COMMANDS}" "" "Command \"$com_name\" unknown!"`"
[ $? -ne 0 ] && return 1
cmd="`echo "${com}" | cut -d" " -f2`"
com_size="`echo "${com}" | cut -d" " -f3`"
outparser="`echo "${com}" | cut -d" " -f4`"
name="`echo "${com}" | cut -d" " -f5`"
#echo "$com_name $cmd $com_size" >&2
case $com_name in
'null') {
ParseNullCom "${com_string}"
PARAMS="\"${com_string}\"> mode="${mode}" OutParser="$OutParser
};;
'volt'|'amper'|'coin') {
phaza="`echo ${com_string} | cut -d":" -f2`"
if [ "$phaza" != "$com_name" ]; then
[ -z "$phaza" ] && {
phaza=1
}
else
phaza=1
fi
case $phaza in
1) com_size=12 ;;
2) com_size=9 ;;
3) com_size=6 ;;
*) com_size=12 ;;
esac
PARAMS="\"${com_string}\"> phaza="${phaza}
};;
'power') {
typpowerr="`echo ${com_string} | cut -d":" -f2`"
typpower=0
phaza=0
if [ "$typpowerr" != "$com_name" ]; then
typpower="`echo "${POWERS}" | sed -n 's/^'${typpowerr}' \(.*\)$/\1/p'`"
[ -z "${typpower}" ] && {
echo "Parameter \"$typpowerr\" unknown!" >&2
typpower=0
}
phazar=`echo ${com_string} | sed -n 's/^[^:]*:[^:]*:\(.*\)$/\1/p'`
phaza="`CheckParameter phaza "${phazar}" "${PHAZAS}" '1'`"
fi
mode=`printf "%X" $(expr ${typpower} \* 4 + $phaza)`
case $phaza in
'0') com_size=15 ;;
*) com_size=12 ;;
esac
PARAMS="\"${com_string}\"> mode="$mode", typpower="${typpower}", phaza="${phaza}
};;
'kwatthour'|'kwatthour_af') {
params="`echo ${com_string} | sed -n 's~^[^:]*:\(.*\)~\1~p'`"
#echo "_$params" >&2
mode='0'
zona='0'
month='0'
moder="`echo $params | cut -d':' -f1`"
#echo $moder >&2
if [ -n "$moder" ]; then
month=`echo "${moder}" | sed -n 's/^[^=]*=\(.*\)$/\1/p'`
if [ -n "${month}" ]; then
moder=`echo "${moder}" | cut -d"=" -f1`
else
month='0';
fi
mode="`CheckParameter mode "${moder}" "${MODES}" '0'`"
if [ $mode = '3' ]; then
#echo $month >&2
[ $month -gt 12 -o $month -lt 1 ] && month='1'
month=`printf "%X" $((10#$month))`
else
month='0'
fi
zoner=`echo ${params} | sed -n 's/^[^:]*:\(.*\)$/\1/p'`
zona="`CheckParameter zona "${zoner}" "${ZONES}" '00'`"
fi
zona=`printf "%02X" $zona`
if [ $zona -lt 6 ]; then
case $mode in
'0'|'1'|'2'|'3'|'4'|'5') com_size=19 ;;
'6') com_size=15 ;;
*) com_size=19 ;;
esac
else
com_size=$MAX_BLOCK_SIZE
fi
PARAMS="\"${com_string}\"> mode="${mode}", zona="${zona}", month="${month}
};;
'kwatthour_phaza')
zoner=`echo ${com_string} | sed -n 's/^[^:]*:\([^:]*\).*$/\1/p'`
zona="`CheckParameter zona "${zoner}" "${ZONES}" '00'`"
phazar=`echo ${com_string} | sed -n 's/^[^:]*:[^:]*:\(.*\)$/\1/p'`
phaza="`CheckParameter phaza "${phazar}" "${PHAZAS}" '1'`"
com_size=15
PARAMS="\"${com_string}\"> zona="${zona}", phaza="${phaza}
;;
'power_phaza')
phazar=`echo ${com_string} | sed -n 's/^[^:]*:\(.*\)$/\1/p'`
phaza="`CheckParameter phaza "${phazar}" "${PHAZAS}" '1'`"
PARAMS="\"${com_string}\"> phaza="${phaza}
;;
*) PARAMS="none" ;;
esac
eval cmd=$cmd
eval com_size=$com_size
eval outparser="$outparser"
[ $DEBUG -gt 0 ] && echo "COMMAND: $com_name; PARAMS: ${PARAMS}; SIZE: $com_size; OUTPARSER: $outparser; DESCRIBE: ${name}" >&2
#echo "$com_name $cmd $com_size $outparser $name"
return 0
}
# CorrectionCounter $data 2 $T2_CORRECT
CorrectionCounter()
{
local data="${1}"
case "$2" in
'2') correct_watt="`sh ${BASE_DIR}correction.sh 2 ${WATT_HOUR_CORRECTION_T2} 2>> ${BASE_DIR}error_corr.log`" ;;
'3') correct_watt="`sh ${BASE_DIR}correction.sh 3 ${WATT_HOUR_CORRECTION_T3} 2>> ${BASE_DIR}error_corr.log`" ;;
esac
[ $? -eq 1 ] && echo "T${2} `date +%Y%m%d%H%M%S`" >> ${BASE_DIR}merc230_last
#echo "2=\"$2\" WATT_HOUR_CORRECTION_T2=$WATT_HOUR_CORRECTION_T2 BASE_DIR=$BASE_DIR correct_watt=$correct_watt" >&2
echo "$1" | awk -Fx -v cor=$correct_watt -v cors=${3} 'function preob(val)
{
if(val=="FFFFFFFF")val=0000; val=sprintf("%d", "0x"val); oval=sprintf("%01d", val);
return oval;
};
{
ovalA=preob($3$2$5$4);
#ovalR=preob($11$10$13$12);
printf("%01d\n", ovalA - cors + cor);
}'
}
Parser_com()
{
com=$1
request=$2
leng=$3
outparser="$4"
#echo $outparser >&2
lenn=${#request}
#echo $com $request $leng
de=`expr ${lenn} - 6`
sn=`echo $request | cut -c1-3`
snd=`printf "%d" 0$sn`
data=`echo $request | cut -c4-$de`
crc=`echo $request | cut -c$(expr $de + 1)-$lenn`
#echo sn=$sn data=$data crc=$crc
if [ $leng -eq $MIN_BLOCK_SIZE ]; then
case $data in
# Ok
x00) {
echo "$com - Ok [${snd}]"
return 0
};;
# Недопустимая команда или параметр.
x01) {
echo "ERROR: Invalid command or parameter [device: ${snd}]."
return 1
};;
# Внутренняя ошибка счетчика.
x02) {
echo "ERROR: Internal error counter [device: ${snd}]."
return 2
};;
# Не достаточен уровень доступа для удовлетворения запроса.
x03) {
echo "ERROR: Not sufficient level of access to satisfy the request [device: ${snd}]."
return 3
};;
# Внутренние часы счетчика уже корректировались в течение текущих суток
x04) {
echo "ERROR: The internal clock counter has been adjusted for the current day [device: ${snd}]."
return 4
};;
# Не открыт канал связи
x05) {
echo "ERROR: Do not open the link [device:${snd}]. Need openread"
return 5
};;
*) {
echo "ERROR: Unknown code \"$data\" [device: ${snd}]."
return 6
};;
esac
else
# ParserCommand $data "01D1"
if [ -n "$outparser" ]; then
#echo $outparser >&2
rezout="`DoOutParser $outparser`"
echo "${rezout}"
return 255
fi
case $com in
'null') {
echo $data outparser=$outparser >&2
};;
'freq') {
echo $data | awk -Fx 'function preob(name,val)
{
val=sprintf("%d", "0x"val); val=val / 100; printf("%s = %01.02f\n", name, val);
};
{preob("Freq(Hr)", $2$4$3)}'
};;
'amper') {
echo $data | awk -Fx 'function preob(name,val)
{
val=sprintf("%d", "0x"val); val=val / 1000; printf("%s = %01.03f\n", name, val);
};
{preob("Ph1(A)", $2$4$3);
preob("Ph2(A)", $5$7$6);
preob("Ph3(A)", $8$10$9)}'
};;
'volt') {
echo $data | awk -Fx 'function preob(name,val)
{
val=sprintf("%d", "0x"val); val=val / 100; printf("%s = %01.02f\n", name, val);
};
{preob("Ph1(V)", $2$4$3);
preob("Ph2(V)", $5$7$6);
preob("Ph3(V)", $8$10$9)}'
};;
'power') {
echo $data | awk -Fx 'function preob(name,val)
{
val=sprintf("%d", "0x"val); printf("%s = %01d\n", name, val);
};
{preob("Phs(W)", $2$4$3);
preob("Ph1(W)", $5$7$6);
preob("Ph2(W)", $8$10$9);
preob("Ph3(W)", $11$13$12)}' |\
(while read i1 i2 i3; do
zn=$((i3 & 0x3FFFFF))
dl=${#zn}
if [ $dl -gt 2 ]; then
st=`expr $dl - 2`
zn2=`echo ${zn} | cut -c$((${st} + 1))-$((${st} + 2)) | sed 's/^[0]*//'`
else
st=1
zn2=0
fi
#printf "%s %s %01d.%02d\n" $i1 $i2 ${zn:0:`expr $dl - 2`} ${zn:`expr $dl - 2`:2}; FOR BASH
#zn=123409
zn1=`echo ${zn} | cut -c1-${st}`
#echo "zn=$zn st=$st dl=$dl zn1=$zn1 zn2=$zn2" >&2
printf "%s %s %01d,%02d\n" ${i1} ${i2} $zn1 $zn2 # FOR SH
done)
};;
'coefw') {
echo $data | awk -Fx 'function preob(name,val)
{
val=sprintf("%d", "0x"val); printf("%s = %01d\n", name, val);
};
{preob("Cosf", $2$4$3);
preob("Cosf1", $5$7$6);
preob("Cosf2", $8$10$9);
preob("Cosf3", $11$13$12)}' |\
(while read i1 i2 i3; do
zn=$((i3 & 0x3FFFFF))
# dl=${#zn}
printf "%s %s 0.%03d\n" $i1 $i2 ${zn}
#printf "%s %s %01d\n" $i1 $i2 ${zn}
done)
};;
'kwatthour') {
echo $data | awk -Fx -v lenc=$leng 'function preob(name,val)
{
if(val=="FFFFFFFF")val=0000; val=sprintf("%d", "0x"val); val=val / 1000; printf("%s = %01.03f\n", name, val);
};
{
if (lenc==19) {
preob("Ap(kWh)", $3$2$5$4);
preob("Ao(kWh)", $7$6$9$8);
preob("Rp(kWh)", $11$10$13$12);
preob("Ro(kWh)", $15$14$17$16);
} else {
preob("Ap_F1(kWh)", $3$2$5$4);
preob("Ap_F2(kWh)", $7$6$9$8);
preob("Ap_F3(kWh)", $11$10$13$12);
};
}'
};;
'kwatthour_t2') {
CorrectionCounter $data 2 $T2_CORRECT
};;
'kwatthour_t3') {
CorrectionCounter $data 3 $T3_CORRECT
};;
'kwatthour_af') {
echo $data | awk -Fx -v lenc=$leng 'function preob(val)
{
if(val=="FFFFFFFF")val=0000; oval=sprintf("%d", "0x"val);
return oval;
};
{
if (lenc==19) {
printf("%01d\n%01d\n%01d\n%01d", preob($3$2$5$4), preob($7$6$9$8), preob($11$10$13$12), preob($15$14$17$16))
} else {
printf("%01d\n%01d\n%01d", preob($3$2$5$4), preob($7$6$9$8), preob($11$10$13$12))
};
}'
};;
esac
return 255
fi
}