00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 from Common import Debug
00030 from PyQt4.QtCore import *
00031 from PyQt4.QtGui import *
00032
00033
00034 try:
00035 import serial
00036 isSerialAvailable = True
00037 except:
00038 isSerialAvailable = False
00039 Debug.info('PySerial not found. Serial Barcode Scanners will not be available.')
00040
00041
00042
00043
00044 class SerialBarcodeScanner(QThread):
00045 Keys = {
00046 '-': 'Minus',
00047 '+': 'Plus',
00048 '.': 'Period',
00049 '/': 'Slash',
00050 ',': 'Comma',
00051 '*': 'Asterisk',
00052 '%': 'Percent',
00053 ' ': 'Space',
00054 }
00055 def __init__(self, parent=None):
00056 QThread.__init__(self, parent)
00057
00058 def run(self):
00059 try:
00060 device = serial.Serial(0)
00061 except:
00062 Debug.info('Could not open Serial device. Serial Barcode Scanners will not be available.')
00063 return
00064
00065 while True:
00066 try:
00067 data = device.read()
00068 except:
00069 Debug.error('Could not read from serial device. Serial Barcode Scanner stopped.')
00070 return
00071
00072
00073 if not QApplication.focusWidget():
00074 continue
00075 if data:
00076 char = data[0]
00077 try:
00078 key = char.upper()
00079 key = SerialBarcodeScanner.Keys.get( key, key )
00080 key = eval('Qt.Key_%s' % key)
00081 except:
00082 Debug.warning('Could not find key for char "%s".' % char )
00083 continue
00084
00085 event = QKeyEvent( QEvent.KeyPress, key, QApplication.keyboardModifiers(), char )
00086 QApplication.postEvent( QApplication.focusWidget(), event )
00087
00088 event = QKeyEvent( QEvent.KeyRelease, key, QApplication.keyboardModifiers(), char )
00089 QApplication.postEvent( QApplication.focusWidget(), event )
00090