pepotocsctlとはTocos無線モジュールTOCOS TWE-LiteとToCoStickで温湿度計AM2321センサーを制御するプログラムです。

o 2015.2.23 リリース

o 2015.10.15 タイマーを5分へ変更

o リトライタイマー変更

o 2016.1.20バージョンアップ

o I2C温湿度計AM2321センサーのリード機能追加

o Tocos TWE-Lite DIP Ver1.6以上対応でチェックサム付加は省略しています

o 2016.10.31バージョンアップ

o 受信時にチェックサムをチェックするようにした

o AI1~4のアナログ入力リードコマンド追加

o 2017.3.23バージョンアップ

o ライトアフターリードコマンド追加

o ウエイトリードコマンド追加

pepotocsctl単独では動作しません、別途epiconが必要です

Tocos無線モジュールのDIOとAM2321センサーのみをコマンドで操作できます

root@tmp# pepotocsctl

root@tmp# pepotocosctl

usage /usr/local/bin/pepotocosctl read->chno:0-6 | write->chno:0-6 [DO:0|1|f] to 300000ms

DI(デジタルインプット)の4ポート分リード

root@tmp# pepowirelessdioctl 0 7

DI(デジタルインプット)の一番ポートリード

root@tmp# pepowirelessdioctl 1 1

DO(デジタルアウトプウト)一番ポートへ1をライト

root@tmp# pepowirelessdioctl 1 1 1

DO(デジタルアウトプウト)一番ポートへ1をライト、5秒間保持

root@tmp# pepowirelessdioctl 1 1 5000 0

I2C温湿度計AM2321センサーのリード

root@tmp# ./pepowirelessdioctl 5

2016/01/20,19:16:19,4.0℃,75.4%

アナログ入力リード

root@tmp# pepotocosctl 6

4080,4080,4080,4080

ライトアフターリード

root@tmp# pepotocosctl 0 0

7;4080,4080,4080,4080

デジタルインプットとアナログインプットリード

root@tmp# pepotocosctl 7

f;1952,4080,4080,4080

リードウエイト・DIの変化を指定時間待つ

root@tmp# pepotocosctl 7 120

e;4080,4080,4080,4080

Raspberry_piとTocos無線モジュールのコラボ動画→https://www.youtube.com/watch?v=-_FqOdLbzMw

