internalComm.c 30 KB

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