简体   繁体   English

C ++不适用于设备的ioctl

[英]c++ Inappropriate ioctl for device

I'm using a USB device usually connected on /dev/ttyUSB0 我正在使用通常连接在/ dev / ttyUSB0上的USB设备

Sometimes when there's more USB devices it goes to /dev/ttyUSB1 or others 有时,当有更多的USB设备时,它会转到/ dev / ttyUSB1或其他设备

I've added a rule under /etc/udev/rules.d/myrule.rules with the following line: 我在/etc/udev/rules.d/myrule.rules下添加了以下行:

SUBSYSTEM=="usb", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyyy", MODE="0666", SYMLINK="MyUSB" SUBSYSTEM ==“ usb”,ATTRS {idVendor} ==“ xxxx”,ATTRS {idProduct} ==“ yyyy”,MODE =“ 0666”,SYMLINK =“ MyUSB”

That works fine, when I plug my USB device I get the /dev/MyUSB file ready. 这很好,当我插入USB设备时,我就准备好了/ dev / MyUSB文件。

The problem is that when I try to access to this file using my C++ program it doesn't work sending a message: "Inappropriate ioctl for device". 问题是,当我尝试使用C ++程序访问该文件时,它无法发送消息:“设备的ioctl不适当”。 If I use the /dev/ttyUSB0, which is also available everything works well. 如果我也使用/ dev / ttyUSB0,那么一切正常。

Do I have to modify my C++ code to deal with SYMLINKS ? 我是否需要修改C ++代码以处理SYMLINKS?

Thanks in advance, 提前致谢,

Carles. 卡尔斯。

This seems to work. 这似乎有效。 I tested it using a flash drive 我使用闪存驱动器进行了测试

Add the rule in /etc/udev/rules.d/myrule.rules Reload the rules using sudo udevadm control --reload-rules In the program 在/etc/udev/rules.d/myrule.rules中添加规则使用sudo udevadm控件重新加载规则--reload-rules在程序中

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>

int main() {    
  struct stat buf;
  int fd = open("/dev/MyUSB", O_RDWR  | O_CREAT);
  int k = fstat(fd, &buf);

  // The device handle is contained in st_rdev
  dev_t dt = buf.st_rdev;
  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM