Using open and i2c in C++
I've realized that open()
and ioctl()
does not work inside a cpp object. I am able to do the operation if it is called inside my main()
function, but NOT when inside any of my classes. I have a object that is running in my main loop that has another object that makes the file system calls.
So basically when in the main loop it can open (I get a 3 for the pointer and the ioctl
is succes开发者_StackOverflowsful). But when I do it in object it returns 0 for open (which isn't supposedly an error) and the ioctl fails.
I know I can't use the ios::
iostream options because they don't work with ioctl
. How can I make regular ioctl work inside a cpp object?
int add=0x4b;
int i2c_bus;
if(( i2c_bus = open( "/dev/i2c-0", O_RDWR )) < 0 )
{
printf("Unable to open file /dev/i2c-0.\n");
}
if( ioctl( i2c_bus, I2C_SLAVE, add ) < 0 )
{
printf("Open chip %d FAILED file %d\n",add, i2c_bus);
return -1;
}
else
{
printf("Open chip %d Succeeded file %d\n\n",add, i2c_bus);
return 1;
}
You've assigned the result of open
to i2c_bus
, but you're using fd
in the ioctl. Did you change the variable names when you moved from main
?
精彩评论