internalComm.c 27 KB

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