hlw8110_handle.c 25 KB

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