假设我打开了Devices。
int fd,fd1;
fd_set readfds;
int maxfd;
fd = open("/dev/ttyUSB0");
if(fd<0) printf("device not available\n");//How can i Wait here until device becomes available?.. Also when it shows device not available it will just continue on doing select.
printf("device /dev/ttyUSB0 available\n");
fd1 = open("/dev/ttyUSB1");
if(fd<0) printf("device not available\n");//How can i Wait here until device becomes available?
printf("device /dev/ttyUSB1 available\n");
maxfd = MAX(fd,fd1)+1;
现在我将它们添加到fd_set;
while(1){
FD_SET(fd,&readfds);
FD_SET(fd1,&readfds);
select(maxfd, &readfds, NULL, NULL, NULL);
if(FD_ISSET(fd,&readfds){
// Read the device. If there is nothing to read then device has been removed or something happend.
}
if(FD_ISSET(fd1,&readfds){
// Read the device. If there is nothing to read then device has been removed or something happend.
}
}
现在,当设备可用时,我如何检查它。如果我打开设备时设备不可用,请告诉我。如何对其进行监控,以检查是否已将其插入?我不想使用udev/libudev.h。
谢谢,
发布于 2012-11-09 02:35:33
常见的方法是尝试向设备写入一些数据,然后读回响应。如果期望响应,则表示设备已连接,否则未连接。
例如,当扫描连接了调制解调器的COM端口时,应该向COM端口写入"ATE0\r"
,并返回调制解调器响应"OK"
。这意味着调制解调器已连接到该COM端口。
同样的想法也适用于USB设备。只有协议更复杂,并且请求/响应数据可能因设备而更复杂。
https://stackoverflow.com/questions/13295189
复制相似问题