You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.7 KiB

12 months ago
12 months ago
12 months ago
  1. #include "hpp272.hpp"
  2. #include "stm32basic/modbus/modbus_block_host.hpp"
  3. using namespace iflytop;
  4. #define TAG "HPP272"
  5. void HPP272::init(ModbusBlockHost* modbusBlockHost) {
  6. m_modbusBlockHost = modbusBlockHost;
  7. m_id = 240;
  8. m_cache_lock.init();
  9. }
  10. bool HPP272::ping(int id) {
  11. int16_t val = 0;
  12. return m_modbusBlockHost->readReg03(id, 0x1F00, (uint16_t*)&val, 100);
  13. }
  14. void HPP272::setid(int32_t id) { m_id = id; }
  15. void HPP272::read_calibration_date(int32_t* year, int32_t* month, int32_t* day) { // TODO
  16. *year = 0;
  17. *month = 0;
  18. *day = 0;
  19. }
  20. int32_t HPP272::read_errorcode() {
  21. int16_t val = 0;
  22. bool suc = m_modbusBlockHost->readReg03(m_id, 0x0200, (uint16_t*)&val, 100);
  23. if (!suc) {
  24. return -1;
  25. }
  26. // 0 = Status OK.
  27. // 1 = Critical error,maintenance needed.
  28. // 2 = Error, device may recover automatically.
  29. // 4 = Warning.
  30. // 8 = Notification.
  31. // 16 = Calibration enabled
  32. return val;
  33. }
  34. bool HPP272::read_reg(int32_t add, uint16_t* val, size_t len) { return m_modbusBlockHost->readReg03Muti(m_id, add, val, len, 100); }
  35. bool HPP272::read_sensor_data(hpp272_data_t* sensordata) { //
  36. return m_modbusBlockHost->readReg03Muti(m_id, 0x0100, (uint16_t*)sensordata, sizeof(*sensordata) / 2, 100);
  37. }
  38. void HPP272::updateSensorDataAndErrorcode() {
  39. if (!read_sensor_data(&m_readbuf)) {
  40. return;
  41. }
  42. int32_t errorcode = read_errorcode();
  43. {
  44. zlock_guard lck(m_cache_lock);
  45. m_cachedata = m_readbuf;
  46. m_cache_errorcode = errorcode;
  47. }
  48. }
  49. int32_t HPP272::read_cache_errorcode() {
  50. zlock_guard lck(m_cache_lock);
  51. return m_cache_errorcode;
  52. }
  53. bool HPP272::read_cache_sensor_data(hpp272_data_t* sensordata) {
  54. zlock_guard lck(m_cache_lock);
  55. *sensordata = m_cachedata;
  56. return true;
  57. }