在 C++ 中使用 open 和 i2c
我意识到 open()
和 ioctl()
在 cpp 对象内不起作用。如果在我的 main() 函数中调用它,我就可以执行该操作,但在我的任何类中都不能执行该操作。我有一个在主循环中运行的对象,该对象还有另一个进行文件系统调用的对象。
所以基本上在主循环中它可以打开(我得到一个 3 指针并且 ioctl 成功)。但是当我在对象中执行此操作时,它会返回 0 表示打开(这应该不是错误)并且 ioctl 失败。
我知道我无法使用 ios:: iostream 选项,因为它们不能与 ioctl 一起使用。如何使常规 ioctl 在 cpp 对象内工作?
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;
}
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 successful). 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已将
open
的结果分配给i2c_bus
,但您在 ioctl 中使用fd
。当您从main
移动时,您是否更改了变量名称?You've assigned the result of
open
toi2c_bus
, but you're usingfd
in the ioctl. Did you change the variable names when you moved frommain
?