我将2D条形码扫描仪与Raspberry Pi 4 Model B连接,并尝试扫描一些代码。在使用evdev库时,我成功地获得了输出。但问题是,在3次连续扫描后,它给我抛出了一个异常,说"[错误16]设备或资源繁忙">。我找不到这个问题的根本原因,并尝试了许多故障排除方法,但似乎都不起作用。有人能帮我吗?这是我用过的密码。
from evdev import InputDevice, categorize, ecodes
from datetime import datetime
import calendar
scancodes = {
# Scancode: ASCIICode
0: None, 1: u'ESC', 2: u'1', 3: u'2', 4: u'3', 5: u'4', 6: u'5', 7: u'6', 8: u'7', 9: u'8',
10: u'9', 11: u'0', 12: u'-', 13: u'=', 14: u'BKSP', 15: u'TAB', 16: u'q', 17: u'w', 18: u'e', 19: u'r',
20: u't', 21: u'y', 22: u'u', 23: u'i', 24: u'o', 25: u'p', 26: u'[', 27: u']', 28: u'CRLF', 29: u'LCTRL',
30: u'a', 31: u's', 32: u'd', 33: u'f', 34: u'g', 35: u'h', 36: u'j', 37: u'k', 38: u'l', 39: u';',
40: u'"', 41: u'`', 42: u'LSHFT', 43: u'\', 44: u'z', 45: u'x', 46: u'c', 47: u'v', 48: u'b', 49: u'n',
50: u'm', 51: u',', 52: u'.', 53: u'/', 54: u'RSHFT', 56: u'LALT', 57: u' ', 100: u'RALT'
}
capscodes = {
0: None, 1: u'ESC', 2: u'!', 3: u'@', 4: u'#', 5: u'$', 6: u'%', 7: u'^', 8: u'&', 9: u'*',
10: u'(', 11: u')', 12: u'_', 13: u'+', 14: u'BKSP', 15: u'TAB', 16: u'Q', 17: u'W', 18: u'E', 19: u'R',
20: u'T', 21: u'Y', 22: u'U', 23: u'I', 24: u'O', 25: u'P', 26: u'{', 27: u'}', 28: u'CRLF', 29: u'LCTRL',
30: u'A', 31: u'S', 32: u'D', 33: u'F', 34: u'G', 35: u'H', 36: u'J', 37: u'K', 38: u'L', 39: u':',
40: u''', 41: u'~', 42: u'LSHFT', 43: u'|', 44: u'Z', 45: u'X', 46: u'C', 47: u'V', 48: u'B', 49: u'N',
50: u'M', 51: u'<', 52: u'>', 53: u'?', 54: u'RSHFT', 56: u'LALT', 57: u' ', 100: u'RALT'
}
class scan_barcode:
def __init__(self,devicePath):
self.devicePath = devicePath
def readBarcode(self):
dev = InputDevice(self.devicePath)
dev.grab() # grab provides exclusive access to the device
x = ''
caps = False
for event in dev.read_loop():
if event.type == ecodes.EV_KEY:
data = categorize(event) # Save the event temporarily to introspect it
if data.scancode == 42:
if data.keystate == 1:
caps = True
if data.keystate == 0:
caps = False
if data.keystate == 1: # Down events only
if caps:
key_lookup = u'{}'.format(capscodes.get(data.scancode)) or u'UNKNOWN:[{}]'.format(data.scancode) # Lookup or return UNKNOWN:XX
else:
key_lookup = u'{}'.format(scancodes.get(data.scancode)) or u'UNKNOWN:[{}]'.format(data.scancode) # Lookup or return UNKNOWN:XX
if (data.scancode != 42) and (data.scancode != 28):
x += key_lookup
if(data.scancode == 28):
return(x)
scanned_data = scan_barcode('/dev/input/event0')
def scanner_function():
try:
value = scanned_data.readBarcode()
print(f"Scanned value:{str(value)}")
except Exception as e:
print(e)
pass
while True:
scanner_function()
即使当我通过异常时,它也不允许我转到其他任务。整个过程到此为止。
这是输出:
Scanned value: 4568hidhXGu
Scanned value: 1238fujXjje75
Scanned value: 789665
[Error 16] Device or resource busy
[Error 16] Device or resource busy
[Error 16] Device or resource busy
[Error 16] Device or resource busy
[Error 16] Device or resource busy
我不确定问题是否与您的代码有关。我认为这更多地与你的扫描仪有关。我已经用R32二维码阅读器测试了你的脚本(https://www.sycreader.com/en/3650/)这是完美的工作。您使用的扫描仪类型是什么?
结果:
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:11.035355
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:11.675270
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:14.563287
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:15.007284
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:15.799299
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:20.959301
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:21.591286
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:24.515289
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:26.331292
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:31.323339
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:32.747289
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:34.495291
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:36.367294
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:37.903286
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:39.507295
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:41.099288
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:42.575295
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:44.123283
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:45.579286
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:47.055336
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:48.671301
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:49.983288
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:52.779284
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:54.755299
Scanned value:Toiletbezoek DateTime:2022-12-03 09:49:56.159286