internalComm.c 33 KB

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