#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/i2c.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <errno.h>
#include <string.h>
//i2c_msg MSG[2];
int main(int argc, char** argv)
{
i2c_rdwr_ioctl_data i2cdata;
i2c_msg* addr_MSG = new i2c_msg[2];
unsigned char Buf_send[2] = { 0x40, 0x50 };
unsigned char Buf_Re[2] = { '\0', '\0' };
int fd = open("/dev/i2c-0", O_RDWR);
if (fd < 0)
{
printf("open error!\n");
}
ioctl(fd, I2C_TIMEOUT, 1);
ioctl(fd, I2C_RETRIES, 2);
//i2Cdata填充数据
i2cdata.nmsgs = 2;
i2cdata.msgs = addr_MSG;
//MSG数据填充 //write
addr_MSG[0].addr = 0x50;
addr_MSG[0].len = 2;
addr_MSG[0].flags = 0;
addr_MSG[0].buf = Buf_send;
//read
addr_MSG[1].addr = 0x50;
addr_MSG[1].len = 2;
addr_MSG[1].flags = I2C_M_RD;
addr_MSG[1].buf = Buf_Re;
int res = ioctl(fd, I2C_RDWR, (unsigned long)&i2cdata);
if (res < 0)
{
perror("ioctl error");
}
delete[] addr_MSG;
close(fd);
}