1. 封装的函数
用到了内核中的hwmon子系统,
hwmon子系统作为Linux内核中的一个子系统,用于监控硬件传感器的状态(设备的温度、电压和风扇转速)和提供对硬件传感器的访问接口。
在应用层,对传感器信息的读取,本质上是对驱动中hwmon子系统在注册传感器设备时所创建的各个属性文件进行读取操作。
(1)HANDLE TempSensorOpen(char *pSensorName) 打开设备文件:例如 nTmpHandle = TempSensorOpen("/sys/class/hwmon/hwmon0/temp1_input"); (2)float TempSensorRead(HANDLE nHd) 获取温度值
2. 函数源码注释
2.1 TempSensorOpen
/********************************** *function: TempSensorOpen *input : * pSensorName : Full path of sensor base hwmon system *return : * success : return HANDLE,else return INVALID_HANDLE; */ HANDLE TempSensorOpen(char *pSensorName) { int nFd = 0; /* check file param */ if(pSensorName == NULL) { LOG_ERROR("get null file path."); return INVALID_HANDLE; } /* check file existed */ if(access(pSensorName,F_OK) != 0) { LOG_ERROR("Sensor file %s not found.",pSensorName); return INVALID_HANDLE; } /* Open */ nFd = open(pSensorName,O_RDONLY); if(nFd <= 0) { LOG_ERROR("Open Sensor file %s error with %s.",pSensorName,strerror(errno)); return INVALID_HANDLE; } return (HANDLE)nFd; }
2.2 TempSensorRead
/********************************** *function: TempSensorRead *input : * nHd : Sensor handle from TempSensorOpen *return : * success : return temperatur value,else return -1000.0 */ float TempSensorRead(HANDLE nHd) { int nFd = (int)nHd; char nTmp[32] = {0}; /* Seek to file start pos */ lseek(nFd,0,SEEK_SET); /* read value */ if(read(nFd,nTmp,sizeof(nTmp)) <= 0) { LOG_ERROR("read value error with %s.",strerror(errno)); return INVALID_TEMP_VAL; } return (float)(strtol(nTmp,NULL,0)) / 1000.0; }
标签:nFd,HANDLE,temperature,TempSensorOpen,--,pSensorName,file,return,库函数 From: https://www.cnblogs.com/dkhlaojogo/p/18202854