internalComm.c 33 KB

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