internalComm.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  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. #define RS485_RAW_DATA 0
  39. #define RS485_INFO(format, args...) StoreInternalCommLog("[%s:%4d][%s][RS485] "format, (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__), __LINE__, __FUNCTION__, ##args)
  40. struct Address Addr = {
  41. AUX_ADD,
  42. FAN_ADD,
  43. RELAY_ADD,
  44. ACPLUG_ADD,
  45. LED_ADD,
  46. RC1_ADD,
  47. RC2_ADD,
  48. RD_ADD,
  49. BROADCAST_ADD
  50. };
  51. struct Command Cmd = {
  52. MSGID_G_FW_VERSION,
  53. MSGID_G_HW_VERSION,
  54. MSGID_G_AC_INPUT_VOLTAGE,
  55. MSGID_G_DC_OUTPUT_VOLTAGE,
  56. MSGID_G_FAN_SPEED,
  57. MSGID_G_TEMPERATURE,
  58. MSGID_G_AUX_VOLTAGE,
  59. MSGID_G_RELAY_STATE,
  60. MSGID_G_GFD,
  61. MSGID_G_GPIO_INPUT,
  62. MSGID_G_MODEL_NAME,
  63. MSGID_G_AC_OUTPUT_CURRENT,
  64. MSGID_G_AC_STATUS,
  65. MSGID_G_AC_ALARM,
  66. MSGID_G_AC_OUTPUT_POWER,
  67. MSGID_G_DC_INPUT_VOLTAGE,
  68. MSGID_S_FAN_SPEED,
  69. MSGID_S_MODEL_NAME,
  70. MSGID_S_RELAY_STATE,
  71. MSGID_S_GPIO_OUTPUT,
  72. MSGID_S_RTC,
  73. MSGID_S_AC_LED_STATE,
  74. MSGID_S_AC_CP_DUTY,
  75. MSGID_S_AC_LEGACY_REQ,
  76. MSGID_S_GFD,
  77. MSGID_S_AC_RESET_MCU,
  78. MSGID_S_MCU_OPERATION,
  79. MSGID_S_LED_COLOR,
  80. MSGID_S_FW_CHECKSUM,
  81. MSGID_S_FW_ABORT,
  82. MSGID_S_FW_PACKET,
  83. MSGID_S_FW_FINISH
  84. };
  85. int StoreInternalCommLog(const char *fmt, ...)
  86. {
  87. char Buf[4096+256];
  88. char buffer[4096];
  89. va_list args;
  90. struct timeb SeqEndTime;
  91. struct tm *tm;
  92. va_start(args, fmt);
  93. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  94. va_end(args);
  95. memset(Buf,0,sizeof(Buf));
  96. ftime(&SeqEndTime);
  97. SeqEndTime.time = time(NULL);
  98. tm=localtime(&SeqEndTime.time);
  99. sprintf(Buf,"echo \'%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\' >> /Storage/SystemLog/[%04d.%02d]InternalCommLog",
  100. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  101. buffer,
  102. tm->tm_year+1900,tm->tm_mon+1);
  103. system(Buf);
  104. return rc;
  105. }
  106. int tranceiveRelDelayTime(int fd, unsigned char* cmd, unsigned char cmd_len, unsigned char* rx, unsigned short _delay)
  107. {
  108. int len;
  109. //sleep(2); //required to make flush work, for some reason
  110. tcflush(fd,TCIOFLUSH);
  111. if(write(fd, cmd, cmd_len) >= cmd_len)
  112. {
  113. #if RS485_RAW_DATA
  114. char buffer[1024];
  115. char temp[32];
  116. memset(buffer, 0x00, sizeof(buffer));
  117. sprintf(buffer, "Tx->");
  118. for(int i = 0; i < cmd_len; i++)
  119. {
  120. sprintf(temp, " %02X", cmd[i]);
  121. strcat(buffer, temp);
  122. }
  123. RS485_INFO("%s", buffer);
  124. #endif
  125. usleep(_delay * 1000);
  126. len = read(fd, rx, 512);
  127. #if RS485_RAW_DATA
  128. memset(buffer, 0x00, sizeof(buffer));
  129. sprintf(buffer, "Rx<-");
  130. if(len > 0)
  131. {
  132. for(int i = 0; i < len; i++)
  133. {
  134. sprintf(temp, " %02X", rx[i]);
  135. strcat(buffer, temp);
  136. }
  137. }
  138. else
  139. {
  140. strcat(buffer, " No Data!");
  141. }
  142. RS485_INFO("%s", buffer);
  143. #endif
  144. }
  145. else
  146. {
  147. #ifdef SystemLogMessage
  148. DEBUG_ERROR("Serial command %s response fail.\n", cmd);
  149. #endif
  150. }
  151. return len;
  152. }
  153. int tranceive(int fd, unsigned char* cmd, unsigned char cmd_len, unsigned char* rx)
  154. {
  155. int len;
  156. //sleep(2); //required to make flush work, for some reason
  157. tcflush(fd,TCIOFLUSH);
  158. if(write(fd, cmd, cmd_len) >= cmd_len)
  159. {
  160. #if RS485_RAW_DATA
  161. char buffer[1024];
  162. char temp[32];
  163. memset(buffer, 0x00, sizeof(buffer));
  164. sprintf(buffer, "Tx->");
  165. for(int i = 0; i < cmd_len; i++)
  166. {
  167. sprintf(temp, " %02X", cmd[i]);
  168. strcat(buffer, temp);
  169. }
  170. RS485_INFO("%s", buffer);
  171. #endif
  172. usleep(15000);
  173. len = read(fd, rx, 512);
  174. #if RS485_RAW_DATA
  175. memset(buffer, 0x00, sizeof(buffer));
  176. sprintf(buffer, "Rx<-");
  177. if(len > 0)
  178. {
  179. for(int i = 0; i < len; i++)
  180. {
  181. sprintf(temp, " %02X", rx[i]);
  182. strcat(buffer, temp);
  183. }
  184. }
  185. else
  186. {
  187. strcat(buffer, " No Data!");
  188. }
  189. RS485_INFO("%s", buffer);
  190. #endif
  191. }
  192. else
  193. {
  194. #ifdef SystemLogMessage
  195. DEBUG_ERROR("Serial command %s response fail.\n", cmd);
  196. #endif
  197. }
  198. return len;
  199. }
  200. int Internal_Comm_Request(unsigned char fd, unsigned char targetAddr, unsigned char msg, unsigned char *data, unsigned short dataLen, unsigned char *response)
  201. {
  202. int resLen = 0, rxLen = 0;
  203. unsigned char tx[512], rx[512];
  204. unsigned char txChksum = 0x00, rxChksum = 0;
  205. tx[0] = FRAME_ID;
  206. tx[1] = CSU_ADD;
  207. tx[2] = targetAddr;
  208. tx[3] = msg;
  209. tx[4] = dataLen & 0xFF;
  210. tx[5] = (dataLen >> 8) & 0xFF;
  211. if(dataLen > 0)
  212. {
  213. memcpy(&tx[6], &data[0], dataLen);
  214. for(int i = 0; i < dataLen; i++)
  215. {
  216. txChksum ^= tx[6 + i];
  217. }
  218. }
  219. tx[6 + dataLen] = txChksum;
  220. rxLen = tranceive(fd, &tx[0], INTERNAL_PACKET_MIN_LEN + dataLen, &rx[0]);
  221. if(rxLen >= INTERNAL_PACKET_MIN_LEN)
  222. {
  223. resLen = (rx[4] | (rx[5] << 8));
  224. if(rxLen == (INTERNAL_PACKET_MIN_LEN + resLen))
  225. {
  226. for(int i = 0; i < resLen; i++)
  227. {
  228. rxChksum ^= rx[6 + i];
  229. }
  230. if(rxChksum == rx[INTERNAL_PACKET_MIN_LEN - 1 + resLen])
  231. {
  232. if(rx[2] == tx[1] && rx[1] == tx[2] && rx[3] == tx[3])
  233. {
  234. if(resLen > 0)
  235. {
  236. memcpy(response, &rx[6], resLen);
  237. }
  238. #if 0
  239. printf("\r\n");
  240. for(int i = 0; i < rxLen; i++)
  241. {
  242. printf(" %02X", rx[i]);
  243. }
  244. #endif
  245. }
  246. else
  247. {
  248. // response packet(address or message) is not match
  249. resLen = -1;
  250. }
  251. }
  252. else
  253. {
  254. // checksum is not match
  255. resLen = -1;
  256. }
  257. }
  258. else
  259. {
  260. // rx packet length and data length is not match
  261. resLen = -1;
  262. }
  263. }
  264. else
  265. {
  266. // rx packet length is less than INTERNAL_PACKET_MIN_LEN
  267. resLen = -1;
  268. }
  269. return resLen;
  270. }
  271. unsigned char Query_FW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
  272. {
  273. unsigned char result = FAIL;
  274. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_FW_Ver, 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. {
  284. if (len < 6+(rx[4] | rx[5]<<8))
  285. return result;
  286. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  287. {
  288. chksum ^= rx[6+idx];
  289. }
  290. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  291. (rx[2] == tx[1]) &&
  292. (rx[1] == tx[2]) &&
  293. (rx[3] == tx[3]))
  294. {
  295. memcpy(Ret_Buf->Version_FW, (char *)rx+6, (rx[4] | rx[5]<<8));
  296. *(Ret_Buf->Version_FW + 8) = 0x00;
  297. result = PASS;
  298. }
  299. }
  300. return result;
  301. }
  302. unsigned char Query_HW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
  303. {
  304. unsigned char result = FAIL;
  305. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_HW_Ver, 0x00, 0x00, 0x00};
  306. unsigned char rx[512];
  307. unsigned char chksum = 0x00;
  308. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  309. if(len > 6)
  310. {
  311. if (len < 6+(rx[4] | rx[5]<<8))
  312. return result;
  313. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  314. {
  315. chksum ^= rx[6+idx];
  316. }
  317. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  318. (rx[2] == tx[1]) &&
  319. (rx[1] == tx[2]) &&
  320. (rx[3] == tx[3]))
  321. {
  322. memcpy(Ret_Buf->Version_HW, (char *)rx+6, (rx[4] | rx[5]<<8));
  323. *(Ret_Buf->Version_HW + 8) = 0x00;
  324. result = PASS;
  325. }
  326. }
  327. return result;
  328. }
  329. unsigned char Query_Present_InputVoltage(unsigned char fd, unsigned char targetAddr, PresentInputVoltage *Ret_Buf)
  330. {
  331. unsigned char result = FAIL;
  332. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Present_InputVoltage, 0x00, 0x00, 0x00};
  333. unsigned char rx[512];
  334. unsigned char chksum = 0x00;
  335. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  336. if(len > 13)
  337. {
  338. if (len < 6+(rx[4] | rx[5]<<8))
  339. return result;
  340. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  341. {
  342. chksum ^= rx[6+idx];
  343. }
  344. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  345. (rx[2] == tx[1]) &&
  346. (rx[1] == tx[2]) &&
  347. (rx[3] == tx[3]) &&
  348. chksum != 0)
  349. {
  350. Ret_Buf->inputType = rx[6];
  351. Ret_Buf->L1N_L12 =(rx[7] | (rx[8]<<8))/10.0;
  352. Ret_Buf->L2N_L23 =(rx[9] | (rx[10]<<8))/10.0;
  353. Ret_Buf->L3N_L31 =(rx[11] | (rx[12]<<8))/10.0;
  354. if (Ret_Buf->L1N_L12 >= 320 ||
  355. Ret_Buf->L2N_L23 >= 320 ||
  356. Ret_Buf->L3N_L31 >= 320)
  357. {
  358. result = FAIL;
  359. }
  360. else
  361. result = PASS;
  362. }
  363. }
  364. return result;
  365. }
  366. unsigned char Query_DC_InputVoltage(unsigned char fd, unsigned char targetAddr, DCInputVoltage *Ret_Buf)
  367. {
  368. unsigned char result = FAIL;
  369. unsigned char resLen = 0;
  370. unsigned char response[512];
  371. memset(response, 0x00, 512);
  372. resLen = Internal_Comm_Request(fd, targetAddr, Cmd.query_DC_InputVoltage, 0, 0, &response[0]);
  373. if(resLen >= 0)
  374. {
  375. Ret_Buf->DC_Input_1 =(response[0] | (response[1] << 8)) / 10;
  376. Ret_Buf->DC_Input_2 =(response[2] | (response[3] << 8)) / 10;
  377. result = PASS;
  378. }
  379. return result;
  380. }
  381. unsigned char Query_Present_OutputVoltage(unsigned char fd, unsigned char targetAddr, PresentOutputVoltage *Ret_Buf)
  382. {
  383. unsigned char result = FAIL;
  384. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Present_OutputVoltage, 0x00, 0x00, 0x00};
  385. unsigned char rx[512];
  386. unsigned char chksum = 0x00;
  387. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  388. if(len > 6)
  389. {
  390. if (len < 6+(rx[4] | rx[5]<<8))
  391. return result;
  392. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  393. {
  394. chksum ^= rx[6+idx];
  395. }
  396. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  397. (rx[2] == tx[1]) &&
  398. (rx[1] == tx[2]) &&
  399. (rx[3] == tx[3]))
  400. {
  401. Ret_Buf->behindFuse_Voltage_C1 =(rx[6] | (rx[7]<<8));
  402. Ret_Buf->behindRelay_Voltage_C1 =(rx[8] | (rx[9]<<8));
  403. if((rx[4] | rx[5]<<8) > 4)
  404. {
  405. Ret_Buf->behindFuse_Voltage_C2 =(rx[10] | (rx[11]<<8));
  406. Ret_Buf->behindRelay_Voltage_C2 =(rx[12] | (rx[13]<<8));
  407. }
  408. result = PASS;
  409. }
  410. }
  411. return result;
  412. }
  413. unsigned char Query_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Ret_Buf)
  414. {
  415. unsigned char result = FAIL;
  416. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Fan_Speed, 0x00, 0x00, 0x00};
  417. unsigned char rx[512];
  418. unsigned char chksum = 0x00;
  419. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  420. if(len > 6)
  421. {
  422. if (len < 6+(rx[4] | rx[5]<<8))
  423. return result;
  424. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  425. {
  426. chksum ^= rx[6+idx];
  427. }
  428. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  429. (rx[2] == tx[1]) &&
  430. (rx[1] == tx[2]) &&
  431. (rx[3] == tx[3]))
  432. {
  433. for(int idx=0;idx < 4;idx++)
  434. Ret_Buf->speed[idx] = (rx[6+(2*idx)] | (rx[6+(2*idx)+1]<<8));
  435. result = PASS;
  436. }
  437. }
  438. return result;
  439. }
  440. unsigned char Query_Temperature(unsigned char fd, unsigned char targetAddr, Temperature *Ret_Buf)
  441. {
  442. unsigned char result = FAIL;
  443. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Temperature, 0x00, 0x00, 0x00};
  444. unsigned char rx[512];
  445. unsigned char chksum = 0x00;
  446. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  447. if(len > 6)
  448. {
  449. if (len < 6+(rx[4] | rx[5]<<8))
  450. return result;
  451. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  452. {
  453. chksum ^= rx[6+idx];
  454. }
  455. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  456. (rx[2] == tx[1]) &&
  457. (rx[1] == tx[2]) &&
  458. (rx[3] == tx[3]))
  459. {
  460. for(int idx=0;idx < 4;idx++)
  461. Ret_Buf->temperature[idx] = rx[6+idx] - 60;
  462. result = PASS;
  463. }
  464. }
  465. return result;
  466. }
  467. unsigned char Query_Aux_PowerVoltage(unsigned char fd, unsigned char targetAddr, AuxPower *Ret_Buf)
  468. {
  469. unsigned char result = FAIL;
  470. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Aux_PowerVoltage, 0x00, 0x00, 0x00};
  471. unsigned char rx[512];
  472. unsigned char chksum = 0x00;
  473. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  474. if(len > 6)
  475. {
  476. if (len < 6+(rx[4] | rx[5]<<8))
  477. return result;
  478. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  479. {
  480. chksum ^= rx[6+idx];
  481. }
  482. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  483. (rx[2] == tx[1]) &&
  484. (rx[1] == tx[2]) &&
  485. (rx[3] == tx[3]))
  486. {
  487. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  488. Ret_Buf->voltage[idx] = rx[6+idx];
  489. result = PASS;
  490. }
  491. }
  492. return result;
  493. }
  494. unsigned char Query_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Ret_Buf)
  495. {
  496. unsigned char result = FAIL;
  497. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Relay_Output, 0x00, 0x00, 0x00};
  498. unsigned char rx[512];
  499. unsigned char chksum = 0x00;
  500. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  501. // for (int i = 0; i < 7; i++)
  502. // printf("tx = %x \n", tx[i]);
  503. // for (int i = 0; i < len; i++)
  504. // printf("rx = %x \n", rx[i]);
  505. if(len > 6)
  506. {
  507. if (len < 6+(rx[4] | rx[5]<<8))
  508. return result;
  509. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  510. {
  511. chksum ^= rx[6+idx];
  512. }
  513. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  514. (rx[2] == tx[1]) &&
  515. (rx[1] == tx[2]) &&
  516. (rx[3] == tx[3]))
  517. {
  518. Ret_Buf->relay_event.bits.AC_Contactor = (rx[6] >> 0) & 0x01;
  519. Ret_Buf->relay_event.bits.CCS_Precharge = (rx[6] >> 1) & 0x01;
  520. Ret_Buf->relay_event.bits.Gun1_N = (rx[7] >> 0) & 0x01;
  521. Ret_Buf->relay_event.bits.Gun1_P = (rx[7] >> 1) & 0x01;
  522. Ret_Buf->relay_event.bits.Gun1_Parallel_N = (rx[7] >> 2) & 0x01;
  523. Ret_Buf->relay_event.bits.Gun1_Parallel_P = (rx[7] >> 3) & 0x01;
  524. Ret_Buf->relay_event.bits.Gun2_N = (rx[8] >> 0) & 0x01;
  525. Ret_Buf->relay_event.bits.Gun2_P = (rx[8] >> 1) & 0x01;
  526. Ret_Buf->relay_event.bits.Gun2_Parallel_N = (rx[8] >> 2) & 0x01;
  527. Ret_Buf->relay_event.bits.Gun2_Parallel_P = (rx[8] >> 3) & 0x01;
  528. result = PASS;
  529. }
  530. }
  531. return result;
  532. }
  533. unsigned char Query_Gfd_Adc(unsigned char fd, unsigned char targetAddr, Gfd *Ret_Buf)
  534. {
  535. unsigned char result = FAIL;
  536. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Gfd_Adc, 0x00, 0x00, 0x00};
  537. unsigned char rx[512];
  538. unsigned char chksum = 0x00;
  539. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  540. // for(int i = 0; i < 7; i++)
  541. // printf ("tx = %d \n", tx[i]);
  542. if(len > 6)
  543. {
  544. if (len < 6+(rx[4] | rx[5]<<8))
  545. {
  546. //printf("Query_Gfd_Adc fail \n");
  547. return result;
  548. }
  549. // for(int i = 0; i < len; i++)
  550. // printf ("rx = %d \n", rx[i]);
  551. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  552. {
  553. chksum ^= rx[6+idx];
  554. }
  555. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  556. (rx[2] == tx[1]) &&
  557. (rx[1] == tx[2]) &&
  558. (rx[3] == tx[3]))
  559. {
  560. Ret_Buf->Resister_conn1 = (rx[6] | (rx[7] << 8));
  561. Ret_Buf->voltage_conn1 = (rx[8] | (rx[9] << 8));
  562. Ret_Buf->result_conn1 = rx[10];
  563. Ret_Buf->rb_step_1 = rx[11];
  564. Ret_Buf->Resister_conn2 = (rx[12] | (rx[13] << 8));
  565. Ret_Buf->voltage_conn2 = (rx[14] | (rx[15] << 8));
  566. Ret_Buf->result_conn2 = rx[16];
  567. Ret_Buf->rb_step_2 = rx[17];
  568. result = PASS;
  569. }
  570. }
  571. return result;
  572. }
  573. unsigned char Query_Gpio_Input(unsigned char fd, unsigned char targetAddr, Gpio_in *Ret_Buf)
  574. {
  575. unsigned char result = FAIL;
  576. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Gpio_In, 0x00, 0x00, 0x00};
  577. unsigned char rx[512];
  578. unsigned char chksum = 0x00;
  579. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  580. if(len > 6)
  581. {
  582. if (len < 6+(rx[4] | rx[5]<<8))
  583. return result;
  584. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  585. {
  586. chksum ^= rx[6+idx];
  587. }
  588. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  589. (rx[2] == tx[1]) &&
  590. (rx[1] == tx[2]) &&
  591. (rx[3] == tx[3]))
  592. {
  593. Ret_Buf->AC_Connector = (rx[6] >> 0) & 0x01;
  594. Ret_Buf->AC_MainBreaker = (rx[6] >> 1) & 0x01;
  595. Ret_Buf->SPD = (rx[6] >> 2) & 0x01;
  596. Ret_Buf->Door_Open = (rx[6] >> 3) & 0x01;
  597. Ret_Buf->GFD[0] = (rx[6] >> 4) & 0x01;
  598. Ret_Buf->GFD[1] = (rx[6] >> 5) & 0x01;
  599. Ret_Buf->AC_Drop = (rx[6] >> 6) & 0x01;
  600. Ret_Buf->Emergency_IO = (rx[7] >> 0) & 0x01;
  601. Ret_Buf->Button_Emergency_Press = (rx[8] >> 0) & 0x01;
  602. Ret_Buf->Button_On_Press = (rx[8] >> 1) & 0x01;
  603. Ret_Buf->Button_Off_Press = (rx[8] >> 2) & 0x01;
  604. Ret_Buf->Key_1_Press = (rx[8] >> 3) & 0x01;
  605. Ret_Buf->Key_2_Press = (rx[8] >> 4) & 0x01;
  606. Ret_Buf->Key_3_Press = (rx[8] >> 5) & 0x01;
  607. Ret_Buf->Key_4_Press = (rx[8] >> 6) & 0x01;
  608. result = PASS;
  609. }
  610. }
  611. return result;
  612. }
  613. unsigned char Query_Model_Name(unsigned char fd, unsigned char targetAddr, unsigned char *modelname)
  614. {
  615. unsigned char result = FAIL;
  616. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Model_Name, 0x00, 0x00, 0x00};
  617. unsigned char rx[512];
  618. unsigned char chksum = 0x00;
  619. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  620. if(len > 6)
  621. {
  622. if (len < 6+(rx[4] | rx[5]<<8))
  623. return result;
  624. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  625. {
  626. chksum ^= rx[6+idx];
  627. }
  628. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  629. (rx[2] == tx[1]) &&
  630. (rx[1] == tx[2]) &&
  631. (rx[3] == tx[3]))
  632. {
  633. strncpy((char *)modelname, (char *)(rx + 6), (rx[4] | rx[5]<<8));
  634. result = PASS;
  635. }
  636. }
  637. return result;
  638. }
  639. unsigned char Config_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Set_Buf)
  640. {
  641. unsigned char result = FAIL;
  642. 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};
  643. unsigned char rx[512];
  644. unsigned char chksum = 0x00;
  645. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  646. chksum ^= tx[6+idx];
  647. tx[14] = chksum;
  648. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  649. if(len > 6)
  650. {
  651. if (len < 6+(rx[4] | rx[5]<<8))
  652. return result;
  653. chksum = 0x00;
  654. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  655. {
  656. chksum ^= rx[6+idx];
  657. }
  658. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  659. (rx[2] == tx[1]) &&
  660. (rx[1] == tx[2]) &&
  661. (rx[3] == tx[3]) &&
  662. rx[6] == PASS)
  663. {
  664. result = PASS;
  665. }
  666. }
  667. return result;
  668. }
  669. unsigned char Config_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Set_Buf)
  670. {
  671. unsigned char result = FAIL;
  672. 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]};
  673. unsigned char rx[512];
  674. unsigned char chksum = 0x00;
  675. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  676. chksum ^= tx[6 + idx];
  677. tx[9] = chksum;
  678. // for (int i = 0; i < 10; i++)
  679. // printf("set relay cmd : tx = %x \n", tx[i]);
  680. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  681. if(len > 6)
  682. {
  683. if (len < 6+(rx[4] | rx[5]<<8))
  684. return result;
  685. // for (int i = 0; i < len; i++)
  686. // printf("set relay cmd : rx = %x \n", rx[i]);
  687. chksum = 0x00;
  688. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  689. {
  690. chksum ^= rx[6+idx];
  691. }
  692. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  693. (rx[2] == tx[1]) &&
  694. (rx[1] == tx[2]) &&
  695. (rx[3] == tx[3]) &&
  696. rx[6] == PASS)
  697. {
  698. result = PASS;
  699. }
  700. }
  701. return result;
  702. }
  703. unsigned char Config_Gpio_Output(unsigned char fd, unsigned char targetAddr, Gpio_out *Set_Buf)
  704. {
  705. unsigned char result = FAIL;
  706. unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Gpio_Output, 0x01, 0x00, 0x00, 0x00};
  707. unsigned char rx[512];
  708. unsigned char chksum = 0x00;
  709. tx[6] |= (Set_Buf->AC_Connector?0x01:0x00);
  710. for(int idx = 0;idx<2;idx++)
  711. tx[6] |= (Set_Buf->Button_LED[idx]?0x01:0x00)<<(1+idx);
  712. for(int idx = 0;idx<4;idx++)
  713. tx[6] |= (Set_Buf->System_LED[idx]?0x01:0x00)<<(3+idx);
  714. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  715. chksum ^= tx[6+idx];
  716. tx[14] = chksum;
  717. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  718. if(len > 6)
  719. {
  720. if (len < 6+(rx[4] | rx[5]<<8))
  721. return result;
  722. chksum = 0x00;
  723. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  724. {
  725. chksum ^= rx[6+idx];
  726. }
  727. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  728. (rx[2] == tx[1]) &&
  729. (rx[1] == tx[2]) &&
  730. (rx[3] == tx[3]))
  731. {
  732. result = PASS;
  733. }
  734. }
  735. return result;
  736. }
  737. unsigned char Config_Gfd_Value(unsigned char fd, unsigned char targetAddr, Gfd_config *Set_Buf)
  738. {
  739. unsigned char result = FAIL;
  740. unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Gfd_Value, 0x02, 0x00, 0x00, 0x00, 0x00};
  741. unsigned char rx[512];
  742. unsigned char chksum = 0x00;
  743. tx[6] = Set_Buf->index;
  744. tx[7] = Set_Buf->state;
  745. for(int idx = 0; idx<(tx[4] | tx[5]<<8);idx++)
  746. chksum ^= tx[6+idx];
  747. tx[8] = chksum;
  748. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  749. if(len > 6)
  750. {
  751. if (len < 6+(rx[4] | rx[5]<<8))
  752. return result;
  753. chksum = 0x00;
  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. result = PASS;
  764. }
  765. }
  766. return result;
  767. }
  768. unsigned char Config_Model_Name(unsigned char fd, unsigned char targetAddr, unsigned char *modelname)
  769. {
  770. unsigned char result = FAIL;
  771. unsigned char tx[21] = {0xaa, 0x00, targetAddr, Cmd.config_Model_Name, 0x0E, 0x00,
  772. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  773. unsigned char rx[512];
  774. unsigned char chksum = 0x00;
  775. memcpy(tx + 6, modelname, 14);
  776. for(int idx = 0; idx<(tx[4] | tx[5]<<8);idx++)
  777. chksum ^= tx[6+idx];
  778. tx[20] = chksum;
  779. // for(int i = 0; i < 21; i++)
  780. // printf ("tx = %x \n", tx[i]);
  781. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  782. // for(int i = 0; i < len; i++)
  783. // printf ("rx = %x \n", rx[i]);
  784. if(len > 6)
  785. {
  786. if (len < 6+(rx[4] | rx[5]<<8))
  787. return result;
  788. chksum = 0x00;
  789. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  790. {
  791. chksum ^= rx[6+idx];
  792. }
  793. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  794. (rx[2] == tx[1]) &&
  795. (rx[1] == tx[2]) &&
  796. (rx[3] == tx[3]) &&
  797. rx[6] == PASS)
  798. {
  799. result = PASS;
  800. }
  801. }
  802. return result;
  803. }
  804. unsigned char Config_Rtc_Data(unsigned char fd, unsigned char targetAddr, Rtc *Set_Buf)
  805. {
  806. unsigned char result = FAIL;
  807. unsigned char tx[21] = { 0xaa, 0x00, targetAddr, Cmd.config_Rtc_Data, 0x0E, 0x00, Set_Buf->RtcData[0], Set_Buf->RtcData[1],
  808. Set_Buf->RtcData[2], Set_Buf->RtcData[3], Set_Buf->RtcData[4], Set_Buf->RtcData[5], Set_Buf->RtcData[6], Set_Buf->RtcData[7],
  809. Set_Buf->RtcData[8], Set_Buf->RtcData[9], Set_Buf->RtcData[10], Set_Buf->RtcData[11], Set_Buf->RtcData[12], Set_Buf->RtcData[13]};
  810. unsigned char rx[512];
  811. unsigned char chksum = 0x00;
  812. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  813. chksum ^= tx[6 + idx];
  814. tx[20] = chksum;
  815. if (tranceive(fd, tx, sizeof(tx), rx) > 0)
  816. {
  817. chksum = 0x00;
  818. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
  819. {
  820. chksum ^= rx[6 + idx];
  821. }
  822. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  823. (rx[2] == tx[1]) &&
  824. (rx[1] == tx[2]) &&
  825. (rx[3] == tx[3]) &&
  826. rx[6] == PASS)
  827. {
  828. result = PASS;
  829. }
  830. }
  831. return result;
  832. }
  833. unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
  834. {
  835. unsigned char result = FAIL;
  836. 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};
  837. unsigned char rx[512];
  838. unsigned char chksum = 0x00;
  839. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  840. chksum ^= tx[6+idx];
  841. tx[10] = chksum;
  842. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  843. if(len > 6)
  844. {
  845. if (len < 6+(rx[4] | rx[5]<<8))
  846. return result;
  847. chksum = 0x00;
  848. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  849. {
  850. chksum ^= rx[6+idx];
  851. }
  852. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  853. (rx[2] == tx[1]) &&
  854. (rx[1] == tx[2]) &&
  855. (rx[3] == tx[3]) &&
  856. (rx[6] == 0x00))
  857. {
  858. result = PASS;
  859. }
  860. }
  861. return result;
  862. }
  863. unsigned char Update_Abord(unsigned char fd, unsigned char targetAddr)
  864. {
  865. unsigned char result = FAIL;
  866. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Start, 0x04, 0x00, 0x00};
  867. unsigned char rx[512];
  868. unsigned char chksum = 0x00;
  869. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  870. if(len > 6)
  871. {
  872. if (len < 6+(rx[4] | rx[5]<<8))
  873. return result;
  874. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  875. {
  876. chksum ^= rx[6+idx];
  877. }
  878. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  879. (rx[2] == tx[1]) &&
  880. (rx[1] == tx[2]) &&
  881. (rx[3] == tx[3]) &&
  882. (rx[6] == 0x00))
  883. {
  884. result = PASS;
  885. }
  886. }
  887. return result;
  888. }
  889. unsigned char Update_Transfer(unsigned char fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
  890. {
  891. unsigned char result = FAIL;
  892. unsigned char tx[11 + length];
  893. unsigned char rx[512];
  894. unsigned char chksum = 0x00;
  895. tx[0] = 0xaa;
  896. tx[1] = 0x00;
  897. tx[2] = targetAddr;
  898. tx[3] = Cmd.update_Transfer;
  899. tx[4] = (4 + length) & 0xff;
  900. tx[5] = ((4 + length)>>8) & 0xff;
  901. tx[6] = (startAddr>>0) & 0xff;
  902. tx[7] = (startAddr>>8) & 0xff;
  903. tx[8] = (startAddr>>16) & 0xff;
  904. tx[9] = (startAddr>>24) & 0xff;
  905. memcpy(tx+10, data, length);
  906. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  907. chksum ^= tx[6+idx];
  908. tx[sizeof(tx)-1] = chksum;
  909. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  910. if(len > 6)
  911. {
  912. if (len < 6+(rx[4] | rx[5]<<8))
  913. return result;
  914. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  915. {
  916. chksum ^= rx[6+idx];
  917. }
  918. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  919. (rx[2] == tx[1]) &&
  920. (rx[1] == tx[2]) &&
  921. (rx[3] == tx[3]) &&
  922. (rx[6] == 0x00))
  923. {
  924. result = PASS;
  925. }
  926. }
  927. return result;
  928. }
  929. unsigned char Update_Finish(unsigned char fd, unsigned char targetAddr)
  930. {
  931. unsigned char result = FAIL;
  932. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Finish, 0x04, 0x00, 0x00};
  933. unsigned char rx[512];
  934. unsigned char chksum = 0x00;
  935. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  936. if(len > 6)
  937. {
  938. if (len < 6+(rx[4] | rx[5]<<8))
  939. return result;
  940. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  941. {
  942. chksum ^= rx[6+idx];
  943. }
  944. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  945. (rx[2] == tx[1]) &&
  946. (rx[1] == tx[2]) &&
  947. (rx[3] == tx[3]) &&
  948. (rx[6] == 0x00))
  949. {
  950. result = PASS;
  951. }
  952. }
  953. return result;
  954. }
  955. unsigned char Query_AC_Status(unsigned char fd, unsigned char targetAddr, Ac_Status *Ret_Buf)
  956. {
  957. unsigned char result = FAIL;
  958. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_ac_status, 0x00, 0x00, 0x00};
  959. unsigned char rx[512];
  960. unsigned char chksum = 0x00;
  961. unsigned char len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);
  962. if(len > 6)
  963. {
  964. if (len < 6+(rx[4] | rx[5]<<8))
  965. return result;
  966. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  967. {
  968. chksum ^= rx[6+idx];
  969. }
  970. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  971. (rx[2] == tx[1]) &&
  972. (rx[1] == tx[2]) &&
  973. (rx[3] == tx[3]))
  974. {
  975. Ret_Buf->CpStatus = rx[6];
  976. Ret_Buf->CurLimit = (rx[7] | (rx[8] << 8));
  977. Ret_Buf->PilotVol_P = (rx[9] | (rx[10] << 8));
  978. Ret_Buf->PilotVol_N = (rx[11] | (rx[12] << 8));
  979. Ret_Buf->LockStatus = rx[13];
  980. Ret_Buf->RelayStatus = rx[14];
  981. Ret_Buf->ShutterStatus = rx[15];
  982. Ret_Buf->MeterStatus = rx[16];
  983. Ret_Buf->PpStatus = rx[17];
  984. Ret_Buf->MaxCurrent = rx[18];
  985. Ret_Buf->RotateSwitchStatus = rx[19];
  986. //
  987. // Ret_Buf->AC_Connector = (rx[6] >> 0) & 0x01;
  988. // Ret_Buf->AC_MainBreaker = (rx[6] >> 1) & 0x01;
  989. // Ret_Buf->SPD = (rx[6] >> 2) & 0x01;
  990. // Ret_Buf->Door_Open = (rx[6] >> 3) & 0x01;
  991. // Ret_Buf->GFD[0] = (rx[6] >> 4) & 0x01;
  992. // Ret_Buf->GFD[1] = (rx[6] >> 5) & 0x01;
  993. // Ret_Buf->AC_Drop = (rx[6] >> 6) & 0x01;
  994. //
  995. // Ret_Buf->Emergency_IO = (rx[7] >> 0) & 0x01;
  996. //
  997. // Ret_Buf->Button_Emergency_Press = (rx[8] >> 0) & 0x01;
  998. // Ret_Buf->Button_On_Press = (rx[8] >> 1) & 0x01;
  999. // Ret_Buf->Button_Off_Press = (rx[8] >> 2) & 0x01;
  1000. // Ret_Buf->Key_1_Press = (rx[8] >> 3) & 0x01;
  1001. // Ret_Buf->Key_2_Press = (rx[8] >> 4) & 0x01;
  1002. // Ret_Buf->Key_3_Press = (rx[8] >> 5) & 0x01;
  1003. // Ret_Buf->Key_4_Press = (rx[8] >> 6) & 0x01;
  1004. result = PASS;
  1005. }
  1006. }
  1007. return result;
  1008. }
  1009. unsigned char Query_AC_Alarm_Code(unsigned char fd, unsigned char targetAddr, Ac_Alarm_code *Ret_Buf)
  1010. {
  1011. unsigned char result = FAIL;
  1012. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_ac_alarm_code, 0x00, 0x00};
  1013. unsigned char rx[512];
  1014. unsigned char chksum = 0x00;
  1015. unsigned char len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);
  1016. if(len > 6)
  1017. {
  1018. if (len < 6+(rx[4] | rx[5]<<8))
  1019. return result;
  1020. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  1021. {
  1022. chksum ^= rx[6+idx];
  1023. }
  1024. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1025. (rx[2] == tx[1]) &&
  1026. (rx[1] == tx[2]) &&
  1027. (rx[3] == tx[3]))
  1028. {
  1029. Ret_Buf->AcAlarmCode = rx[6] + (rx[7] << 8) + (rx[8] << 16) + (rx[9] << 24);
  1030. result = PASS;
  1031. }
  1032. }
  1033. return result;
  1034. }
  1035. unsigned char Query_Charging_Energy(unsigned char fd, unsigned char targetAddr, Ac_Charging_energy *Ret_Buf)
  1036. {
  1037. unsigned char result = FAIL;
  1038. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_ac_output_energy, 0x00, 0x00,0x00};
  1039. unsigned char rx[512];
  1040. unsigned char chksum = 0x00;
  1041. unsigned char len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);
  1042. if(len > 6)
  1043. {
  1044. if (len < 6+(rx[4] | rx[5]<<8))
  1045. return result;
  1046. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  1047. {
  1048. chksum ^= rx[6+idx];
  1049. }
  1050. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1051. (rx[2] == tx[1]) &&
  1052. (rx[1] == tx[2]) &&
  1053. (rx[3] == tx[3]))
  1054. {
  1055. Ret_Buf->Energy = rx[6] + (rx[7] << 8) + (rx[8] << 16) + (rx[9] << 24);
  1056. result = PASS;
  1057. }
  1058. }
  1059. return result;
  1060. }
  1061. unsigned char Query_Charging_Current(unsigned char fd, unsigned char targetAddr, Ac_Charging_current *Ret_Buf)
  1062. {
  1063. unsigned char result = FAIL;
  1064. unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_ac_output_current, 0x00, 0x00, 0x00};
  1065. unsigned char rx[512];
  1066. unsigned char chksum = 0x00;
  1067. unsigned char len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);
  1068. if(len > 6)
  1069. {
  1070. if (len < 6+(rx[4] | rx[5]<<8))
  1071. return result;
  1072. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  1073. {
  1074. chksum ^= rx[6+idx];
  1075. }
  1076. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1077. (rx[2] == tx[1]) &&
  1078. (rx[1] == tx[2]) &&
  1079. (rx[3] == tx[3]))
  1080. {
  1081. Ret_Buf->OuputCurrentL1 = rx[6] + (rx[7] << 8);
  1082. Ret_Buf->OuputCurrentL2 = rx[8] + (rx[9] << 8);
  1083. Ret_Buf->OuputCurrentL3 = rx[10] + (rx[11] << 8);
  1084. result = PASS;
  1085. }
  1086. }
  1087. return result;
  1088. }
  1089. unsigned char Config_LED_Status(unsigned char fd, unsigned char targetAddr, Ac_Led_Status *Ret_Buf)
  1090. {
  1091. unsigned char result = FAIL;
  1092. unsigned char tx[12] = {0xaa, 0x00, targetAddr, Cmd.config_ac_led_status, 0x05, 0x00, Ret_Buf->ActionMode, (Ret_Buf->AcAlarmCode >> 0) & 0xFF,
  1093. (Ret_Buf->AcAlarmCode >> 8) & 0xFF, (Ret_Buf->AcAlarmCode >> 16) & 0xFF, (Ret_Buf->AcAlarmCode >> 24) & 0xFF};
  1094. unsigned char rx[512];
  1095. unsigned char chksum = 0x00;
  1096. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  1097. chksum ^= tx[6 + idx];
  1098. tx[11] = chksum;
  1099. if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0)
  1100. {
  1101. chksum = 0x00;
  1102. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
  1103. {
  1104. chksum ^= rx[6 + idx];
  1105. }
  1106. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1107. (rx[2] == tx[1]) &&
  1108. (rx[1] == tx[2]) &&
  1109. (rx[3] == tx[3]) &&
  1110. rx[6] == PASS)
  1111. {
  1112. result = PASS;
  1113. }
  1114. }
  1115. return result;
  1116. }
  1117. unsigned char Config_Legacy_Req(unsigned char fd, unsigned char targetAddr, unsigned char _switch)
  1118. {
  1119. unsigned char result = FAIL;
  1120. unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Legacy_Req, 0x02, 0x00, _switch, 0x00};
  1121. unsigned char rx[512];
  1122. unsigned char chksum = 0x00;
  1123. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  1124. chksum ^= tx[6 + idx];
  1125. tx[8] = chksum;
  1126. if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0)
  1127. {
  1128. chksum = 0x00;
  1129. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
  1130. {
  1131. chksum ^= rx[6 + idx];
  1132. }
  1133. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1134. (rx[2] == tx[1]) &&
  1135. (rx[1] == tx[2]) &&
  1136. (rx[3] == tx[3]) &&
  1137. rx[6] == PASS)
  1138. {
  1139. result = PASS;
  1140. }
  1141. }
  1142. return result;
  1143. }
  1144. unsigned char Config_Ac_Duty(unsigned char fd, unsigned char targetAddr, unsigned char _value)
  1145. {
  1146. unsigned char result = FAIL;
  1147. unsigned char tx[8] = {0xaa, 0x00, targetAddr, Cmd.config_ac_duty, 0x01, 0x00, _value};
  1148. unsigned char rx[512];
  1149. unsigned char chksum = 0x00;
  1150. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  1151. chksum ^= tx[6 + idx];
  1152. tx[7] = chksum;
  1153. if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0)
  1154. {
  1155. chksum = 0x00;
  1156. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
  1157. {
  1158. chksum ^= rx[6 + idx];
  1159. }
  1160. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1161. (rx[2] == tx[1]) &&
  1162. (rx[1] == tx[2]) &&
  1163. (rx[3] == tx[3]) &&
  1164. rx[6] == PASS)
  1165. {
  1166. result = PASS;
  1167. }
  1168. }
  1169. return result;
  1170. }
  1171. unsigned char Config_CSU_Mode(unsigned char fd, unsigned char targetAddr)
  1172. {
  1173. unsigned char result = FAIL;
  1174. unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_csu_mode, 0x02, 0x00, 0x01, 0x00};
  1175. unsigned char rx[512];
  1176. unsigned char chksum = 0x00;
  1177. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  1178. chksum ^= tx[6 + idx];
  1179. tx[7] = chksum;
  1180. if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0)
  1181. {
  1182. chksum = 0x00;
  1183. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
  1184. {
  1185. chksum ^= rx[6 + idx];
  1186. }
  1187. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1188. (rx[2] == tx[1]) &&
  1189. (rx[1] == tx[2]) &&
  1190. (rx[3] == tx[3]) &&
  1191. rx[6] == PASS)
  1192. {
  1193. result = PASS;
  1194. }
  1195. }
  1196. return result;
  1197. }
  1198. unsigned char Config_Reset_MCU(unsigned char fd, unsigned char targetAddr)
  1199. {
  1200. unsigned char result = FAIL;
  1201. unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_reset_mcu, 0x02, 0x00, 0x01, 0x00};
  1202. unsigned char rx[512];
  1203. unsigned char chksum = 0x00;
  1204. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  1205. chksum ^= tx[6 + idx];
  1206. tx[7] = chksum;
  1207. if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0)
  1208. {
  1209. chksum = 0x00;
  1210. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
  1211. {
  1212. chksum ^= rx[6 + idx];
  1213. }
  1214. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1215. (rx[2] == tx[1]) &&
  1216. (rx[1] == tx[2]) &&
  1217. (rx[3] == tx[3]) &&
  1218. rx[6] == PASS)
  1219. {
  1220. result = PASS;
  1221. }
  1222. }
  1223. return result;
  1224. }
  1225. unsigned char Config_Led_Color(unsigned char fd, unsigned char targetAddr, Led_Color *Ret_Buf)
  1226. {
  1227. unsigned char result = FAIL;
  1228. unsigned char tx[13] = {0xaa, 0x00, targetAddr, Cmd.config_led_color, 0x06, 0x00,
  1229. Ret_Buf->Connect_1_Red, Ret_Buf->Connect_1_Green, Ret_Buf->Connect_1_Blue,
  1230. Ret_Buf->Connect_2_Red, Ret_Buf->Connect_2_Green, Ret_Buf->Connect_2_Blue};
  1231. unsigned char rx[512];
  1232. unsigned char chksum = 0x00;
  1233. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
  1234. chksum ^= tx[6 + idx];
  1235. tx[12] = chksum;
  1236. // for(int i = 0; i < 13; i++)
  1237. // printf ("tx = %x \n", tx[i]);
  1238. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1239. // for(int i = 0; i < len; i++)
  1240. // printf ("rx = %x \n", rx[i]);
  1241. if(len > 6)
  1242. {
  1243. if (len < 6+(rx[4] | rx[5]<<8))
  1244. return result;
  1245. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  1246. {
  1247. chksum ^= rx[6+idx];
  1248. }
  1249. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1250. (rx[2] == tx[1]) &&
  1251. (rx[1] == tx[2]) &&
  1252. (rx[3] == tx[3]) &&
  1253. (rx[6] == PASS))
  1254. {
  1255. result = PASS;
  1256. }
  1257. }
  1258. return result;
  1259. }