嵌入式linux支持条码枪

来源:互联网 发布:许雯may的淘宝店网址 编辑:程序博客网 时间:2024/04/29 06:32

最近一个项目里面需要用到USB接口条码枪设备。

市面上通用的USB条码枪都是标准USB接口,不需要驱动,USB条码枪得到的数据会向USB键盘一样处理。


通过dmesg命令查看你的linux是否支持条码枪,另外支持的话插入条码枪可以在输入设备中找到他映射的虚拟设备。

通过命令查看你linux里面的设备:cat /proc/bus/input/devices,通过里面的数据分析出你的条码枪中对应的虚拟设备。

然后通过读取/dev/input/event*里面的数据来获取条码枪扫描的数据。


linux系统通过结构体struct input_event存储输入数据,具体数值定义可以在linux头文件linux/input.h中找到

struct input_event {struct timeval time;__u16 type;//类型__u16 code;//代号__s32 value;//值};
支持条码枪的demo代码如下


/* * bacode.c * *  Created on: 2015-3-3 *      Author: hp */#include <stdlib.h>#include <string.h>#include <fcntl.h>#include <unistd.h>#include <stdio.h>#include <ctype.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <linux/input.h>#include<math.h>#define KBD_DEVICE "/dev/input/event"static char DeviceName[128];#define DEV_INFO"/proc/bus/input/devices"static int fd_reader = -1;static int usedev = 0;static int getUSBDevice(char *buf){printf("buf:\n%s\n", buf);char *p = NULL;if (NULL != (p = strstr(buf, "pxa3xx_keypad"))) {if (NULL != (p = strstr(p, "event"))) {printf("pxa3xx_keypad:%s\n", p);usedev = atoi(p+5);}return -1;}if (NULL == (p = strstr(buf, "Handlers="))) {return -1;}if (NULL != (p = strstr(p, "event"))) {printf("event:%s\n", p);return atoi(p+5);}return -1;}static int readOneDevice(int fd){char ch = 0;char tmp = 0;int cnt = 0;char buf[1024];int ret = 0;while (1 == read(fd, &ch, 1)) {if (ch==tmp && ch == '\n') {if (-1 != (ret = getUSBDevice(buf))) {return ret;} else {memset(buf, 0, sizeof(buf));cnt = 0;tmp = 0;continue;}}buf[cnt++] = ch;tmp = ch;if (cnt >= 1023) {break;}}return -1;}static int InitBarCodeDevice(void){int fd = open(DEV_INFO, O_RDONLY | O_NONBLOCK);int num = 0;if (fd >= 0) {num = readOneDevice(fd);}if (num < 0) {num = usedev ? 0 : 1;}printf("used max dev:%d num:%d\n", usedev, num);snprintf(DeviceName, sizeof(DeviceName), "%s%d", KBD_DEVICE, num);printf("DeviceName:%s\n", DeviceName);}static int fd_Reader = -1;static unsigned char LowBarChar[] = {'\0', '\0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', //0-13'\0', '\0', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', //14-27'\0', '\0', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`',//28-41'\0', '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/'};static unsigned char UpperBarChar[] = {'\0', '\0', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', //0-13'\0', '\0', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', //14-27'\0', '\0', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',//28-41'\0', '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?'};static int OpenBarCodeReader(void){if (fd_Reader >= 0) {close(fd_Reader);}fd_Reader = open(DeviceName, O_RDONLY | O_NONBLOCK);return fd_Reader;}static int CloseBarCodeReader(void){if (fd_Reader >= 0){close(fd_Reader);fd_Reader=-1;}return 0;}static void msleep(int msec){struct timeval delay;delay.tv_sec =msec /1000;delay.tv_usec =(msec%1000)*1000;select(0, NULL, NULL, NULL, &delay);}int ReaderIsReady(void){fd_set fds;struct timeval timeout;timeout.tv_sec=0;timeout.tv_usec=3000;if (fd_Reader < 0 && OpenBarCodeReader() < 0) {return 0;}FD_ZERO(&fds);FD_SET(fd_Reader, &fds);if (select(fd_Reader+1, &fds, NULL, NULL, &timeout) < 0) {return 0;}return FD_ISSET(fd_Reader, &fds);}int tmpGetUCode(void){struct input_event ev[64];int read_bytes;int tmpCode = 0;int barcodecount = 0;unsigned char barcodebuf[64];int i = 0;int shiftFlag = 0;memset(barcodebuf, 0, sizeof(barcodebuf));unsigned char *keyChar = LowBarChar;while(ReaderIsReady()) {msleep(10);memset(ev, 0, sizeof(struct input_event) * 64);read_bytes = read(fd_Reader, ev, sizeof(struct input_event)* 64);if (read_bytes < 0) {CloseBarCodeReader();}if (read_bytes >= sizeof(struct input_event) && 0 == (read_bytes % sizeof(struct input_event))) {for (i = 0; i < (read_bytes / sizeof(struct input_event)); i++) {if (KEY_LEFTSHIFT == ev[i].code && EV_KEY == ev[i].type) {keyChar = UpperBarChar;} else {keyChar = LowBarChar;}if (EV_KEY == ev[i].type && ev[i].value) {barcodebuf[barcodecount++] = keyChar[ev[i].code];}}}msleep(30);}if (barcodebuf[0])printf("barcode:%s\n",barcodebuf);}int main(void){printf("Scanning Barcode Begining\n");InitBarCodeDevice();while (1){tmpGetUCode();msleep(20);}}


0 0