Carles
Carles

Reputation: 129

c++ Inappropriate ioctl for device

I'm using a USB device usually connected on /dev/ttyUSB0

Sometimes when there's more USB devices it goes to /dev/ttyUSB1 or others

I've added a rule under /etc/udev/rules.d/myrule.rules with the following line:

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.

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". If I use the /dev/ttyUSB0, which is also available everything works well.

Do I have to modify my C++ code to deal with SYMLINKS ?

Thanks in advance,

Carles.

Upvotes: 2

Views: 4137

Answers (1)

Jens Munk
Jens Munk

Reputation: 4725

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

#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;
}

Upvotes: 0

Related Questions