头文件 head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define GET_HUM _IOR('m', 1, int)
#define GET_TEM _IOR('m', 0, int)
#endif
应用程序 si7006.c
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include "head.h"
int main(int argc, char const *argv[])
{
char buf[128]={0};
unsigned short hum,tem;
float hum1,tem1;
int fd = open("/dev/si7006",O_RDWR);
if(fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while(1)
{
//读取温湿度数据
ioctl(fd,GET_HUM,&hum);
ioctl(fd,GET_TEM,&tem);
//进行字节序转换
hum = ntohs(hum);
tem = ntohs(tem);
//数据转换
hum1 = (float)hum*125/65536-6;
tem1 = (float)tem*175.72/65536-