internalComm.c 30 KB

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