internalComm.c 30 KB

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