LINUX/树莓派读取USB扫码枪/二维码扫描器输出
本文地址:http://tongxinmao.com/Article/Detail/id/223
USB扫码枪一般都是模拟成HID输入设备,当键盘处理即可 #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <linux/input.h> struct input_event buff; int fd; int read_nu; int main(int argc, char *argv[]) { fd = open("/dev/input/event0", O_RDONLY); if (fd < 0) { perror("can not open device usbkeyboard!"); exit(1); } int i = 0; while (1) { while (read(fd, &buff, sizeof(struct input_event)) == 0) { ; } if (buff.type == EV_KEY) if (buff.value == 0) printf("type:%d code:%d value:%d\n", buff.type, buff.code, buff.value); //#if 0 //i++; //if(i > 12) //{ //break; //} //#endif } close(fd); return 1; }
LINUX下模拟键盘输入也是往/dev/input/eventx设备写入即可:
int key(int fd,int value)
{
struct input_event event;
event.type=EV_KEY;
event.code=KEY_A; //模拟输入'A'
event.value=value;
gettimeofday(&event.time,0);
if(write(fd,&event,sizeof(event))<0)
{
printf("simulate key error! \n");
return -1;
}
return 0;
}
USB键盘码对照表
http://www.win.tue.nl/~aeb/linux/kbd/scancodes-10.html#ss10.6
上一篇:MT6261D USB 描述符
下一篇:RDA CSDTK SDK RDA8851