Datalogic 8500xt 在使用 pyserial 扫描条形码时遗漏了最后一位数字

问题描述 投票:0回答:1

我正在尝试在我的 Python 程序中使用带有 pyserial 的 Datalogic 8500xt 扫描仪扫描条形码。但是,我遇到了一个问题,即扫描仪遗漏了条形码的最后一位数字。例如,如果我扫描一个有七位数字的条形码,例如“1234567”,扫描仪只会返回“123456”。当我扫描具有五位数字的条形码(例如“09876”)并且扫描仪返回“0987”时,也会出现同样的问题。

我尝试调整扫描仪的设置来解决这个问题,但没有成功。我还尝试增加扫描之间的睡眠时间,但也无济于事。我正在使用 pyserial 与扫描仪通信,我想知道我的代码中是否缺少某些东西。

这是我用来从扫描仪读取数据的代码片段:

 try:
        # device_port= serial_ports()[0]
        device_port= 'COM3'
        print("device_port : ",device_port)
        print("Connecting device")
        ser = serial.Serial(
            port = device_port,
            timeout = 1,
            baudrate=9600,
            parity=serial.PARITY_EVEN,
            stopbits=serial.STOPBITS_ONE,
            bytesize=serial.SEVENBITS,
        )
        i = True

        while i:
            # if wt == 'S14\x0D':
                ser.write('S01\x0D'.encode('utf-8'))
                print("connected")
                # ser.write('\x05'.encode('utf-8'))
                # time.sleep(1)
                barcode=None
                read_val = ser.read(size=128)
                read_val = str(read_val, 'UTF-8')  
                res = list(read_val)
                print(res)
                res = res[4:-1]
                if len(res)>0:
                    i=False
                    barcode=res
        ser.close()
        return {"barcode": barcode}
                
    except:           
            return {"message": "No Barcode Device Connected"}
python barcode-scanner pyserial serial-communication
1个回答
0
投票

我可以看到三个可能的罪魁祸首:

  • 因一个逻辑错误而关闭:尝试
    res = res[4:-1]
    ->
    res = res[4:]
  • 超时时间不够,试试
    timeout=1 -> timeout=5,  # Increase timeout to allow more time for data transfer
  • 读取缓冲区大小不足:尝试
    ser.read(size=128)
    ->
    ser.read(size=256)

同时删除裸露的

except
并用更具体的替换它,以避免在调试时隐藏错误。

© www.soinside.com 2019 - 2024. All rights reserved.