首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过打开和读取来检测可用的设备

通过打开和读取来检测可用的设备
EN

Stack Overflow用户
提问于 2012-11-09 02:15:21
回答 1查看 71关注 0票数 3

假设我打开了Devices。

代码语言:javascript
运行
复制
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;

代码语言:javascript
运行
复制
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。

谢谢,

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-09 02:35:33

常见的方法是尝试向设备写入一些数据,然后读回响应。如果期望响应,则表示设备已连接,否则未连接。

例如,当扫描连接了调制解调器的COM端口时,应该向COM端口写入"ATE0\r",并返回调制解调器响应"OK"。这意味着调制解调器已连接到该COM端口。

同样的想法也适用于USB设备。只有协议更复杂,并且请求/响应数据可能因设备而更复杂。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13295189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档