c - 没有这样的设备或地址是什么意思(错误代码6)
问题描述
我正在尝试使用 c 代码打开一个串行端口,并且我使用以下命令创建了一个节点;
mknod /tmp/ttyACM0 c 100 0;
chmod 700 /tmp/ttyACM0;
然后用下面的方法运行一个打开串口的可执行文件;
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;
}
它返回;
Error opening serial port /tmp/ttyACM0 - No such device or address(6).
/tmp 目录下实际上有一个 ttyACM0 文件,但它返回了错误消息。我能做些什么来传递这个错误?
编辑:当我查看 /proc/devices 文件时,没有 ttyACM0 设备。现在我认为我的问题的原因可能是这个。
解决方案
设备节点并不意味着该设备实际存在。当您打开它时,内核会尝试查找匹配的设备,如果它不存在,则会出现上述错误。
过去十年中的任何 Linux 系统都将通过 udevd 自动为现有设备创建设备节点。您必须手动创建它强烈表明该设备根本不存在,就像错误所说的那样。