internalComm.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. /*
  2. * internalComm.c
  3. *
  4. * Created on: 2019年5月7日
  5. * Author: foluswen
  6. */
  7. #include <sys/time.h>
  8. #include <sys/timeb.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include <sys/ipc.h>
  15. #include <sys/shm.h>
  16. #include <sys/shm.h>
  17. #include <sys/mman.h>
  18. #include <linux/wireless.h>
  19. #include <arpa/inet.h>
  20. #include <netinet/in.h>
  21. #include <unistd.h>
  22. #include <stdarg.h>
  23. #include <stdio.h> /*標準輸入輸出定義*/
  24. #include <stdlib.h> /*標準函數庫定義*/
  25. #include <unistd.h> /*Unix 標準函數定義*/
  26. #include <fcntl.h> /*檔控制定義*/
  27. #include <termios.h> /*PPSIX 終端控制定義*/
  28. #include <errno.h> /*錯誤號定義*/
  29. #include <errno.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <ctype.h>
  33. #include <ifaddrs.h>
  34. #include <math.h>
  35. #include "internalComm.h"
  36. #define PASS 1
  37. #define FAIL -1
  38. struct Address Addr={0x01,0x02,0x03,0x05,0x06,0xFF};
  39. struct Command Cmd={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x24,0x27,0x28,0x29,0x2C,0x81,0x83,
  40. 0x85,0x86,0x87,0x88,0x089,0x8A,0x8B,0x8C,0x90,0x93,0xe0,0xe1,0xe2,0xe3};
  41. int tranceiveRelDelayTime(int fd, unsigned char* cmd, unsigned char cmd_len, unsigned char* rx, unsigned short _delay)
  42. {
  43. int len;
  44. //sleep(2); //required to make flush work, for some reason
  45. tcflush(fd,TCIOFLUSH);
  46. if(write(fd, cmd, cmd_len) >= cmd_len)
  47. {
  48. usleep(_delay * 1000);
  49. len = read(fd, rx, 512);
  50. }
  51. else
  52. {
  53. #ifdef SystemLogMessage
  54. DEBUG_ERROR("Serial command %s response fail.\n", cmd);
  55. #endif
  56. }
  57. return len;
  58. }
  59. int tranceive(int fd, unsigned char* cmd, unsigned char cmd_len, unsigned char* rx)
  60. {
  61. int len;
  62. //sleep(2); //required to make flush work, for some reason
  63. tcflush(fd,TCIOFLUSH);
  64. if(write(fd, cmd, cmd_len) >= cmd_len)
  65. {
  66. usleep(15000);
  67. len = read(fd, rx, 512);
  68. }
  69. else
  70. {
  71. #ifdef SystemLogMessage
  72. DEBUG_ERROR("Serial command %s response fail.\n", cmd);
  73. #endif
  74. }
  75. return len;
  76. }
  77. unsigned char Query_FW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
  78. {
  79. unsigned char result = FAIL;
  80. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_FW_Ver, 0x00, 0x00, 0x00};
  81. unsigned char rx[512];
  82. unsigned char chksum = 0x00;
  83. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  84. if(len > 6)
  85. {
  86. if (len < 6+(rx[4] | rx[5]<<8))
  87. return result;
  88. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  89. {
  90. chksum ^= rx[6+idx];
  91. }
  92. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  93. (rx[2] == tx[1]) &&
  94. (rx[1] == tx[2]) &&
  95. (rx[3] == tx[3]))
  96. {
  97. memcpy(Ret_Buf->Version_FW, (char *)rx+6, (rx[4] | rx[5]<<8));
  98. *(Ret_Buf->Version_FW + 8) = 0x00;
  99. result = PASS;
  100. }
  101. }
  102. return result;
  103. }
  104. unsigned char Query_HW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
  105. {
  106. unsigned char result = FAIL;
  107. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_HW_Ver, 0x00, 0x00, 0x00};
  108. unsigned char rx[512];
  109. unsigned char chksum = 0x00;
  110. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  111. if(len > 6)
  112. {
  113. if (len < 6+(rx[4] | rx[5]<<8))
  114. return result;
  115. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  116. {
  117. chksum ^= rx[6+idx];
  118. }
  119. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  120. (rx[2] == tx[1]) &&
  121. (rx[1] == tx[2]) &&
  122. (rx[3] == tx[3]))
  123. {
  124. memcpy(Ret_Buf->Version_HW, (char *)rx+6, (rx[4] | rx[5]<<8));
  125. *(Ret_Buf->Version_HW + 8) = 0x00;
  126. result = PASS;
  127. }
  128. }
  129. return result;
  130. }
  131. unsigned char Query_Present_InputVoltage(unsigned char fd, unsigned char targetAddr, PresentInputVoltage *Ret_Buf)
  132. {
  133. unsigned char result = FAIL;
  134. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Present_InputVoltage, 0x00, 0x00, 0x00};
  135. unsigned char rx[512];
  136. unsigned char chksum = 0x00;
  137. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  138. if(len > 6)
  139. {
  140. if (len < 6+(rx[4] | rx[5]<<8))
  141. return result;
  142. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  143. {
  144. chksum ^= rx[6+idx];
  145. }
  146. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  147. (rx[2] == tx[1]) &&
  148. (rx[1] == tx[2]) &&
  149. (rx[3] == tx[3]))
  150. {
  151. Ret_Buf->inputType = rx[6];
  152. Ret_Buf->L1N_L12 =(rx[7] | (rx[8]<<8))/10.0;
  153. Ret_Buf->L2N_L23 =(rx[9] | (rx[10]<<8))/10.0;
  154. Ret_Buf->L3N_L31 =(rx[11] | (rx[12]<<8))/10.0;
  155. result = PASS;
  156. }
  157. }
  158. return result;
  159. }
  160. unsigned char Query_Present_OutputVoltage(unsigned char fd, unsigned char targetAddr, PresentOutputVoltage *Ret_Buf)
  161. {
  162. unsigned char result = FAIL;
  163. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Present_OutputVoltage, 0x00, 0x00, 0x00};
  164. unsigned char rx[512];
  165. unsigned char chksum = 0x00;
  166. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  167. if(len > 6)
  168. {
  169. if (len < 6+(rx[4] | rx[5]<<8))
  170. return result;
  171. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  172. {
  173. chksum ^= rx[6+idx];
  174. }
  175. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  176. (rx[2] == tx[1]) &&
  177. (rx[1] == tx[2]) &&
  178. (rx[3] == tx[3]))
  179. {
  180. Ret_Buf->behindFuse_Voltage_C1 =(rx[6] | (rx[7]<<8));
  181. Ret_Buf->behindRelay_Voltage_C1 =(rx[8] | (rx[9]<<8));
  182. if((rx[4] | rx[5]<<8) > 4)
  183. {
  184. Ret_Buf->behindFuse_Voltage_C2 =(rx[10] | (rx[11]<<8));
  185. Ret_Buf->behindRelay_Voltage_C2 =(rx[12] | (rx[13]<<8));
  186. }
  187. result = PASS;
  188. }
  189. }
  190. return result;
  191. }
  192. unsigned char Query_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Ret_Buf)
  193. {
  194. unsigned char result = FAIL;
  195. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Fan_Speed, 0x00, 0x00, 0x00};
  196. unsigned char rx[512];
  197. unsigned char chksum = 0x00;
  198. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  199. if(len > 6)
  200. {
  201. if (len < 6+(rx[4] | rx[5]<<8))
  202. return result;
  203. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  204. {
  205. chksum ^= rx[6+idx];
  206. }
  207. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  208. (rx[2] == tx[1]) &&
  209. (rx[1] == tx[2]) &&
  210. (rx[3] == tx[3]))
  211. {
  212. for(int idx=0;idx < 4;idx++)
  213. Ret_Buf->speed[idx] = (rx[6+(2*idx)] | (rx[6+(2*idx)+1]<<8));
  214. result = PASS;
  215. }
  216. }
  217. return result;
  218. }
  219. unsigned char Query_Temperature(unsigned char fd, unsigned char targetAddr, Temperature *Ret_Buf)
  220. {
  221. unsigned char result = FAIL;
  222. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Temperature, 0x00, 0x00, 0x00};
  223. unsigned char rx[512];
  224. unsigned char chksum = 0x00;
  225. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  226. if(len > 6)
  227. {
  228. if (len < 6+(rx[4] | rx[5]<<8))
  229. return result;
  230. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  231. {
  232. chksum ^= rx[6+idx];
  233. }
  234. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  235. (rx[2] == tx[1]) &&
  236. (rx[1] == tx[2]) &&
  237. (rx[3] == tx[3]))
  238. {
  239. for(int idx=0;idx < 4;idx++)
  240. Ret_Buf->temperature[idx] = rx[6+idx] - 60;
  241. result = PASS;
  242. }
  243. }
  244. return result;
  245. }
  246. unsigned char Query_Aux_PowerVoltage(unsigned char fd, unsigned char targetAddr, AuxPower *Ret_Buf)
  247. {
  248. unsigned char result = FAIL;
  249. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Aux_PowerVoltage, 0x00, 0x00, 0x00};
  250. unsigned char rx[512];
  251. unsigned char chksum = 0x00;
  252. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  253. if(len > 6)
  254. {
  255. if (len < 6+(rx[4] | rx[5]<<8))
  256. return result;
  257. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  258. {
  259. chksum ^= rx[6+idx];
  260. }
  261. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  262. (rx[2] == tx[1]) &&
  263. (rx[1] == tx[2]) &&
  264. (rx[3] == tx[3]))
  265. {
  266. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  267. Ret_Buf->voltage[idx] = rx[6+idx];
  268. result = PASS;
  269. }
  270. }
  271. return result;
  272. }
  273. unsigned char Query_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Ret_Buf)
  274. {
  275. unsigned char result = FAIL;
  276. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Relay_Output, 0x00, 0x00, 0x00};
  277. unsigned char rx[512];
  278. unsigned char chksum = 0x00;
  279. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  280. // for (int i = 0; i < 7; i++)
  281. // printf("tx = %x \n", tx[i]);
  282. // for (int i = 0; i < len; i++)
  283. // printf("rx = %x \n", rx[i]);
  284. if(len > 6)
  285. {
  286. if (len < 6+(rx[4] | rx[5]<<8))
  287. return result;
  288. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  289. {
  290. chksum ^= rx[6+idx];
  291. }
  292. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  293. (rx[2] == tx[1]) &&
  294. (rx[1] == tx[2]) &&
  295. (rx[3] == tx[3]))
  296. {
  297. Ret_Buf->relay_event.bits.AC_Contactor = (rx[6] >> 0) & 0x01;
  298. Ret_Buf->relay_event.bits.CCS_Precharge = (rx[6] >> 1) & 0x01;
  299. Ret_Buf->relay_event.bits.Gun1_N = (rx[7] >> 0) & 0x01;
  300. Ret_Buf->relay_event.bits.Gun1_P = (rx[7] >> 1) & 0x01;
  301. Ret_Buf->relay_event.bits.Gun1_Parallel_N = (rx[7] >> 2) & 0x01;
  302. Ret_Buf->relay_event.bits.Gun1_Parallel_P = (rx[7] >> 3) & 0x01;
  303. Ret_Buf->relay_event.bits.Gun2_N = (rx[8] >> 0) & 0x01;
  304. Ret_Buf->relay_event.bits.Gun2_P = (rx[8] >> 1) & 0x01;
  305. result = PASS;
  306. }
  307. }
  308. return result;
  309. }
  310. unsigned char Query_Gfd_Adc(unsigned char fd, unsigned char targetAddr, Gfd *Ret_Buf)
  311. {
  312. unsigned char result = FAIL;
  313. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Gfd_Adc, 0x00, 0x00, 0x00};
  314. unsigned char rx[512];
  315. unsigned char chksum = 0x00;
  316. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  317. // for(int i = 0; i < 7; i++)
  318. // printf ("tx = %d \n", tx[i]);
  319. if(len > 6)
  320. {
  321. if (len < 6+(rx[4] | rx[5]<<8))
  322. {
  323. //printf("Query_Gfd_Adc fail \n");
  324. return result;
  325. }
  326. // for(int i = 0; i < len; i++)
  327. // printf ("rx = %d \n", rx[i]);
  328. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  329. {
  330. chksum ^= rx[6+idx];
  331. }
  332. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  333. (rx[2] == tx[1]) &&
  334. (rx[1] == tx[2]) &&
  335. (rx[3] == tx[3]))
  336. {
  337. Ret_Buf->Resister_conn1 = (rx[6] | (rx[7] << 8));
  338. Ret_Buf->voltage_conn1 = (rx[8] | (rx[9] << 8));
  339. Ret_Buf->result_conn1 = rx[10];
  340. Ret_Buf->rb_step_1 = rx[11];
  341. Ret_Buf->Resister_conn2 = (rx[12] | (rx[13] << 8));
  342. Ret_Buf->voltage_conn2 = (rx[14] | (rx[15] << 8));
  343. Ret_Buf->result_conn2 = rx[16];
  344. Ret_Buf->rb_step_2 = rx[17];
  345. result = PASS;
  346. }
  347. }
  348. return result;
  349. }
  350. unsigned char Query_Gpio_Input(unsigned char fd, unsigned char targetAddr, Gpio_in *Ret_Buf)
  351. {
  352. unsigned char result = FAIL;
  353. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Gpio_In, 0x00, 0x00, 0x00};
  354. unsigned char rx[512];
  355. unsigned char chksum = 0x00;
  356. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  357. if(len > 6)
  358. {
  359. if (len < 6+(rx[4] | rx[5]<<8))
  360. return result;
  361. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  362. {
  363. chksum ^= rx[6+idx];
  364. }
  365. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  366. (rx[2] == tx[1]) &&
  367. (rx[1] == tx[2]) &&
  368. (rx[3] == tx[3]))
  369. {
  370. Ret_Buf->AC_Connector = (rx[6] >> 0) & 0x01;
  371. Ret_Buf->AC_MainBreaker = (rx[6] >> 1) & 0x01;
  372. Ret_Buf->SPD = (rx[6] >> 2) & 0x01;
  373. Ret_Buf->Door_Open = (rx[6] >> 3) & 0x01;
  374. Ret_Buf->GFD[0] = (rx[6] >> 4) & 0x01;
  375. Ret_Buf->GFD[1] = (rx[6] >> 5) & 0x01;
  376. Ret_Buf->AC_Drop = (rx[6] >> 6) & 0x01;
  377. Ret_Buf->Emergency_IO = (rx[7] >> 0) & 0x01;
  378. Ret_Buf->Button_Emergency_Press = (rx[8] >> 0) & 0x01;
  379. Ret_Buf->Button_On_Press = (rx[8] >> 1) & 0x01;
  380. Ret_Buf->Button_Off_Press = (rx[8] >> 2) & 0x01;
  381. Ret_Buf->Key_1_Press = (rx[8] >> 3) & 0x01;
  382. Ret_Buf->Key_2_Press = (rx[8] >> 4) & 0x01;
  383. Ret_Buf->Key_3_Press = (rx[8] >> 5) & 0x01;
  384. Ret_Buf->Key_4_Press = (rx[8] >> 6) & 0x01;
  385. result = PASS;
  386. }
  387. }
  388. return result;
  389. }
  390. unsigned char Query_Model_Name(unsigned char fd, unsigned char targetAddr, unsigned char *modelname)
  391. {
  392. unsigned char result = FAIL;
  393. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Model_Name, 0x00, 0x00, 0x00};
  394. unsigned char rx[512];
  395. unsigned char chksum = 0x00;
  396. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  397. if(len > 6)
  398. {
  399. if (len < 6+(rx[4] | rx[5]<<8))
  400. return result;
  401. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  402. {
  403. chksum ^= rx[6+idx];
  404. }
  405. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  406. (rx[2] == tx[1]) &&
  407. (rx[1] == tx[2]) &&
  408. (rx[3] == tx[3]))
  409. {
  410. strncpy((char *)modelname, (char *)(rx + 6), (rx[4] | rx[5]<<8));
  411. result = PASS;
  412. }
  413. }
  414. return result;
  415. }
  416. unsigned char Config_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Set_Buf)
  417. {
  418. unsigned char result = FAIL;
  419. unsigned char tx[15] = {0xaa, 0x00, targetAddr, Cmd.config_Fan_Speed, 0x08, 0x00, Set_Buf->speed[0]&0xff, (Set_Buf->speed[0]>>8)&0xff, Set_Buf->speed[1]&0xff, (Set_Buf->speed[1]>>8)&0xff, Set_Buf->speed[2]&0xff, (Set_Buf->speed[2]>>8)&0xff, Set_Buf->speed[3]&0xff, (Set_Buf->speed[3]>>8)&0xff, 0x00};
  420. unsigned char rx[512];
  421. unsigned char chksum = 0x00;
  422. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  423. chksum ^= tx[6+idx];
  424. tx[14] = chksum;
  425. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  426. if(len > 6)
  427. {
  428. if (len < 6+(rx[4] | rx[5]<<8))
  429. return result;
  430. chksum = 0x00;
  431. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  432. {
  433. chksum ^= rx[6+idx];
  434. }
  435. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  436. (rx[2] == tx[1]) &&
  437. (rx[1] == tx[2]) &&
  438. (rx[3] == tx[3]) &&
  439. rx[6] == PASS)
  440. {
  441. result = PASS;
  442. }
  443. }
  444. return result;
  445. }
  446. unsigned char Config_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Set_Buf)
  447. {
  448. unsigned char result = FAIL;
  449. unsigned char tx[10] = {0xaa, 0x00, targetAddr, Cmd.config_Relay_Output, 0x03, 0x00, Set_Buf->relay_event.relay_status[0], Set_Buf->relay_event.relay_status[1], Set_Buf->relay_event.relay_status[2]};
  450. unsigned char rx[512];
  451. unsigned char chksum = 0x00;
  452. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  453. chksum ^= tx[6 + idx];
  454. tx[9] = chksum;
  455. // for (int i = 0; i < 10; i++)
  456. // printf("set relay cmd : tx = %x \n", tx[i]);
  457. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  458. if(len > 6)
  459. {
  460. if (len < 6+(rx[4] | rx[5]<<8))
  461. return result;
  462. // for (int i = 0; i < len; i++)
  463. // printf("set relay cmd : rx = %x \n", rx[i]);
  464. chksum = 0x00;
  465. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  466. {
  467. chksum ^= rx[6+idx];
  468. }
  469. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  470. (rx[2] == tx[1]) &&
  471. (rx[1] == tx[2]) &&
  472. (rx[3] == tx[3]) &&
  473. rx[6] == PASS)
  474. {
  475. result = PASS;
  476. }
  477. }
  478. return result;
  479. }
  480. unsigned char Config_Gpio_Output(unsigned char fd, unsigned char targetAddr, Gpio_out *Set_Buf)
  481. {
  482. unsigned char result = FAIL;
  483. unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Gpio_Output, 0x01, 0x00, 0x00, 0x00};
  484. unsigned char rx[512];
  485. unsigned char chksum = 0x00;
  486. tx[6] |= (Set_Buf->AC_Connector?0x01:0x00);
  487. for(int idx = 0;idx<2;idx++)
  488. tx[6] |= (Set_Buf->Button_LED[idx]?0x01:0x00)<<(1+idx);
  489. for(int idx = 0;idx<4;idx++)
  490. tx[6] |= (Set_Buf->System_LED[idx]?0x01:0x00)<<(3+idx);
  491. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  492. chksum ^= tx[6+idx];
  493. tx[14] = chksum;
  494. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  495. if(len > 6)
  496. {
  497. if (len < 6+(rx[4] | rx[5]<<8))
  498. return result;
  499. chksum = 0x00;
  500. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  501. {
  502. chksum ^= rx[6+idx];
  503. }
  504. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  505. (rx[2] == tx[1]) &&
  506. (rx[1] == tx[2]) &&
  507. (rx[3] == tx[3]))
  508. {
  509. result = PASS;
  510. }
  511. }
  512. return result;
  513. }
  514. unsigned char Config_Gfd_Value(unsigned char fd, unsigned char targetAddr, Gfd_config *Set_Buf)
  515. {
  516. unsigned char result = FAIL;
  517. unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Gfd_Value, 0x02, 0x00, 0x00, 0x00, 0x00};
  518. unsigned char rx[512];
  519. unsigned char chksum = 0x00;
  520. tx[6] = Set_Buf->index;
  521. tx[7] = Set_Buf->state;
  522. for(int idx = 0; idx<(tx[4] | tx[5]<<8);idx++)
  523. chksum ^= tx[6+idx];
  524. tx[8] = chksum;
  525. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  526. if(len > 6)
  527. {
  528. if (len < 6+(rx[4] | rx[5]<<8))
  529. return result;
  530. chksum = 0x00;
  531. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  532. {
  533. chksum ^= rx[6+idx];
  534. }
  535. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  536. (rx[2] == tx[1]) &&
  537. (rx[1] == tx[2]) &&
  538. (rx[3] == tx[3]))
  539. {
  540. result = PASS;
  541. }
  542. }
  543. return result;
  544. }
  545. unsigned char Config_Model_Name(unsigned char fd, unsigned char targetAddr, unsigned char *modelname)
  546. {
  547. unsigned char result = FAIL;
  548. unsigned char tx[21] = {0xaa, 0x00, targetAddr, Cmd.config_Model_Name, 0x0E, 0x00,
  549. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  550. unsigned char rx[512];
  551. unsigned char chksum = 0x00;
  552. memcpy(tx + 6, modelname, 14);
  553. for(int idx = 0; idx<(tx[4] | tx[5]<<8);idx++)
  554. chksum ^= tx[6+idx];
  555. tx[20] = chksum;
  556. // for(int i = 0; i < 21; i++)
  557. // printf ("tx = %x \n", tx[i]);
  558. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  559. // for(int i = 0; i < len; i++)
  560. // printf ("rx = %x \n", rx[i]);
  561. if(len > 6)
  562. {
  563. if (len < 6+(rx[4] | rx[5]<<8))
  564. return result;
  565. chksum = 0x00;
  566. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  567. {
  568. chksum ^= rx[6+idx];
  569. }
  570. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  571. (rx[2] == tx[1]) &&
  572. (rx[1] == tx[2]) &&
  573. (rx[3] == tx[3]) &&
  574. rx[6] == PASS)
  575. {
  576. result = PASS;
  577. }
  578. }
  579. return result;
  580. }
  581. unsigned char Config_Rtc_Data(unsigned char fd, unsigned char targetAddr, Rtc *Set_Buf)
  582. {
  583. unsigned char result = FAIL;
  584. unsigned char tx[21] = { 0xaa, 0x00, targetAddr, Cmd.config_Rtc_Data, 0x0E, 0x00, Set_Buf->RtcData[0], Set_Buf->RtcData[1],
  585. Set_Buf->RtcData[2], Set_Buf->RtcData[3], Set_Buf->RtcData[4], Set_Buf->RtcData[5], Set_Buf->RtcData[6], Set_Buf->RtcData[7],
  586. Set_Buf->RtcData[8], Set_Buf->RtcData[9], Set_Buf->RtcData[10], Set_Buf->RtcData[11], Set_Buf->RtcData[12], Set_Buf->RtcData[13]};
  587. unsigned char rx[512];
  588. unsigned char chksum = 0x00;
  589. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  590. chksum ^= tx[6 + idx];
  591. tx[20] = chksum;
  592. if (tranceive(fd, tx, sizeof(tx), rx) > 0)
  593. {
  594. chksum = 0x00;
  595. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
  596. {
  597. chksum ^= rx[6 + idx];
  598. }
  599. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  600. (rx[2] == tx[1]) &&
  601. (rx[1] == tx[2]) &&
  602. (rx[3] == tx[3]) &&
  603. rx[6] == PASS)
  604. {
  605. result = PASS;
  606. }
  607. }
  608. return result;
  609. }
  610. unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
  611. {
  612. unsigned char result = FAIL;
  613. unsigned char tx[11] = {0xaa, 0x00, targetAddr, Cmd.update_Start, 0x04, 0x00, (crc32>>0)&0xff, (crc32>>8)&0xff, (crc32>>16)&0xff, (crc32>>24)&0xff, 0x00};
  614. unsigned char rx[512];
  615. unsigned char chksum = 0x00;
  616. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  617. chksum ^= tx[6+idx];
  618. tx[10] = chksum;
  619. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  620. if(len > 6)
  621. {
  622. if (len < 6+(rx[4] | rx[5]<<8))
  623. return result;
  624. chksum = 0x00;
  625. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  626. {
  627. chksum ^= rx[6+idx];
  628. }
  629. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  630. (rx[2] == tx[1]) &&
  631. (rx[1] == tx[2]) &&
  632. (rx[3] == tx[3]) &&
  633. (rx[6] == 0x00))
  634. {
  635. result = PASS;
  636. }
  637. }
  638. return result;
  639. }
  640. unsigned char Update_Abord(unsigned char fd, unsigned char targetAddr)
  641. {
  642. unsigned char result = FAIL;
  643. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Start, 0x04, 0x00, 0x00};
  644. unsigned char rx[512];
  645. unsigned char chksum = 0x00;
  646. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  647. if(len > 6)
  648. {
  649. if (len < 6+(rx[4] | rx[5]<<8))
  650. return result;
  651. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  652. {
  653. chksum ^= rx[6+idx];
  654. }
  655. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  656. (rx[2] == tx[1]) &&
  657. (rx[1] == tx[2]) &&
  658. (rx[3] == tx[3]) &&
  659. (rx[6] == 0x00))
  660. {
  661. result = PASS;
  662. }
  663. }
  664. return result;
  665. }
  666. unsigned char Update_Transfer(unsigned char fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
  667. {
  668. unsigned char result = FAIL;
  669. unsigned char tx[11 + length];
  670. unsigned char rx[512];
  671. unsigned char chksum = 0x00;
  672. tx[0] = 0xaa;
  673. tx[1] = 0x00;
  674. tx[2] = targetAddr;
  675. tx[3] = Cmd.update_Transfer;
  676. tx[4] = (4 + length) & 0xff;
  677. tx[5] = ((4 + length)>>8) & 0xff;
  678. tx[6] = (startAddr>>0) & 0xff;
  679. tx[7] = (startAddr>>8) & 0xff;
  680. tx[8] = (startAddr>>16) & 0xff;
  681. tx[9] = (startAddr>>24) & 0xff;
  682. memcpy(tx+10, data, length);
  683. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  684. chksum ^= tx[6+idx];
  685. tx[sizeof(tx)-1] = chksum;
  686. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  687. if(len > 6)
  688. {
  689. if (len < 6+(rx[4] | rx[5]<<8))
  690. return result;
  691. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  692. {
  693. chksum ^= rx[6+idx];
  694. }
  695. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  696. (rx[2] == tx[1]) &&
  697. (rx[1] == tx[2]) &&
  698. (rx[3] == tx[3]) &&
  699. (rx[6] == 0x00))
  700. {
  701. result = PASS;
  702. }
  703. }
  704. return result;
  705. }
  706. unsigned char Update_Finish(unsigned char fd, unsigned char targetAddr)
  707. {
  708. unsigned char result = FAIL;
  709. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Finish, 0x04, 0x00, 0x00};
  710. unsigned char rx[512];
  711. unsigned char chksum = 0x00;
  712. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  713. if(len > 6)
  714. {
  715. if (len < 6+(rx[4] | rx[5]<<8))
  716. return result;
  717. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  718. {
  719. chksum ^= rx[6+idx];
  720. }
  721. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  722. (rx[2] == tx[1]) &&
  723. (rx[1] == tx[2]) &&
  724. (rx[3] == tx[3]) &&
  725. (rx[6] == 0x00))
  726. {
  727. result = PASS;
  728. }
  729. }
  730. return result;
  731. }
  732. unsigned char Query_AC_Status(unsigned char fd, unsigned char targetAddr, Ac_Status *Ret_Buf)
  733. {
  734. unsigned char result = FAIL;
  735. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_ac_status, 0x00, 0x00, 0x00};
  736. unsigned char rx[512];
  737. unsigned char chksum = 0x00;
  738. unsigned char len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);
  739. if(len > 6)
  740. {
  741. if (len < 6+(rx[4] | rx[5]<<8))
  742. return result;
  743. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  744. {
  745. chksum ^= rx[6+idx];
  746. }
  747. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  748. (rx[2] == tx[1]) &&
  749. (rx[1] == tx[2]) &&
  750. (rx[3] == tx[3]))
  751. {
  752. Ret_Buf->CpStatus = rx[6];
  753. Ret_Buf->CurLimit = (rx[7] | (rx[8] << 8));
  754. Ret_Buf->PilotVol_P = (rx[9] | (rx[10] << 8));
  755. Ret_Buf->PilotVol_N = (rx[11] | (rx[12] << 8));
  756. Ret_Buf->LockStatus = rx[13];
  757. Ret_Buf->RelayStatus = rx[14];
  758. Ret_Buf->ShutterStatus = rx[15];
  759. Ret_Buf->MeterStatus = rx[16];
  760. Ret_Buf->PpStatus = rx[17];
  761. Ret_Buf->MaxCurrent = rx[18];
  762. Ret_Buf->RotateSwitchStatus = rx[19];
  763. //
  764. // Ret_Buf->AC_Connector = (rx[6] >> 0) & 0x01;
  765. // Ret_Buf->AC_MainBreaker = (rx[6] >> 1) & 0x01;
  766. // Ret_Buf->SPD = (rx[6] >> 2) & 0x01;
  767. // Ret_Buf->Door_Open = (rx[6] >> 3) & 0x01;
  768. // Ret_Buf->GFD[0] = (rx[6] >> 4) & 0x01;
  769. // Ret_Buf->GFD[1] = (rx[6] >> 5) & 0x01;
  770. // Ret_Buf->AC_Drop = (rx[6] >> 6) & 0x01;
  771. //
  772. // Ret_Buf->Emergency_IO = (rx[7] >> 0) & 0x01;
  773. //
  774. // Ret_Buf->Button_Emergency_Press = (rx[8] >> 0) & 0x01;
  775. // Ret_Buf->Button_On_Press = (rx[8] >> 1) & 0x01;
  776. // Ret_Buf->Button_Off_Press = (rx[8] >> 2) & 0x01;
  777. // Ret_Buf->Key_1_Press = (rx[8] >> 3) & 0x01;
  778. // Ret_Buf->Key_2_Press = (rx[8] >> 4) & 0x01;
  779. // Ret_Buf->Key_3_Press = (rx[8] >> 5) & 0x01;
  780. // Ret_Buf->Key_4_Press = (rx[8] >> 6) & 0x01;
  781. result = PASS;
  782. }
  783. }
  784. return result;
  785. }
  786. unsigned char Query_AC_Alarm_Code(unsigned char fd, unsigned char targetAddr, Ac_Alarm_code *Ret_Buf)
  787. {
  788. unsigned char result = FAIL;
  789. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_ac_alarm_code, 0x00, 0x00};
  790. unsigned char rx[512];
  791. unsigned char chksum = 0x00;
  792. unsigned char len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);
  793. if(len > 6)
  794. {
  795. if (len < 6+(rx[4] | rx[5]<<8))
  796. return result;
  797. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  798. {
  799. chksum ^= rx[6+idx];
  800. }
  801. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  802. (rx[2] == tx[1]) &&
  803. (rx[1] == tx[2]) &&
  804. (rx[3] == tx[3]))
  805. {
  806. Ret_Buf->AcAlarmCode = rx[6] + (rx[7] << 8) + (rx[8] << 16) + (rx[9] << 24);
  807. result = PASS;
  808. }
  809. }
  810. return result;
  811. }
  812. unsigned char Query_Charging_Energy(unsigned char fd, unsigned char targetAddr, Ac_Charging_energy *Ret_Buf)
  813. {
  814. unsigned char result = FAIL;
  815. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_ac_output_energy, 0x00, 0x00,0x00};
  816. unsigned char rx[512];
  817. unsigned char chksum = 0x00;
  818. unsigned char len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);
  819. if(len > 6)
  820. {
  821. if (len < 6+(rx[4] | rx[5]<<8))
  822. return result;
  823. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  824. {
  825. chksum ^= rx[6+idx];
  826. }
  827. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  828. (rx[2] == tx[1]) &&
  829. (rx[1] == tx[2]) &&
  830. (rx[3] == tx[3]))
  831. {
  832. Ret_Buf->Energy = rx[6] + (rx[7] << 8) + (rx[8] << 16) + (rx[9] << 24);
  833. result = PASS;
  834. }
  835. }
  836. return result;
  837. }
  838. unsigned char Query_Charging_Current(unsigned char fd, unsigned char targetAddr, Ac_Charging_current *Ret_Buf)
  839. {
  840. unsigned char result = FAIL;
  841. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_ac_output_current, 0x00, 0x00, 0x00};
  842. unsigned char rx[512];
  843. unsigned char chksum = 0x00;
  844. unsigned char len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);
  845. if(len > 6)
  846. {
  847. if (len < 6+(rx[4] | rx[5]<<8))
  848. return result;
  849. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  850. {
  851. chksum ^= rx[6+idx];
  852. }
  853. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  854. (rx[2] == tx[1]) &&
  855. (rx[1] == tx[2]) &&
  856. (rx[3] == tx[3]))
  857. {
  858. Ret_Buf->OuputCurrentL1 = rx[6] + (rx[7] << 8);
  859. Ret_Buf->OuputCurrentL2 = rx[8] + (rx[9] << 8);
  860. Ret_Buf->OuputCurrentL3 = rx[10] + (rx[11] << 8);
  861. result = PASS;
  862. }
  863. }
  864. return result;
  865. }
  866. unsigned char Config_LED_Status(unsigned char fd, unsigned char targetAddr, Ac_Led_Status *Ret_Buf)
  867. {
  868. unsigned char result = FAIL;
  869. unsigned char tx[12] = {0xaa, 0x00, targetAddr, Cmd.config_ac_led_status, 0x05, 0x00, Ret_Buf->ActionMode, (Ret_Buf->AcAlarmCode >> 0) & 0xFF,
  870. (Ret_Buf->AcAlarmCode >> 8) & 0xFF, (Ret_Buf->AcAlarmCode >> 16) & 0xFF, (Ret_Buf->AcAlarmCode >> 24) & 0xFF};
  871. unsigned char rx[512];
  872. unsigned char chksum = 0x00;
  873. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  874. chksum ^= tx[6 + idx];
  875. tx[11] = chksum;
  876. if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0)
  877. {
  878. chksum = 0x00;
  879. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
  880. {
  881. chksum ^= rx[6 + idx];
  882. }
  883. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  884. (rx[2] == tx[1]) &&
  885. (rx[1] == tx[2]) &&
  886. (rx[3] == tx[3]) &&
  887. rx[6] == PASS)
  888. {
  889. result = PASS;
  890. }
  891. }
  892. return result;
  893. }
  894. unsigned char Config_Legacy_Req(unsigned char fd, unsigned char targetAddr, unsigned char _switch)
  895. {
  896. unsigned char result = FAIL;
  897. unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Legacy_Req, 0x02, 0x00, _switch, 0x00};
  898. unsigned char rx[512];
  899. unsigned char chksum = 0x00;
  900. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  901. chksum ^= tx[6 + idx];
  902. tx[8] = chksum;
  903. if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0)
  904. {
  905. chksum = 0x00;
  906. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
  907. {
  908. chksum ^= rx[6 + idx];
  909. }
  910. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  911. (rx[2] == tx[1]) &&
  912. (rx[1] == tx[2]) &&
  913. (rx[3] == tx[3]) &&
  914. rx[6] == PASS)
  915. {
  916. result = PASS;
  917. }
  918. }
  919. return result;
  920. }
  921. unsigned char Config_Ac_Duty(unsigned char fd, unsigned char targetAddr, unsigned char _value)
  922. {
  923. unsigned char result = FAIL;
  924. unsigned char tx[8] = {0xaa, 0x00, targetAddr, Cmd.config_ac_duty, 0x01, 0x00, _value};
  925. unsigned char rx[512];
  926. unsigned char chksum = 0x00;
  927. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  928. chksum ^= tx[6 + idx];
  929. tx[7] = chksum;
  930. if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0)
  931. {
  932. chksum = 0x00;
  933. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
  934. {
  935. chksum ^= rx[6 + idx];
  936. }
  937. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  938. (rx[2] == tx[1]) &&
  939. (rx[1] == tx[2]) &&
  940. (rx[3] == tx[3]) &&
  941. rx[6] == PASS)
  942. {
  943. result = PASS;
  944. }
  945. }
  946. return result;
  947. }
  948. unsigned char Config_CSU_Mode(unsigned char fd, unsigned char targetAddr)
  949. {
  950. unsigned char result = FAIL;
  951. unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_csu_mode, 0x02, 0x00, 0x01, 0x00};
  952. unsigned char rx[512];
  953. unsigned char chksum = 0x00;
  954. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  955. chksum ^= tx[6 + idx];
  956. tx[7] = chksum;
  957. if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0)
  958. {
  959. chksum = 0x00;
  960. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
  961. {
  962. chksum ^= rx[6 + idx];
  963. }
  964. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  965. (rx[2] == tx[1]) &&
  966. (rx[1] == tx[2]) &&
  967. (rx[3] == tx[3]) &&
  968. rx[6] == PASS)
  969. {
  970. result = PASS;
  971. }
  972. }
  973. return result;
  974. }
  975. unsigned char Config_Reset_MCU(unsigned char fd, unsigned char targetAddr)
  976. {
  977. unsigned char result = FAIL;
  978. unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_reset_mcu, 0x02, 0x00, 0x01, 0x00};
  979. unsigned char rx[512];
  980. unsigned char chksum = 0x00;
  981. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  982. chksum ^= tx[6 + idx];
  983. tx[7] = chksum;
  984. if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0)
  985. {
  986. chksum = 0x00;
  987. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
  988. {
  989. chksum ^= rx[6 + idx];
  990. }
  991. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  992. (rx[2] == tx[1]) &&
  993. (rx[1] == tx[2]) &&
  994. (rx[3] == tx[3]) &&
  995. rx[6] == PASS)
  996. {
  997. result = PASS;
  998. }
  999. }
  1000. return result;
  1001. }
  1002. unsigned char Config_Led_Color(unsigned char fd, unsigned char targetAddr, Led_Color *Ret_Buf)
  1003. {
  1004. unsigned char result = FAIL;
  1005. unsigned char tx[13] = {0xaa, 0x00, targetAddr, Cmd.config_led_color, 0x06, 0x00,
  1006. Ret_Buf->Connect_1_Red, Ret_Buf->Connect_1_Green, Ret_Buf->Connect_1_Blue,
  1007. Ret_Buf->Connect_2_Red, Ret_Buf->Connect_2_Green, Ret_Buf->Connect_2_Blue};
  1008. unsigned char rx[512];
  1009. unsigned char chksum = 0x00;
  1010. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  1011. chksum ^= tx[6 + idx];
  1012. tx[13] = chksum;
  1013. if (tranceive(fd, tx, sizeof(tx), rx) > 0)
  1014. {
  1015. chksum = 0x00;
  1016. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
  1017. {
  1018. chksum ^= rx[6 + idx];
  1019. }
  1020. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1021. (rx[2] == tx[1]) &&
  1022. (rx[1] == tx[2]) &&
  1023. (rx[3] == tx[3]) &&
  1024. rx[6] == PASS)
  1025. {
  1026. result = PASS;
  1027. }
  1028. }
  1029. return result;
  1030. }