我正在监视新插入的设备。我正在使用 libudev.h 来做我想做的事。
while(1){
ret = select(fd+1, &fds, NULL, NULL, NULL);
if(ret<0){
perror("select() failed. Exitting...");
exit(EXIT_FAILURE);
}
if (FD_ISSET(fd, &fds)) {
dev = udev_monitor_receive_device(mon);
if (dev) {
printf("New device attached\n");
printf(" Node: %s\n", udev_device_get_devnode(dev));
printf(" Action: %s\n", udev_device_get_action(dev));
udev_device_unref(dev);
if(strcmp("/dev/ttyUSB0", udev_device_get_devnode(dev))==0){
fd1 = open("/dev/ttyUSB0");
if (fd<0) exit(0);
FD_SET(fd1, &fds);
select(fd1+1, &fds, NULL, NULL, NULL);
if (FD_ISSET(fd1))
//Read from the serial device and echo back to serial device when data is received
}
}
}
else {
printf("No Device from receive_device(). An error occured.\n");
}
}
现在例如一个设备被插入并且节点是 /dev/ttyUSB0 我需要打开它并监视它的文件描述符。坐下来等待数据可用。
fd1 = open("/dev/ttyUSB0");
if (fd<0) exit(0);
FD_SET(fd1, &fds);
select(fd1+1, &fds, NULL, NULL, NULL);
if (FD_ISSET(fd1)){
//Read from the serial device and echo back to serial device when data is received
}
在监控 /dev/ttyUSB0 的 fd 时,第一个选择必须继续对设备进行监控。我应该如何同时进行 2 个选择?.. 谢谢