我需要从 Linux C++ 程序管理 SMBus 设备。
我可以打开设备 (/dev/i2c-0),但我发现所有 i2c_smbus* 函数(i2c_smbus_write_word_data、i2c_smbus_read_word_data 等)都没有在任何 i2c header 中声明。
i2c 文档说这些函数在 linux/i2c-dev.h 头文件中声明,但在我的 centos7 中不存在。
我在所有标题中寻找它们,但没有在任何文件中定义。
知道为什么没有在那里定义以及它们在哪里吗?
这是我的代码:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <memory.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/io.h>
#include <sys/ioctl.h>
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
int Address = 0x39; //Slave addr
char i2c_dev_node_path[] = "/dev/i2c-0";
int ret_val = 0;
/* Open the device node for the I2C adapter of bus 0 */
i2c_dev_node = open(i2c_dev_node_path, O_RDWR);
if (i2c_dev_node < 0)
{
cout << "EROR Unable to open device node." << endl;
return -1;
}
int i2c_dev_address = 0x39;
ret_val = ioctl(i2c_dev_node,I2C_SLAVE,i2c_dev_address);
if (ret_val < 0)
{
perror("Could not set I2C_SLAVE.");
cout << "EROR Could not set I2C_SLAVE." << endl;
return -1;
}
cout << "ALL OK!!!!!" << endl;
int retVal = i2c_smbus_read_word_data ( i2c_dev_node , 0xac );
}
最佳答案
将i2c-smbus.ko复制到/lib/modules/4.8.28-release/kernel/drivers/i2c/i2c-smbus.ko
yum install libi2c-dev
关于c++ - i2c smbus* 函数在 centos 7 i2c-dev.h 上不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33921195/