Reputation: 1323
Say I open 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;
Now I add them to 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.
}
}
Now how can i check the device when it is available now. Say if device is not available when i opened it. How can i monitor it to check when it has been plugged?. I don't want to use udev/libudev.h.
Thanks,
Upvotes: 3
Views: 88
Reputation: 11277
Common way is to try writing to device some data and reading response back. If response is expected then it means device is connected, otherwise it is not.
For example when scanning COM port on which modem is connected one should write "ATE0\r"
to COM port and get back modem response "OK"
. This means modem is connected to that COM port.
Same idea applies to USB devices. Only protocol is more complex and request/response data may be more complex from device to device.
Upvotes: 1