internalComm.c 35 KB

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