我已經成功地使用Python讀取USB條形碼掃描儀。我希望能夠逃脫fp.read()
如果是空的,並檢查用戶按下左LCD按鈕
def read Barcode
lcd.backlight(lcd.GREEN)
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: '?' }
backPressed = False
lcd.clear()
lcd.message("accn: \nLocation: ")
lcd.setCursor(7, 1)
print("Scan Next Accn:")
while not backPressed:
fp = open('/dev/hidraw0', 'r')
accn = ""
shift = False
done = False
print("should loop")
r, w, e = select.select([ fp ], [], [], 0)
print("Should be a line here")
if fp in r:
print("Fp is in r")
while not done:
print("looping")
buffer = os.read(fp.fileno(), 8)
for c in buffer:
if ord(c) > 0:
if int(ord(c)) == 40:
done = True
fp.flush()
fp.close()
print("Done = True")
break;
if shift:
if int(ord(c)) == 2 :
shift = True
else:
accn += hid2[ int(ord(c)) ]
shift = False
else:
if int(ord(c)) == 2 :
shift = True
else:
accn += hid[ int(ord(c)) ]
print("accn: " + accn)
fileAccn(accn)
fp.close()
backPressed = lcd.buttonPressed(lcd.LEFT)
if(backPressed):
lcd.backlight(lcd.WHITE)
return
backPressed = lcd.buttonPressed(lcd.LEFT)
if(backPressed):
return
return
由於條碼掃描器不發送EOF
使用fp.read()
不帶參數不會返回,如果它是空的。任何幫助表示讚賞。我不願意使用中斷或超時,因爲我不想在掃描條形碼的過程中結束該功能。
在此先感謝。
這個鏈接確實有幫助。我現在正在工作。我注意到讀完條形碼後文件中還有數據。關閉文件並重新打開它似乎也解決了我的問題。 – Tom 2014-10-11 19:58:45