阅读:1631回复:0
读取设备文件event总是读不了全部数据
我使用如下代码读取自定义的USB hid设备文件 /dev/input/event5,可是为什么每次读的长度都在变.
usb设备每次固定上传12byte数据. #include <fcntl.h> #include <errno.h> #include <aio.h> #include <stdio.h> #include <string.h> char buf[64]; ssize_t retval; ssize_t nbytes; struct aiocb myaiocb; int main(void) { static int count,totalcount; int fd; fd = open( "/dev/input/event5", O_RDONLY); while(1) { bzero( &myaiocb, sizeof (struct aiocb)); myaiocb.aio_fildes = fd; myaiocb.aio_offset = 0; myaiocb.aio_buf = (void *) buf; myaiocb.aio_nbytes = sizeof (buf); myaiocb.aio_sigevent.sigev_notify = SIGEV_NONE; retval = aio_read( &myaiocb ); if (retval) perror("aio_read:"); while ( (retval = aio_error( &myaiocb) ) == EINPROGRESS) ; /* free the aiocb */ nbytes = aio_return(&myaiocb); printf("nBytes = %d \n",nbytes); } close(fd); } |
|