internalComm.c 34 KB

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