从/ dev /输入读取 [英] Read from /dev/input

查看:141
本文介绍了从/ dev /输入读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个USB RFID读卡器可模拟键盘。
所以,当我把一个卡,它我看到一个终端窗口-i.e.字符串0684a24bc1

但我想在我的C程序来读取它。
还有当我使用没有问题: scanf函数(%S,浅黄色);

但是,当我用下面的code我得到了很多(约500个字节)无法识别数据。
为什么?
我想有非阻塞读。

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&fcntl.h GT;
#包括LT&;&errno.h中GT;
#包括LT&;&termios.h GT;INT主(INT ARGC,字符** argv的){
  INT的fd;
  焦炭BUF [256];  FD =打开(的/ dev /输入/ EVENT3,O_RDWR | O_NOCTTY | O_NDELAY);
  如果(FD == -1){
    PERROR(open_port:无法打开/ dev / ttyAMA0 - );
    返回(-1);
  }  //关闭阻塞读取,使用(FD,F_SETFL,FNDELAY)如果你想要的
  的fcntl(FD,F_SETFL,0);
  }而(1){
  N =读取(FD(无效*)的buf,255);
  如果(正℃,){
    PERROR(读取失败 - );
    返回-1;
  }否则如果(N == 0)printf的(端口\\ N无数据);
  其他{
    BUF [N] ='\\ 0';
    的printf(%I字节读:%S,N,BUF);
  }
睡眠(1);
的printf(我还是做的事情);}
  关闭(FD);
  返回0;
}


解决方案

按照 Linux的输入文档,第5节,在/ dev /输入/ eventX设备返回数据如下:


  

您可以使用阻塞和非阻塞读取,也可以选择()
  为/ dev /输入/ eventX设备和您总能获得一个整数
  上的读出输入的事件。他们的布局是:

 结构input_event {
      timeval结构的时间;
      无符号短类型;
      无符号短code;
      无符号整型值; };


  
  

时间是时间戳,它返回时该事件
  发生了。类型是例如EV_REL相对时刻,EV_KEY要
  关键preSS或释放。中定义更多类型在include / linux / input.h。


  
  

code'是事件code,例如REL_X或KEY_BACKSPACE,再次
  完整列表是在include / linux / input.h。


  
  

价值是事件进行的值。无论是相对变化
  EV_REL,对于EV_ABS绝对新值(操纵杆...),或者为0 EV_KEY
  发布,1键preSS和2自动重复。


I have an USB RFID card reader which emulates keyboard. So when i put an card to it i see the string on a terminal window -i.e. "0684a24bc1"

But i would like to read it in my C program. There is no problem when i use: scanf("%s",buff);

But when i use below code i got a lot (about 500 bytes) not recognized data. Why? I would like to have non blocking read.

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>

int main(int argc, char ** argv) {
  int fd;
  char buf[256];

  fd = open("/dev/input/event3", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1) {
    perror("open_port: Unable to open /dev/ttyAMA0 - ");
    return(-1);
  }

  // Turn off blocking for reads, use (fd, F_SETFL, FNDELAY) if you want that
  fcntl(fd, F_SETFL, 0);


  }

while(1){
  n = read(fd, (void*)buf, 255);
  if (n < 0) {
    perror("Read failed - ");
    return -1;
  } else if (n == 0) printf("No data on port\n");
  else {
    buf[n] = '\0';
    printf("%i bytes read : %s", n, buf);
  }
sleep(1);
printf("i'm still doing something");

}
  close(fd);
  return 0;
}

解决方案

According to the Linux input documentation, section 5, the /dev/input/eventX devices return data as following:

You can use blocking and nonblocking reads, also select() on the /dev/input/eventX devices, and you'll always get a whole number of input events on a read. Their layout is:

struct input_event {
      struct timeval time;
      unsigned short type;
      unsigned short code;
      unsigned int value; };

'time' is the timestamp, it returns the time at which the event happened. Type is for example EV_REL for relative moment, EV_KEY for a keypress or release. More types are defined in include/linux/input.h.

'code' is event code, for example REL_X or KEY_BACKSPACE, again a complete list is in include/linux/input.h.

'value' is the value the event carries. Either a relative change for EV_REL, absolute new value for EV_ABS (joysticks ...), or 0 for EV_KEY for release, 1 for keypress and 2 for autorepeat.

这篇关于从/ dev /输入读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