从条形码扫描仪读取树莓派中的文本文件 [英] Reading from barcode scanner to a text file in raspberry pi

查看:88
本文介绍了从条形码扫描仪读取树莓派中的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我想读取由条形码"读取的输入并将其保存到文本文件中.

I have a problem that I want to read the input that read by "bar code" and save it to a text file.

Raspberry pi B+ 和最新版本的 wheezy.

Raspberry pi B+ with latest version of wheezy.

条形码"扫描仪数据逻辑 q w 2100".

"bar code" scanner "data logic q w 2100".

推荐答案

我简单几步就解决了问题:

I solved problem with few steps simply:

1) 确保您的存储库已更新.

1) make sure your repo is updated.

2) 您的条码扫描器被选为 USB KEYBOARD,如 img:"1.PNG" 所示[DATALOGIC QW2100 键盘选择][1][1]:http://i.stack.imgur.com/bxkG2.png

2) your barcode scanner is selected as USB KEYBOARD as shown in img:"1.PNG" [DATALOGIC QW2100 KEYBOARD SELECTION][1][1]: http://i.stack.imgur.com/bxkG2.png

3) 在 Ubuntu 条码扫描器中捕获数据并在任何窗口中查看它可以捕获输入终端或文本文件"或任何其他东西.

3) in Ubuntu barcode scanner capture data and view it in any window can capture input " terminal or text file " or any other thing.

4) 在 raspberry pi 中的问题是条形码扫描仪捕获或读取的值被放入/dev/hidraw0"例如..当您的条形码连接到您的 raspi 时,该文件会自动创建.

4) in raspberry pi problem is that barcode scanner captured or read value is made into "/dev/hidraw0" for example.."that file is created auto when your barcode is connected to your raspi.

以下是一些简单的 Python 代码,用于仅在条码连接到 RASPI 时从该文件中捕获数据:

HERE IS SOME SIMPLE PYTHON CODE TO CAPTURE DATA FROM THAT FILE ONLY WHEN BARCODE IS CONNECTED TO RASPI:

import sys

done = False

while not done:

   hid = { 4: 'a', 5: 'b', 6: 'c', 7: 'd', 8: 'e', 9: 'f', 10: 'g', 11: 'h', 12: 'i', 13: 'j', 14: 'k', 15: 'l', 16: 'm', 17: 'n', 18: 'o', 19: 'p', 20: 'q', 21: 'r', 22: 's', 23: 't', 24: 'u', 25: 'v', 26: 'w', 27: 'x', 28: 'y', 29: 'z', 30: '1', 31: '2', 32: '3', 33: '4', 34: '5', 35: '6', 36: '7', 37: '8', 38: '9', 39: '0', 44: ' ', 45: '-', 46: '=', 47: '[', 48: ']', 49: '\\', 51: ';' , 52: '\'', 53: '~', 54: ',', 55: '.', 56: '/'  }

   hid2 = { 4: 'A', 5: 'B', 6: 'C', 7: 'D', 8: 'E', 9: 'F', 10: 'G', 11: 'H', 12: 'I', 13: 'J', 14: 'K', 15: 'L', 16: 'M', 17: 'N', 18: 'O', 19: 'P', 20: 'Q', 21: 'R', 22: 'S', 23: 'T', 24: 'U', 25: 'V', 26: 'W', 27: 'X', 28: 'Y', 29: 'Z', 30: '!', 31: '@', 32: '#', 33: '$', 34: '%', 35: '^', 36: '&', 37: '*', 38: '(', 39: ')', 44: ' ', 45: '_', 46: '+', 47: '{', 48: '}', 49: '|', 51: ':' , 52: '"', 53: '~', 54: '<', 55: '>', 56: '?'  }

   fp = open('/dev/hidraw0', 'rb')


   ss = ""

   shift = False

   done = False

   while not done:

  ## Get the character from the HID
  buffer = fp.read(8)
  for c in buffer:
     if ord(c) > 0:

        ##  40 is carriage return which signifies
        ##  we are done looking for characters
        if int(ord(c)) == 40:
           done = True
           break;

        ##  If we are shifted then we have to 
        ##  use the hid2 characters.
        if shift: 

           ## If it is a '2' then it is the shift key
           if int(ord(c)) == 2 :
              shift = True

           ## if not a 2 then lookup the mapping
           else:
              ss += hid2[ int(ord(c)) ]
              shift = False

        ##  If we are not shifted then use
        ##  the hid characters

        else:

           ## If it is a '2' then it is the shift key
           if int(ord(c)) == 2 :
              shift = True

           ## if not a 2 then lookup the mapping
           else:
              ss += hid[ int(ord(c)) ]     
 print ss

 ##DONE

我添加了第一个 WHILE LOOP 使脚本继续工作,直到你用CTRL+C"杀死它.

I ADDED FIRST WHILE LOOP TO MAKE SCRIPT WORK CONTINUOUS TILL U KILL IT WITH "CTRL+C".

另一件事:图像适用于 DATALOGIC BARCODE SCANNER QW2100 LITE,因此也请仔细检查您的条码手册.

ANOTHER THING: IMAGE IS FOR DATALOGIC BARCODE CODE SCANNER QW2100 LITE, SO CHECK YOUR BARCODE MANUEL CAREFULLY TOO.

这篇关于从条形码扫描仪读取树莓派中的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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