简体   繁体   中英

What is the meaning of No such device or address(Error code 6)

I am trying to open a serial port with using c code and I have created a node with the following command;

mknod /tmp/ttyACM0 c 100 0; 
chmod 700 /tmp/ttyACM0;

and then run an executable file that opens a serial port with the following method;

static int OpenSerialPort(const char *bsdPath)
{
int                fileDescriptor = -1;
struct termios    options;

fileDescriptor = open(bsdPath, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fileDescriptor == -1 || flock(fileDescriptor, LOCK_EX) == -1 )
{
    printf("Error opening serial port %s - %s(%d).\n",
           bsdPath, strerror(errno), errno);
    goto error;
}

if (fcntl(fileDescriptor, F_SETFL, 0) == -1)
{
    printf("Error clearing O_NONBLOCK %s - %s(%d).\n",
        bsdPath, strerror(errno), errno);
    goto error;
}

if (ioctl(fileDescriptor, TIOCEXCL, (char *) 0) < 0) {
  printf("Error setting TIOCEXCL %s - %s(%d).\n",
      bsdPath, strerror(errno), errno);
  goto error;
}
memset(&options,0,sizeof(options));

options.c_iflag=0;
options.c_oflag=0;
options.c_cflag=CS8|CREAD|CLOCAL;
options.c_lflag=0;
options.c_cc[VMIN]=1;
options.c_cc[VTIME]=5;
cfsetospeed(&options, B115200);
cfsetispeed(&options, B115200);

if (tcsetattr(fileDescriptor, TCSANOW, &options) == -1)
{
    printf("Error setting tty attributes %s - %s(%d).\n",
        bsdPath, strerror(errno), errno);
    goto error;
}

return fileDescriptor;
error:
if (fileDescriptor != -1)
{
    close(fileDescriptor);
}
exit(1);
return -1;
}

and it returns;

Error opening serial port /tmp/ttyACM0 - No such device or address(6).

There is actually a ttyACM0 file under /tmp directory but it returns me the error message. What can I do to pass this error?

EDIT: When I look at the /proc/devices file, there is not a ttyACM0 device. Now I think my problem's reason can be this.

A device node does not mean that the device actually exists. When you open it the kernel tries to find the matching device and if it doesn't exists you get the above error.

Any Linux system form the last decade will create device nodes for existing devices automatically through udevd. That you had to create it manually is a strong indication the device doesn't exist at all just as the error says.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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