Raspberry_pi%E8%A1%A8%E7%B4%99.PNG

  1. #!/bin/sh
  2. # licence GPLv2 ; this scripts designed by Isamu.Yamauchi 2015.2.10 update 2017.4.25 for i386 & raspberry pi
  3. # pepotocsctl for contorl TWE-Lite DIP:Ver1.6 or later(Tocos wireless engine) digital-IN/digital-OUT signal.
  4. # pepotocsctl $1:ch[0-7] [$2:0-f] [$3: timer->1 to 300000ms]
  5. PATH=$PATH:/usr/local/bin:/usr/local/sbin
  6. usage(){
  7. echo "usage $0 read->chno:0-6 | write->chno:0-7 [DO:0|1|f] [timer->1 to 300000ms]" >/dev/stderr
  8. exit
  9. }
  10. if [ $# -eq 0 ];then
  11. usage
  12. elif [ "$1" -gt 7 -o "$1" -lt 0 ];then
  13. usage
  14. elif [ "$1" -eq 6 -a $# -ne 1 ];then
  15. usage
  16. elif [ $# -eq 2 -a "$1" -eq 0 ];then
  17. ALLDO=NO
  18. if [ $((0x$2 - 0)) -gt $((0xf - 0)) -o $((0x$2 - 0)) -lt 0 ];then
  19. usage
  20. else
  21. ALLDO=YES
  22. fi
  23. elif [ $# -eq 3 ];then
  24. if [ "$2" -gt 0 -a "$2" -gt 1 -o "$3" -gt 300000 -o "$3" -lt 1 ];then
  25. usage
  26. fi
  27. fi
  28. prog=pepotocsctl
  29. HEDER=":"
  30. # am2321 i2c adderess 5C
  31. I2CADDR="5C"
  32. I2CHEAD="7888AA"
  33. I2COK="01"
  34. I2CNG="00"
  35. I2CTEPHUM="03020004"
  36. I2CWRITE="01"
  37. I2CREAD="02"
  38. I2CREAD_CT="0006"
  39. I2CWRITE_CT="0000"
  40. I2CAM2321_WAIT=1000
  41. I2CREADCT=28
  42. ADDR="788001"
  43. DUMMY="0000000000000000"
  44. DO4ON="0808"
  45. DO4OF="0008"
  46. ALLON="0F0F"
  47. ALLOF="0F00"
  48. ZEROHUM="0000"
  49. TEMPMAX="80"
  50. HUMMAX="100"
  51. WAIT=50
  52. TRY=5
  53. READTM=1
  54. READCT=49
  55. pid=`echo -en $$`
  56. CHECKPG_SUM=/usr/local/bin/pepochecksum
  57. DIR=/www/remote-hand/tmp
  58. ALIAS_DI=${DIR}/.alias_di
  59. CMD="${DIR}/.$prog.${pid}"
  60. TCOSWD=$DIR/.tocos_write_data
  61. tTCOSWD=$DIR/.tocos_write_data_tmp
  62. TCOSRD=$DIR/.tocos_read_data
  63. tTCOSRD=$DIR/.tocos_write_data_tmp
  64. [ -e $ALIAS_DI ] && . "$ALIAS_DI"
  65. [ ! -z "${TOCOS_TTY}" ] && DIO_DEV="/dev/"${TOCOS_TTY} || DIO_DEV=/dev/none_tocos
  66. if [ ! -e $DIO_DEV ];then
  67. [ -e $TCOSWD ] && rm -f $TCOSWD
  68. [ -e $TCOSRD ] && rm -f $TCOSRD
  69. [ -e $tTCOSWD ] && rm -f $tTCOSWD
  70. [ -e $tTCOSRD ] && rm -f $tTCOSRD
  71. echo -en "-1" >/dev/stderr
  72. exit
  73. fi
  74. LOCK=/var/lock/LCK..${TOCOS_TTY}
  75. error(){
  76. [ -e ${CMD} ] && rm -f ${CMD}
  77. exit 0
  78. }
  79. trap error SIGHUP SIGTERM SIGQUIT SIGKILL SIGINT SIGCHLD
  80. CMD_DIO="/usr/local/bin/epicon -s 115200 -ql ${DIO_DEV} -c ${CMD}"
  81. send_dio(){
  82. local send d4off d4on tmp SD RD retry_time
  83. SD=$1
  84. tmp=${ADDR}${SD}${DUMMY}
  85. send=${HEDER}${tmp}X
  86. tmp=${ADDR}${DO4OF}${DUMMY}
  87. d4of=${HEDER}${tmp}X
  88. tmp=${ADDR}${DO4ON}${DUMMY}
  89. d4on=${HEDER}${tmp}X
  90. RETRY=$TRY
  91. while [ -e $LOCK ]
  92. do
  93. retry_time=`echo -en $RANDOM |cut -c 1-3`
  94. msleep $retry_time
  95. RETRY=$(($RETRY - 1))
  96. if [ $RETRY -eq 0 ];then
  97. echo -en "-1" >/dev/stderr
  98. return
  99. fi
  100. done
  101. cat >${CMD}<<EOF
  102. #!/bin/sh
  103. RETRY=$TRY
  104. while [ \${RETRY} -ne 0 ];do
  105. retry_time=\`echo -en \$RANDOM |cut -c 1-2\`
  106. echo -en "${send}\\r\\n"
  107. msleep $WAIT
  108. echo -en "${d4on}\\r\\n"
  109. msleep $WAIT
  110. read -s -n ${READCT} -t ${READTM} RD || RD="-1"
  111. echo -en "${d4of}\\r\\n"
  112. TMP=\`echo -en \${RD} | wc -c\`
  113. [ \${TMP} -eq ${READCT} ] && break
  114. RETRY=\$((\${RETRY} - 1))
  115. msleep \$retry_time
  116. RD="-1"
  117. done
  118. echo -en \${RD} >/dev/stderr
  119. EOF
  120. msleep ${WAIT}
  121. chmod +x ${CMD}
  122. ${CMD_DIO}
  123. }
  124. i2c_read(){
  125. local send read retry_time tmp
  126. I2CCMD="00"
  127. I2CRES="00"
  128. RETRY=$TRY
  129. while [ -e $LOCK ]
  130. do
  131. retry_time=`echo -en $RANDOM |cut -c 1-3`
  132. msleep $retry_time
  133. RETRY=$(($RETRY - 1))
  134. if [ $RETRY -eq 0 ];then
  135. echo -en "-1" >/dev/stderr
  136. return
  137. fi
  138. done
  139. # wake-up
  140. tmp=${I2CHEAD}${I2CWRITE}${I2CADDR}${I2CWRITE_CT}
  141. wake_up=${HEDER}${tmp}X
  142. # temperature & Humidity start command
  143. tmp=${I2CHEAD}${I2CWRITE}${I2CADDR}${I2CTEPHUM}
  144. send=${HEDER}${tmp}X
  145. # temperature & Humidity read command
  146. tmp=${I2CHEAD}${I2CREAD}${I2CADDR}${I2CREAD_CT}
  147. read=${HEDER}${tmp}X
  148. cat >${CMD}<<EOF
  149. #!/bin/sh
  150. RETRY=$TRY
  151. I2CRD="-1"
  152. while [ \${RETRY} -ne 0 ];do
  153. retry_time=\`echo -en \$RANDOM |cut -c 1-2\`
  154. echo -en "${wake_up}\\r\\n"
  155. msleep ${WAIT}
  156. read -s -t ${READTM} I2CRD || I2CRD="-1"
  157. echo -en "${send}\\r\\n"
  158. msleep ${WAIT}
  159. read -s -t ${READTM} I2CRD || I2CRD="-1"
  160. msleep ${AM2321_WAIT}
  161. echo -en "${read}\\r\\n"
  162. msleep ${WAIT}
  163. read -s -n ${I2CREADCT} -t ${READTM} I2CRD || I2CRD="-1"
  164. TMP=\`echo -en \${I2CRD} | wc -c\`
  165. [ \${TMP} -eq ${I2CREADCT} ] && break
  166. RETRY=\$((\${RETRY} - 1))
  167. [ \${RETRY} -eq 0 ] && break
  168. RETRY=\$((\${RETRY} - 1))
  169. msleep \$retry_time
  170. I2CRD="-1"
  171. done
  172. echo -en \${I2CRD} >/dev/stderr
  173. EOF
  174. chmod +x ${CMD}
  175. ${CMD_DIO}
  176. }
  177. read_temp_hum(){
  178. local RD TMP CHECK_DATA SUM RDCHECKPG_SUM
  179. RD="-1"
  180. RD=`i2c_read 2>&1`
  181. TMP=`echo -en ${RD} |wc -c`
  182. if [ $TMP -ne ${I2CREADCT} ];then
  183. echo -en "-1" >/dev/stderr
  184. return
  185. fi
  186. CHECK_DATA=`echo -en ${RD}|awk '{printf (substr($1,2,length($1)-2))}'`
  187. RDCHECKPG_SUM=`echo -en ${RD} |awk '{printf(substr($1,length($1)-2,2))}'`
  188. SUM=`${CHECKPG_SUM} ${CHECK_DATA}`
  189. if [ ${RDCHECKPG_SUM} != ${SUM} ];then
  190. echo -en "-1" >/dev/stderr
  191. return
  192. fi
  193. HUMTMP=`echo -en ${RD} |awk '{printf(substr($1,18,4))}'`
  194. if [ $HUMTMP = $ZEROHUM ];then
  195. echo -en "-1" >/dev/stderr
  196. return
  197. fi
  198. COMMAND=`echo -en ${RD} |awk '{printf(substr($1,8,2))}'`
  199. if [ $COMMAND != $I2CREAD ];then
  200. echo -en "-1" >/dev/stderr
  201. return
  202. fi
  203. RESULT=`echo -en ${RD} |awk '{printf(substr($1,10,2))}'`
  204. if [ $RESULT != $I2COK ];then
  205. echo -en "-1" >/dev/stderr
  206. return
  207. fi
  208. NOWDATE=`date '+%Y/%m/%d,%H:%M:%S'|awk '{printf("%s",$1)}'`
  209. HUMH=`echo -en ${RD} |awk '{printf(substr($1,18,2))}'`
  210. HUMLH=`echo -en ${RD} |awk '{printf(substr($1,20,1))}'`
  211. HUMLL=`echo -en ${RD} |awk '{printf(substr($1,21,1))}'`
  212. DHUMH=$((0x$HUMH * 256))
  213. DHHML=$((0x$HUMLH * 16))
  214. HUM=$(($(($(($DHUMH + $DHHML)) + 0x$HUMLL)) / 10))
  215. HUMMOD=$(($(($(($DHUMH + $DHHML)) + 0x$HUMLL)) % 10))
  216. TEMPH=`echo -en ${RD} |awk '{printf(substr($1,22,2))}'`
  217. TEMPLH=`echo -en ${RD} |awk '{printf(substr($1,24,1))}'`
  218. TEMPLL=`echo -en ${RD} |awk '{printf(substr($1,25,1))}'`
  219. if [ $((0x$TEMPH & 0x80)) != 0 ];then
  220. TEMPH=$((0x$TEMPH & 0x7F))
  221. TMP="minus"
  222. else
  223. TMP="plus"
  224. fi
  225. DTEMPH=$((0x$TEMPH * 256))
  226. DTEMPLH=$((0x$TEMPLH * 16))
  227. TEMP=$(($(($(($DTEMPH + $DTEMPLH)) + 0x$TEMPLL)) / 10))
  228. TEMPMOD=$(($(($(($DTEMPH + $DTEMPLH)) + 0x$TEMPLL)) % 10))
  229. if [ ${TEMP} -gt ${TEMPMAX} -o ${HUM} -gt ${HUMMAX} ];then
  230. echo -en "-1" >/dev/stderr
  231. return
  232. fi
  233. echo -en ${NOWDATE} ${TMP} ${TEMP} ${TEMPMOD} ${HUM} ${HUMMOD}|awk '{
  234. I=""
  235. if ($2 != "plus") I="-"
  236. printf("%s,%s%s.%s℃,%s.%s%",$1,I,$3,$4,$5,$6)}'>/dev/stderr
  237. }
  238. write_dio(){
  239. # write_dio CH 0-1 Timer
  240. local RD SD CH DO DATA TM TMP
  241. [ $# -eq 3 ] && TM=$3 || TM=0
  242. SD=$(($2 ^ 1))
  243. case $1 in
  244. 0) CH="0F" ;;
  245. 1) CH="01" ;;
  246. 2) CH="02" ;;
  247. 3) CH="04" ;;
  248. 4) CH="08" ;;
  249. esac
  250. if [ $CH = "0F" ];then
  251. case $2 in
  252. 0) SD="0F" ;;
  253. 1) SD="0E" ;;
  254. 2) SD="0D" ;;
  255. 3) SD="0C" ;;
  256. 4) SD="0B" ;;
  257. 5) SD="0A" ;;
  258. 6) SD="09" ;;
  259. 7) SD="08" ;;
  260. 8) SD="07" ;;
  261. 9) SD="06" ;;
  262. A) SD="05" ;;
  263. B) SD="04" ;;
  264. C) SD="03" ;;
  265. D) SD="02" ;;
  266. E) SD="01" ;;
  267. F) SD="00" ;;
  268. esac
  269. DATA="$SD"${CH}
  270. RD=`send_dio ${DATA} 2>&1`
  271. TMP=`echo -en ${RD} | wc -c`
  272. [ ${TMP} != ${READCT} ] && RD="-1" || SD=$RD
  273. else
  274. [ $SD -eq 1 ] && DATA=${CH}${CH} || DATA="00"${CH}
  275. RD=`send_dio ${DATA} 2>&1`
  276. if [ ${TM} -ne 0 ];then
  277. SD=$(($SD ^ 1))
  278. [ `echo -en ${RD} | wc -c` != ${READCT} ] && RD="-1" || RD=$2
  279. if [ ${RD} != "-1" ];then
  280. msleep ${TM}
  281. [ $SD -eq 1 ] && DATA=${CH}${CH} || DATA="00"${CH}
  282. RD=`send_dio ${DATA} 2>&1`
  283. fi
  284. fi
  285. SD=$(($SD ^ 1))
  286. TMP=`echo -en ${RD} | wc -c`
  287. [ ${TMP} != ${READCT} ] && RD="-1" || RD=$SD
  288. fi
  289. if [ ${RD} != "-1" ];then
  290. echo -en ${SD} >/dev/stderr
  291. else
  292. echo -en -1 >/dev/stderr
  293. fi
  294. }
  295. read_dio(){
  296. local CH XCH DATA RD RESULT TMP CHECK_DATA SUM RDCHECKPG_SUM
  297. case $1 in
  298. 0) CH="0F" ; XCH=0xff ;;
  299. 1) CH="01" ; XCH=0x01 ;;
  300. 2) CH="02" ; XCH=0x02 ;;
  301. 3) CH="04" ; XCH=0x04 ;;
  302. 4) CH="08" ; XCH=0x08 ;;
  303. esac
  304. DATA=${DO4OF}
  305. RD=`send_dio ${DATA} 2>&1`
  306. TMP=`echo -en ${RD} | wc -c`
  307. if [ ${TMP} != ${READCT} ];then
  308. echo -en "-1" >/dev/stderr
  309. return
  310. fi
  311. CHECK_DATA=`echo -en ${RD}|awk '{printf (substr($1,2,length($1)-2))}'`
  312. RDCHECKPG_SUM=`echo -en ${RD} |awk '{printf(substr($1,length($1)-1,2))}'`
  313. SUM=`${CHECKPG_SUM} ${CHECK_DATA}`
  314. if [ ${RDCHECKPG_SUM} != ${SUM} ];then
  315. echo -en "-1" >/dev/stderr
  316. return
  317. fi
  318. TMP=`echo -en ${RD} | awk '{i=substr(\$0,34,2);printf i}'`
  319. if [ $1 -eq 0 ];then
  320. DATA=$((0x${TMP} & 0x${CH}))
  321. DATA=$(($DATA ^ 0x0f))
  322. else
  323. DATA=$(($((0x${TMP} & 0x${CH})) / ${XCH}))
  324. DATA=$(($DATA ^ 0x01))
  325. fi
  326. printf "%1x" ${DATA} >/dev/stderr
  327. }
  328. read_ai(){
  329. local DATA RD TMP VOLT A1VOLT A2VOLT A3VOLT A4VOLT A1 A2 A3 A4 AISUB CHECK_DATA SUM RDCHECKPG_SUM
  330. DATA=${DO4OF}
  331. RD=`send_dio ${DATA} 2>&1`
  332. TMP=`echo -en ${RD} | wc -c`
  333. if [ ${TMP} != ${READCT} ];then
  334. echo -en "-1" >/dev/stderr
  335. return
  336. fi
  337. CHECK_DATA=`echo -en ${RD}|awk '{printf (substr($1,2,length($1)-2))}'`
  338. RDCHECKPG_SUM=`echo -en ${RD} |awk '{printf(substr($1,length($1)-1,2))}'`
  339. SUM=`${CHECKPG_SUM} ${CHECK_DATA}`
  340. if [ ${RDCHECKPG_SUM} != ${SUM} ];then
  341. echo -en "-1" >/dev/stderr
  342. return
  343. fi
  344. TMP=`echo -en ${RD} | awk '{i=substr(\$0,46,2);printf i}'`
  345. A1=`echo -en ${RD} | awk '{i=substr(\$0,38,2);printf i}'`
  346. VOLT=$((0x$A1 * 4))
  347. AISUB=$(($TMP & 0x03))
  348. A1VOLT=$(($(($VOLT + $AISUB)) * 4))
  349. A2=`echo -en ${RD} | awk '{i=substr(\$0,40,2);printf i}'`
  350. AISUB=$(($(($TMP >> 2 )) & 0x03))
  351. VOLT=$((0x$A2 * 4))
  352. A2VOLT=$(($(($VOLT + $AISUB)) * 4))
  353. A3=`echo -en ${RD} | awk '{i=substr(\$0,42,2);printf i}'`
  354. AISUB=$(($(($TMP >> 4 )) & 0x03))
  355. VOLT=$((0x$A3 * 4))
  356. A3VOLT=$(($(($VOLT + $AISUB)) * 4))
  357. A4=`echo -en ${RD} | awk '{i=substr(\$0,44,2);printf i}'`
  358. AISUB=$(($(($TMP >> 6 )) & 0x03))
  359. VOLT=$((0x$A4 * 4))
  360. A4VOLT=$(($(($VOLT + $AISUB)) * 4))
  361. printf "%4d,%4d,%4d,%4d" ${A1VOLT} ${A2VOLT} ${A3VOLT} ${A4VOLT} >/dev/stderr
  362. }
  363. wait_di(){
  364. local RD READWAIT TMP
  365. READWAIT=$1
  366. cat >${CMD}<<EOF
  367. #!/bin/sh
  368. RD="-1"
  369. read -s -n ${READCT} -t ${READWAIT} RD || RD="-1"
  370. if [ \${RD} = "-1" ];then
  371. echo -en "-1" >/dev/stderr
  372. exit
  373. fi
  374. TMP=\`echo -en \${RD} | wc -c\`
  375. if [ \${TMP} -eq ${READCT} ];then
  376. echo -en \${RD} >/dev/stderr
  377. else
  378. echo -en "-1" >/dev/stderr
  379. fi
  380. EOF
  381. msleep ${WAIT}
  382. chmod +x ${CMD}
  383. ${CMD_DIO}
  384. }
  385. cmd_di(){
  386. local RD TMP DATA
  387. RD=$1
  388. TMP=`echo -en ${RD} | awk '{i=substr(\$0,34,2);printf i}'`
  389. DATA=$((0x${TMP} & 0x0F))
  390. DATA=$(($DATA ^ 0x0f))
  391. printf "%1x" ${DATA} >/dev/stderr
  392. }
  393. cmd_ai(){
  394. local DATA RD TMP VOLT A1VOLT A2VOLT A3VOLT A4VOLT A1 A2 A3 A4 AISUB
  395. RD=$1
  396. TMP=`echo -en ${RD} | awk '{i=substr(\$0,46,2);printf i}'`
  397. A1=`echo -en ${RD} | awk '{i=substr(\$0,38,2);printf i}'`
  398. VOLT=$((0x$A1 * 4))
  399. AISUB=$(($TMP & 0x03))
  400. A1VOLT=$(($(($VOLT + $AISUB)) * 4))
  401. A2=`echo -en ${RD} | awk '{i=substr(\$0,40,2);printf i}'`
  402. AISUB=$(($(($TMP >> 2 )) & 0x03))
  403. VOLT=$((0x$A2 * 4))
  404. A2VOLT=$(($(($VOLT + $AISUB)) * 4))
  405. A3=`echo -en ${RD} | awk '{i=substr(\$0,42,2);printf i}'`
  406. AISUB=$(($(($TMP >> 4 )) & 0x03))
  407. VOLT=$((0x$A3 * 4))
  408. A3VOLT=$(($(($VOLT + $AISUB)) * 4))
  409. A4=`echo -en ${RD} | awk '{i=substr(\$0,44,2);printf i}'`
  410. AISUB=$(($(($TMP >> 6 )) & 0x03))
  411. VOLT=$((0x$A4 * 4))
  412. A4VOLT=$(($(($VOLT + $AISUB)) * 4))
  413. printf "%4d,%4d,%4d,%4d" ${A1VOLT} ${A2VOLT} ${A3VOLT} ${A4VOLT} >/dev/stderr
  414. }
  415. read_di_ai(){
  416. local DATA DATA1 RD TMP CHECK_DATA RDCHECKPG_SUM SUM
  417. DATA=${DO4OF}
  418. RD=`send_dio ${DATA} 2>&1`
  419. TMP=`echo -en ${RD} | wc -c`
  420. if [ ${TMP} != ${READCT} ];then
  421. echo -en "-1" >/dev/stderr
  422. return
  423. fi
  424. CHECK_DATA=`echo -en ${RD}|awk '{printf (substr($1,2,length($1)-2))}'`
  425. RDCHECKPG_SUM=`echo -en ${RD} |awk '{printf(substr($1,length($1)-1,2))}'`
  426. SUM=`${CHECKPG_SUM} ${CHECK_DATA}`
  427. if [ ${RDCHECKPG_SUM} != ${SUM} ];then
  428. echo -en "-1" >/dev/stderr
  429. return
  430. fi
  431. DATA=`cmd_di ${RD} 2>&1`
  432. DATA1=`cmd_ai ${RD} 2>&1`
  433. echo -en ${DATA}";"${DATA1} >/dev/stderr
  434. }
  435. read_di_ai_wait(){
  436. local DATA DATA1 RD TMP CHECK_DATA RDCHECKPG_SUM SUM
  437. DATA=$1
  438. RD=`wait_di ${DATA} 2>&1`
  439. TMP=`echo -en ${RD} | wc -c`
  440. if [ ${TMP} != ${READCT} ];then
  441. echo -en "-1" >/dev/stderr
  442. return
  443. fi
  444. TMP=`echo -en ${RD} | wc -c`
  445. CHECK_DATA=`echo -en ${RD}|awk '{printf (substr($1,2,length($1)-2))}'`
  446. RDCHECKPG_SUM=`echo -en ${RD} |awk '{printf(substr($1,length($1)-1,2))}'`
  447. SUM=`${CHECKPG_SUM} ${CHECK_DATA}`
  448. if [ ${RDCHECKPG_SUM} != ${SUM} ];then
  449. echo -en "-1" >/dev/stderr
  450. return
  451. fi
  452. DATA=`cmd_di ${RD} 2>&1`
  453. DATA1=`cmd_ai ${RD} 2>&1`
  454. echo -en ${DATA}";"${DATA1} >/dev/stderr
  455. }
  456. write_do_rd(){
  457. local RD TMP
  458. RD=`write_dio $1 $2 $3 2>&1`
  459. if [ "$RD" != "-1" ];then
  460. if [ $1 -eq 0 ];then
  461. DATA=`cmd_di ${RD} 2>&1`
  462. DATA1=`cmd_ai ${RD} 2>&1`
  463. echo -en ${DATA}";"${DATA1} >/dev/stderr
  464. return
  465. else
  466. echo -en ${RD} >/dev/stderr
  467. return
  468. fi
  469. fi
  470. echo -en ${RD} >/dev/stderr
  471. }
  472. if [ $# -eq 1 ];then
  473. if [ $1 -eq 5 ];then
  474. read_temp_hum
  475. elif [ $1 -eq 6 ];then
  476. read_ai
  477. elif [ $1 -eq 7 ];then
  478. read_di_ai
  479. else
  480. read_dio $1
  481. fi
  482. elif [ $# -eq 2 -a $1 -eq 7 ];then
  483. read_di_ai_wait $2
  484. elif [ $# -eq 2 ];then
  485. write_do_rd $1 $2
  486. elif [ $# -eq 3 ];then
  487. write_do_rd $1 $2 $3
  488. fi
  489. rm -f ${CMD}