internalComm.c 27 KB

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