immember
驱动牛犊
驱动牛犊
  • 注册日期2008-12-08
  • 最后登录2016-01-09
  • 粉丝0
  • 关注0
  • 积分2分
  • 威望11点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1597回复:0

读取设备文件event总是读不了全部数据

楼主#
更多 发布于:2008-12-26 19:50
我使用如下代码读取自定义的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);
}
游客

返回顶部