internalComm.c 36 KB

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