【问题标题】:How to read barcodes in python?如何在python中读取条形码?
【发布时间】:2019-06-11 16:20:42
【问题描述】:

对于学校作业,我正在制作“智能冰箱”,要在冰箱中注册产品,我正在使用 MCR12-USB 条形码扫描仪:https://www.adafruit.com/product/1203。 连接到树莓派模型 B3+

我想将条形码放入一个变量中以使用 UPC api 进行查找。 任何获取条形码的方法都是好的。

我尝试使用输入字段获取条形码(条形码设置为键盘;hidraw0)。 但是这种方法的问题是光标不会自动在输入字段中。

现在我正在尝试通过串行接口(条形码设置为 RS232)从阅读器获取条形码,但我无法使其正常工作,我真的不知道要打开哪个端口,我已经尝试了 serial0、serial1、ttyAMA0 和 ttyS0,但在所有这些上我只得到一个空行。

import requests
import serial

def barcode():
    with serial.Serial('/dev/serial0', 9600, timeout=1) as port:
        while True:

            print(port.name)
            line = port.readline()
            print(line)
            url = "https://api.upcdatabase.org/product/%s/%s" % (line, api_key)

            headers = {
                'cache-control': "no-cache",
            }

            response = requests.request("GET", url, headers=headers)

            print("-----" * 5)
            print(line)
            print(json.dumps(response.json(), indent=2))
            print("-----" * 5 + "\n")

我希望输出是条形码,但我只得到一个空行

【问题讨论】:

  • 当您对 url 执行显式 REST GET 时会发生什么?
  • 我收到一个 json 响应
  • 尝试在 GET 调用之前打印url。在 GET 调用之后,只需打印 response 另外,您是否看到任何异常?
  • 产品文档说设备提供条形码作为键盘输入:When a barcode is scanned, the raw data is decoded, parity-checked and spit out as if they were typed on a keyboard.您是否尝试过不使用串行和读取条形码的方式与读取键盘输入的方式相同?只是在黑暗中拍摄,但通常答案隐藏在文档中。
  • @BrandonTweed 当我这样做时出现错误:serial.serialutil.SerialException: 无法配置端口:(22, 'Invalid argument')

标签: python barcode-scanner


【解决方案1】:

默认情况下,Rpi 将您的设备视为键盘。如果要将 USB 用作串行端口,则必须更改配置。

按照以下步骤操作:

-在新终端上写:xinput list 并找到条形码阅读器的 ID(数字,id=3 或 10,您应该按名称找到它)

-然后写:xinput test xx 其中xx是你上一步得到的数字

-现在扫描手册3页面顶部的条形码,直到您看到终端上出现02501

-下一步扫描12页面中间的条码,直到看到000603出现在终端上

现在断开并重新连接您的阅读器,当您执行ls /dev/tty* 时,您应该会看到/dev/ttyUSB0。这意味着您的设备已准备好用作虚拟串行端口,并且您的代码现在应该可以工作了。

【讨论】:

  • 当我输入 xinput list 它返回“无法连接到 X 服务器
  • 输入export DISPLAY=:0.0
  • 仍然返回相同的
  • 你在什么 Linux 上?
  • 最新版raspbian
猜你喜欢
  • 2012-01-27
  • 2011-06-16
  • 2013-06-24
  • 2012-02-24
  • 2022-01-11
  • 2013-07-20
  • 1970-01-01
  • 1970-01-01
  • 2020-10-16
相关资源
最近更新 更多