internalComm.c 31 KB

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