hlw8110_handle.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. #include "hlw8110_handle.h"
  2. #include <fcntl.h>
  3. #include "unistd.h"
  4. #include <termios.h>
  5. #include <string.h>
  6. #include "stdio.h"
  7. #include "elog.h"
  8. union IntData
  9. {
  10. unsigned int inte;
  11. unsigned char byte[2];
  12. };
  13. union LongData
  14. {
  15. unsigned long int word;
  16. unsigned int inte[2];
  17. unsigned char byte[4];
  18. };
  19. static uint8_t hlw8110_checksum_write(LowConf *conf)
  20. {
  21. uint32_t a = 0x0000;
  22. uint8_t check_sum = 0;
  23. for(int i =0; i < (conf->tx_lenth-1);i++)
  24. a += conf->serial_tx_buf[i];
  25. a = ~a;
  26. check_sum = a & 0xff;
  27. return check_sum;
  28. }
  29. static uint8_t hlw8110_checksum_read(LowConf *conf)
  30. {
  31. uint32_t a = 0x0000;
  32. uint8_t check_sum = 0;
  33. for(int i =0; i < (conf->rx_lenth-1);i++)
  34. a += (conf->serial_rx_buf[i]);
  35. a = a + conf->serial_tx_buf[0] + conf->serial_tx_buf[1];
  36. a = ~a;
  37. check_sum = a & 0xff;
  38. return check_sum;
  39. }
  40. int hlw8110_write_reg_en(LowConf *conf)
  41. {
  42. if(!conf)
  43. return -1;
  44. conf->tx_lenth = 4;
  45. conf->rx_lenth = 0;
  46. conf->serial_tx_buf[0] = 0xA5;
  47. conf->serial_tx_buf[1] = 0xEA;
  48. conf->serial_tx_buf[2] = 0xE5;
  49. conf->serial_tx_buf[3] =hlw8110_checksum_write(conf);
  50. int size = write(conf->h_hlw8110_fd,conf->serial_tx_buf,conf->tx_lenth);
  51. _DEBUG(SET,conf->serial_tx_buf,size);
  52. if(size == conf->tx_lenth)
  53. {
  54. return 0;
  55. }
  56. return -1;
  57. }
  58. int hlw8110_write_reg_dis(LowConf *conf)
  59. {
  60. if(!conf)
  61. return -1;
  62. conf->tx_lenth = 4;
  63. conf->rx_lenth = 0;
  64. conf->serial_tx_buf[0] = 0xA5;
  65. conf->serial_tx_buf[1] = 0xEA;
  66. conf->serial_tx_buf[2] = 0xDC;
  67. conf->serial_tx_buf[3] =hlw8110_checksum_write(conf);
  68. int size = write(conf->h_hlw8110_fd,conf->serial_tx_buf,conf->tx_lenth);
  69. _DEBUG(SET,conf->serial_tx_buf,size);
  70. if(size == conf->tx_lenth)
  71. {
  72. return 0;
  73. }
  74. printf("%s: %x\n",__func__,conf->serial_tx_buf[3]);
  75. return -1;
  76. }
  77. int hlw8110_set_channel(LowConf *conf,uint8_t channel)
  78. {
  79. if(channel == CHANEL_A || channel == CHANEL_B)
  80. {
  81. conf->tx_lenth = 4;
  82. conf->rx_lenth = 0;
  83. conf->serial_tx_buf[0] = 0xA5;
  84. conf->serial_tx_buf[1] = 0xEA;
  85. conf->serial_tx_buf[2] = (channel == CHANEL_A) ? 0x5a : 0xa5;
  86. conf->serial_tx_buf[3] =hlw8110_checksum_write(conf);
  87. int size = write(conf->h_hlw8110_fd,conf->serial_tx_buf,conf->tx_lenth);
  88. _DEBUG(SET,conf->serial_tx_buf,size);
  89. if(size == conf->tx_lenth)
  90. {
  91. conf->current_channel = channel;
  92. return 0;
  93. }
  94. printf("%s: %x\n",__func__,conf->serial_tx_buf[3]);
  95. return -1;
  96. }
  97. printf("set channel %d error!!!",channel);
  98. return -1;
  99. }
  100. int hlw8110_read_reg(LowConf *conf,uint8_t ADDR_Reg,int reg_lenth)
  101. {
  102. if(conf)
  103. {
  104. conf->serial_tx_buf[0] = 0xa5;
  105. conf->serial_tx_buf[1] = ADDR_Reg;
  106. conf->tx_lenth = 2;
  107. conf->rx_lenth = reg_lenth+1;
  108. int size = write(conf->h_hlw8110_fd,conf->serial_tx_buf,conf->tx_lenth);
  109. _DEBUG(SET,conf->serial_tx_buf,size);
  110. if(size == conf->tx_lenth)
  111. {
  112. return 0;
  113. }
  114. printf("%s: %x\n",__func__,conf->serial_tx_buf[3]);
  115. return -1;
  116. }
  117. printf("read reg %X faile!!!",ADDR_Reg);
  118. return -1;
  119. }
  120. int hlw8110_write_reg(LowConf *conf,uint8_t ADDR_Reg,int reg_lenth,uint32_t _data)
  121. {
  122. if(conf)
  123. {
  124. union LongData Temp_u32_a;
  125. conf->serial_tx_buf[0] = 0xa5;
  126. conf->serial_tx_buf[1] = ADDR_Reg|0x80;
  127. Temp_u32_a.word = _data;
  128. for(int i = 0; i < reg_lenth;i++)
  129. {
  130. conf->serial_tx_buf[i+2] = Temp_u32_a.byte[reg_lenth-1-i];
  131. }
  132. conf->tx_lenth = 3 + reg_lenth;
  133. conf->rx_lenth = 0;
  134. conf->serial_tx_buf[conf->tx_lenth-1] = hlw8110_checksum_write(conf);
  135. int size = write(conf->h_hlw8110_fd,conf->serial_tx_buf,conf->tx_lenth);
  136. _DEBUG(SET,conf->serial_tx_buf,size);
  137. if(size == conf->tx_lenth)
  138. {
  139. return 0;
  140. }
  141. }
  142. return -1;
  143. }
  144. int hlw8110_reset(LowConf *conf)
  145. {
  146. if(conf)
  147. {
  148. conf->tx_lenth = 4;
  149. conf->rx_lenth = 0;
  150. conf->serial_tx_buf[0] = 0xa5;
  151. conf->serial_tx_buf[1] = 0xea;
  152. conf->serial_tx_buf[2] = 0x96;
  153. conf->serial_tx_buf[3] = hlw8110_checksum_write(conf);
  154. int size = write(conf->h_hlw8110_fd,conf->serial_tx_buf,conf->tx_lenth);
  155. _DEBUG(SET,conf->serial_tx_buf,size);
  156. if(size == conf->tx_lenth)
  157. {
  158. return 0;
  159. }
  160. }
  161. return -1;
  162. }
  163. uint8_t hlw8110_judge_chechsum_calfac(LowConf *conf)
  164. {
  165. uint32_t a;
  166. uint32_t d;
  167. int size = 0;
  168. //read RmsIAC RmsIBC RmsPAC PowerPAC PowrPBC PowerSC EnergBc EnergAc
  169. if(conf)
  170. {
  171. if(hlw8110_read_reg(conf,REG_RMS_IAC_ADDR,2))printf("%s: read reg filed\n",__func__);
  172. usleep(D_TIME_50MS);
  173. memset(conf->serial_rx_buf,0,10);
  174. size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  175. _DEBUG(GET,conf->serial_rx_buf,size);
  176. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  177. {
  178. conf->U16_RMSIAC_RegData = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
  179. }else
  180. printf("read U16_RMSIAC_RegData filed\n");
  181. if(hlw8110_read_reg(conf,REG_RMS_IBC_ADDR,2))printf("%s: read reg filed\n",__func__);
  182. usleep(D_TIME_50MS);
  183. memset(conf->serial_rx_buf,0,10);
  184. size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  185. _DEBUG(GET,conf->serial_rx_buf,size);
  186. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  187. {
  188. conf->U16_RMSIBC_RegData = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
  189. }else
  190. printf("read U16_RMSIBC_RegData filed\n");
  191. if(hlw8110_read_reg(conf,REG_RMS_UC_ADDR,2))printf("%s: read reg filed\n",__func__);
  192. usleep(D_TIME_50MS);
  193. memset(conf->serial_rx_buf,0,10);
  194. size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  195. _DEBUG(GET,conf->serial_rx_buf,size);
  196. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  197. {
  198. conf->U16_RMSUC_RegData = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
  199. }else
  200. printf("read U16_RMSUC_RegData filed\n");
  201. if(hlw8110_read_reg(conf,REG_POWER_PAC_ADDR,2))printf("%s: read reg filed\n",__func__);
  202. usleep(D_TIME_50MS);
  203. memset(conf->serial_rx_buf,0,10);
  204. size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  205. _DEBUG(GET,conf->serial_rx_buf,size);
  206. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  207. {
  208. conf->U16_PowerPAC_RegData = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
  209. }else
  210. printf("read U16_PowerPAC_RegData filed\n");
  211. if(hlw8110_read_reg(conf,REG_POWER_PBC_ADDR,2))printf("%s: read reg filed\n",__func__);
  212. usleep(D_TIME_50MS);
  213. memset(conf->serial_rx_buf,0,10);
  214. size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  215. _DEBUG(GET,conf->serial_rx_buf,size);
  216. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  217. {
  218. conf->U16_PowerPBC_RegData = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
  219. }else
  220. printf("read U16_PowerPBC_RegData filed\n");
  221. if(hlw8110_read_reg(conf,REG_POWER_SC_ADDR,2))printf("%s: read reg filed\n",__func__);
  222. usleep(D_TIME_50MS);
  223. memset(conf->serial_rx_buf,0,10);
  224. size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  225. _DEBUG(GET,conf->serial_rx_buf,size);
  226. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  227. {
  228. conf->U16_PowerSC_RegData = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
  229. }else
  230. printf("read U16_PowerSC_RegData filed\n");
  231. if(hlw8110_read_reg(conf,REG_ENERGY_AC_ADDR,2))printf("%s: read reg filed\n",__func__);
  232. usleep(D_TIME_50MS);
  233. memset(conf->serial_rx_buf,0,10);
  234. size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  235. _DEBUG(GET,conf->serial_rx_buf,size);
  236. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  237. {
  238. conf->U16_EnergyAC_RegData = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
  239. }else
  240. printf("read U16_PowerSC_RegData filed\n");
  241. if(hlw8110_read_reg(conf,REG_ENERGY_BC_ADDR,2))printf("%s: read reg filed\n",__func__);
  242. usleep(D_TIME_50MS);
  243. memset(conf->serial_rx_buf,0,10);
  244. size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  245. _DEBUG(GET,conf->serial_rx_buf,size);
  246. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  247. {
  248. conf->U16_EnergyBC_RegData = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
  249. }else
  250. printf("read U16_EnergyBC_RegData filed\n");
  251. if(hlw8110_read_reg(conf,REG_CHECKSUM_ADDR,2))printf("%s: read reg filed\n",__func__);
  252. usleep(D_TIME_50MS);
  253. memset(conf->serial_rx_buf,0,10);
  254. size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  255. _DEBUG(GET,conf->serial_rx_buf,size);
  256. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  257. {
  258. conf->U16_CheckSUM_RegData = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
  259. }else
  260. printf("read U16_CheckSUM_RegData filed\n");
  261. a=0;
  262. a=~(
  263. 0xffff +
  264. conf->U16_RMSIAC_RegData +
  265. conf->U16_RMSIBC_RegData +
  266. conf->U16_RMSUC_RegData +
  267. conf->U16_PowerPAC_RegData +
  268. conf->U16_PowerPBC_RegData +
  269. conf->U16_PowerSC_RegData +
  270. conf->U16_EnergyAC_RegData +
  271. conf->U16_EnergyBC_RegData
  272. );
  273. conf->U16_CheckSUM_Data = a & 0xffff;
  274. if(conf->U16_CheckSUM_Data == conf->U16_CheckSUM_RegData)
  275. {
  276. d = 1;
  277. }else
  278. d = 0;
  279. return d;
  280. }
  281. return 0;
  282. }
  283. int hlw8110_write_reg_sucess(LowConf *conf)
  284. {
  285. if(conf)
  286. {
  287. hlw8110_read_reg(conf,REG_SYSCON_ADDR,2);
  288. usleep(D_TIME_10MS);
  289. memset(conf->serial_rx_buf,0,10);
  290. int size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  291. _DEBUG(GET,conf->serial_rx_buf,size);
  292. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  293. {
  294. conf->U16_Check_SysconReg_Data = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
  295. }else
  296. printf("read U16_Check_SysconReg_Data filed\n");
  297. hlw8110_read_reg(conf,REG_EMUCON_ADDR,2);
  298. usleep(D_TIME_10MS);
  299. memset(conf->serial_rx_buf,0,10);
  300. size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  301. _DEBUG(GET,conf->serial_rx_buf,size);
  302. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  303. {
  304. conf->U16_Check_EmuconReg_Data = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
  305. }else
  306. printf("read U16_Check_EmuconReg_Data filed\n");
  307. hlw8110_read_reg(conf,REG_EMUCON2_ADDR,2);
  308. usleep(D_TIME_10MS);
  309. memset(conf->serial_rx_buf,0,10);
  310. size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  311. _DEBUG(GET,conf->serial_rx_buf,size);
  312. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  313. {
  314. conf->U16_Check_Emucon2Reg_Data = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
  315. }else
  316. printf("read U16_Check_Emucon2Reg_Data filed\n");
  317. return 0;
  318. }
  319. return -1;
  320. }
  321. static int uart_setup(LowConf *conf)
  322. {
  323. if(tcgetattr(conf->h_hlw8110_fd,&(conf->bak_opt)) < 0) {
  324. return -1;
  325. }
  326. memcpy(&(conf->new_opt),&(conf->bak_opt),sizeof(struct termios));
  327. // 修改控制模式,保证程序不会占用串口
  328. conf->new_opt.c_cflag |= CLOCAL;
  329. // 修改控制模式,能够从串口读取数据
  330. conf->new_opt.c_cflag |= CREAD;
  331. // 不使用流控制
  332. conf->new_opt.c_cflag &= ~CRTSCTS;
  333. // 设置数据位
  334. conf->new_opt.c_cflag &= ~CSIZE;
  335. conf->new_opt.c_cflag |= CS8;
  336. // 设置奇偶校验位
  337. conf->new_opt.c_cflag |= PARENB;
  338. conf->new_opt.c_cflag &= ~PARODD;
  339. conf->new_opt.c_iflag &= ~ICRNL;
  340. conf->new_opt.c_iflag |= INPCK;
  341. // 设置停止位
  342. conf->new_opt.c_cflag &= ~CSTOPB;
  343. // 设置最少字符和等待时间
  344. conf->new_opt.c_cc[VMIN] = 1; // 读数据的最小字节数
  345. conf->new_opt.c_cc[VTIME] = 0; //等待第1个数据,单位是10s
  346. // 修改输出模式,原始数据输出
  347. conf->new_opt.c_oflag &= ~OPOST;
  348. conf->new_opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  349. conf->new_opt.c_iflag &= ~ (IXON | IXOFF | IXANY);
  350. // 设置波特率
  351. cfsetispeed(&(conf->new_opt), B9600);
  352. cfsetospeed(&(conf->new_opt), B9600);
  353. // 清空终端未完成的数据
  354. tcflush(conf->h_hlw8110_fd, TCIFLUSH);
  355. // 设置新属性
  356. if(tcsetattr(conf->h_hlw8110_fd, TCSANOW, &(conf->new_opt)) < 0) {
  357. return -1;
  358. }
  359. return 0;
  360. }
  361. float hlw8110_read_UV(LowConf *conf)
  362. {
  363. float a;
  364. hlw8110_read_reg(conf,REG_RMSU_ADDR,3);
  365. usleep(D_TIME_50MS);
  366. memset(conf->serial_rx_buf,0,10);
  367. int size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  368. _DEBUG(GET,conf->serial_rx_buf,size);
  369. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  370. {
  371. conf->U32_RMSU_RegData =(uint32_t)(conf->serial_rx_buf[0]<<16) +
  372. (uint32_t)(conf->serial_rx_buf[1]<<8) +
  373. (uint32_t)(conf->serial_rx_buf[2]);
  374. }else {
  375. log_e("read U32_RMSU_RegData filed\n");
  376. }
  377. if ((conf->U32_RMSU_RegData &0x800000) == 0x800000)
  378. {
  379. a = 0;
  380. }else
  381. {
  382. a = (float)conf->U32_RMSU_RegData;
  383. a = a*(conf->U16_RMSUC_RegData);
  384. a = a/0x400000;
  385. a = a/0.47;
  386. a = a/100;
  387. a = a*D_CAL_U;
  388. conf->F_AC_V = a;
  389. }
  390. return conf->F_AC_V;
  391. }
  392. float hlw8110_read_IA(LowConf *conf)
  393. {
  394. float a;
  395. hlw8110_read_reg(conf,REG_RMSIA_ADDR,3);
  396. usleep(D_TIME_50MS);
  397. memset(conf->serial_rx_buf,0,10);
  398. int size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  399. _DEBUG(GET,conf->serial_rx_buf,size);
  400. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  401. {
  402. conf->U32_RMSIA_RegData =(uint32_t)(conf->serial_rx_buf[0]<<16) +
  403. (uint32_t)(conf->serial_rx_buf[1]<<8) +
  404. (uint32_t)(conf->serial_rx_buf[2]);
  405. }else {
  406. log_e("read U32_RMSIA_RegData filed\n");
  407. }
  408. if ((conf->U32_RMSIA_RegData &0x800000) == 0x800000)
  409. {
  410. a = 0;
  411. }else
  412. {
  413. a = (float)conf->U32_RMSIA_RegData;
  414. a = a*(conf->U16_RMSIAC_RegData);
  415. a = a/0x800000;
  416. a = a/1;
  417. a = a /500;
  418. a = a*D_CAL_U;
  419. conf->F_AC_I = a;
  420. }
  421. return conf->F_AC_I;
  422. }
  423. float hwl8110_read_PA(LowConf *conf)
  424. {
  425. float a;
  426. float b;
  427. hlw8110_read_reg(conf,REG_POWER_PA_ADDR,4);
  428. usleep(D_TIME_50MS);
  429. memset(conf->serial_rx_buf,0,10);
  430. int size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  431. _DEBUG(GET,conf->serial_rx_buf,size);
  432. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  433. {
  434. conf->U32_POWERPA_RegData =(uint32_t)(conf->serial_rx_buf[0]<<24) +
  435. (uint32_t)(conf->serial_rx_buf[1]<<16) +
  436. (uint32_t)(conf->serial_rx_buf[2]<<8) +
  437. (uint32_t)(conf->serial_rx_buf[3]);
  438. }else {
  439. log_e("read U32_POWERPA_RegData filed\n");
  440. }
  441. if (conf->U32_POWERPA_RegData > 0x40000000)
  442. {
  443. b = ~(conf->U32_POWERPA_RegData);
  444. a = (float)b;
  445. }else
  446. {
  447. a = (float)conf->U32_POWERPA_RegData;
  448. }
  449. a = a*(conf->U16_PowerPAC_RegData);
  450. a = a/0x40000000;
  451. a = a/0.47;
  452. a = a/1.0;
  453. a = a*D_CAL_U;
  454. conf->F_AC_P = a;
  455. return a;
  456. }
  457. float hwl8110_read_EA(LowConf *conf)
  458. {
  459. float a;
  460. hlw8110_read_reg(conf,REG_ENERGY_PA_ADDR,3);
  461. usleep(D_TIME_50MS);
  462. int size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  463. _DEBUG(GET,conf->serial_rx_buf,size);
  464. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  465. {
  466. conf->U32_ENERGY_PA_RegData =(uint32_t)(conf->serial_rx_buf[0]<<16) +
  467. (uint32_t)(conf->serial_rx_buf[1]<<8) +
  468. (uint32_t)(conf->serial_rx_buf[2]);
  469. }else {
  470. log_e("read U32_RMSIA_RegData filed\n");
  471. }
  472. hlw8110_read_reg(conf,REG_HFCONST_ADDR,2);
  473. usleep(D_TIME_50MS);
  474. size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  475. _DEBUG(GET,conf->serial_rx_buf,size);
  476. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  477. {
  478. conf->U16_HFConst_RegData = (uint32_t)(conf->serial_rx_buf[0]<<8) +
  479. (uint32_t)(conf->serial_rx_buf[1]);
  480. }else {
  481. log_e("read U16_HFConst_RegData filed\n");
  482. }
  483. a = (float)conf->U32_ENERGY_PA_RegData;
  484. a = a*(conf->U16_EnergyAC_RegData);
  485. a = a/0x20000000;
  486. a = a/0.47;
  487. a = a/1;
  488. a = a * D_CAL_A_E;
  489. conf->F_AC_E = a;
  490. conf->F_AC_BACKUP_E = conf->F_AC_E;
  491. return conf->F_AC_E;
  492. }
  493. float hwl8110_read_LineFreq(LowConf *conf)
  494. {
  495. float a = 0.0;
  496. uint16_t b = 0;
  497. hlw8110_read_reg(conf,REG_UFREQ_ADDR,2);
  498. usleep(D_TIME_50MS);
  499. int size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  500. _DEBUG(GET,conf->serial_rx_buf,size);
  501. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  502. {
  503. b = (uint16_t)(conf->serial_rx_buf[0]<<8) +
  504. (uint16_t)(conf->serial_rx_buf[1]);
  505. }else {
  506. log_e("read U16_HFConst_RegData filed\n");
  507. }
  508. //char *buf = (char *)&(b);
  509. //printf("b[0]=%x b[1]=%x\n",buf[0],buf[1]);
  510. a = (float)b;
  511. if (a!=0)
  512. {
  513. a = 3579545/(8*a);
  514. }
  515. conf->F_AC_LINE_Freq = a;
  516. return conf->F_AC_LINE_Freq;
  517. }
  518. float hwl8110_read_PF(LowConf *conf)
  519. {
  520. float a;
  521. uint32_t b;
  522. hlw8110_read_reg(conf,REG_PF_ADDR,3);
  523. usleep(D_TIME_50MS);
  524. int size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  525. _DEBUG(GET,conf->serial_rx_buf,size);
  526. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  527. {
  528. b = (uint32_t)(conf->serial_rx_buf[0]<<16) +
  529. (uint32_t)(conf->serial_rx_buf[1]<< 8) +
  530. (uint32_t)(conf->serial_rx_buf[2]);
  531. }else {
  532. log_e("read U16_HFConst_RegData filed\n");
  533. }
  534. if (b>0x800000)
  535. {
  536. a = (float)(0xffffff-b + 1)/0x7fffff;
  537. }
  538. else
  539. {
  540. a = (float)b/0x7fffff;
  541. }
  542. if (conf->F_AC_P < 0.3)
  543. a = 0;
  544. conf->F_AC_P = a;
  545. return a;
  546. }
  547. float hlw8110_read_Angle(LowConf *conf)
  548. {
  549. float a;
  550. uint16_t b= 0;
  551. hlw8110_read_reg(conf,REG_ANGLE_ADDR,2);
  552. usleep(D_TIME_50MS);
  553. int size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
  554. _DEBUG(GET,conf->serial_rx_buf,size);
  555. if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
  556. {
  557. b = (uint32_t)(conf->serial_rx_buf[0]<<8) +
  558. (uint32_t)(conf->serial_rx_buf[1]);
  559. }else {
  560. log_e("read REG_ANGLE_ADDR filed\n");
  561. }
  562. if ( conf->F_AC_PF < 55)
  563. {
  564. a = b;
  565. a = a * 0.0805;
  566. conf->F_Angle = a;
  567. }
  568. else
  569. {
  570. a = b;
  571. a = a * 0.0965;
  572. conf->F_Angle = a;
  573. }
  574. if (conf->F_AC_P < 0.5)
  575. {
  576. conf->F_Angle = 0;
  577. }
  578. if (conf->F_Angle < 90)
  579. {
  580. a = conf->F_Angle;
  581. }
  582. else if (conf->F_Angle < 180)
  583. {
  584. a = 180-conf->F_Angle;
  585. }
  586. else if (conf->F_Angle < 360)
  587. {
  588. a = 360 - conf->F_Angle;
  589. }
  590. else
  591. {
  592. a = conf->F_Angle -360;
  593. }
  594. return a;
  595. }
  596. int hlw8110_read_Status(LowConf* conf,int* nStatus)
  597. {
  598. char value_path[64];
  599. char buf[3]; // 用于存储读取到的GPIO值,通常为'0'或'1'
  600. ssize_t bytesRead;
  601. int fd;
  602. // 构造GPIO值的路径
  603. snprintf(value_path, sizeof(value_path), GPIO_VALUE_PATH, GPIO_PE3);
  604. // 打开GPIO值文件以读取
  605. fd = open(value_path, O_RDONLY);
  606. if (fd == -1) {
  607. perror("Failed to open GPIO value for reading");
  608. return 1;
  609. }
  610. // 读取GPIO的值
  611. bytesRead = read(fd, buf, sizeof(buf) - 1);
  612. if (bytesRead == -1) {
  613. perror("Failed to read from GPIO value");
  614. close(fd);
  615. return 1;
  616. }
  617. // 确保字符串以null终止
  618. buf[bytesRead] = '\0';
  619. // 打印读取到的GPIO值
  620. // printf("GPIO %d value: %s\n", GPIO_PE3, buf);
  621. if (buf[0]=='0')
  622. {
  623. *nStatus=1;
  624. }else{
  625. *nStatus=0;
  626. }
  627. // 关闭文件描述符
  628. close(fd);
  629. return 0;
  630. }
  631. LowConf * hlw8110_init(void)
  632. {
  633. // int ret;
  634. static LowConf conf;
  635. memset(&conf,0,sizeof(LowConf));
  636. conf.h_hlw8110_fd = open("/dev/ttyS4", O_RDWR | O_NOCTTY | O_NDELAY);
  637. if(conf.h_hlw8110_fd < 0)
  638. return NULL;
  639. else
  640. fcntl(conf.h_hlw8110_fd, F_SETFL, O_NONBLOCK);
  641. uart_setup(&conf);
  642. hlw8110_write_reg_en(&conf);
  643. usleep(D_TIME_10MS);
  644. hlw8110_set_channel(&conf,CHANEL_A);
  645. usleep(D_TIME_10MS);
  646. hlw8110_write_reg(&conf,REG_SYSCON_ADDR,2,0x0a04);
  647. usleep(D_TIME_10MS);
  648. hlw8110_write_reg(&conf,REG_EMUCON_ADDR,2,0x0001);
  649. usleep(D_TIME_10MS);
  650. //hlw8110_write_reg(&conf,REG_EMUCON_ADDR,2,0x0018);
  651. hlw8110_write_reg(&conf,REG_EMUCON2_ADDR,2,0x0065);
  652. usleep(D_TIME_10MS);
  653. hlw8110_write_reg_dis(&conf);
  654. usleep(D_TIME_10MS);
  655. hlw8110_judge_chechsum_calfac(&conf);
  656. usleep(D_TIME_10MS);
  657. hlw8110_write_reg_sucess(&conf);
  658. conf.opreation = 1;
  659. return &conf;
  660. }
  661. void hlw8110_deinit(LowConf *conf)
  662. {
  663. tcsetattr(conf->h_hlw8110_fd, TCSANOW, &(conf->bak_opt));
  664. close(conf->h_hlw8110_fd);
  665. memset(conf,0,sizeof(LowConf));
  666. }
  667. LowConf* hlw8110_Reset(LowConf *conf)
  668. {
  669. hlw8110_reset(conf);
  670. usleep(10000);
  671. hlw8110_deinit(conf);
  672. usleep(10000);
  673. conf = hlw8110_init();
  674. return conf;
  675. }