|
[php]
#!/usr/bin/python
#Filename:BarCoder.py
import serial,time
class BarCoder:
def __init__(self, Port = 0, Timeout = None):
self.fd = serial.Serial(port = Port, timeout = Timeout)
self.result = None
self.flag = 6 #nest deep
def getdata(self):
self.switch()
self.read()
self.checkdata()
if self.flag == -1:
self.result = 'error'
return self.result
def checkdata(self): #my powerful ALG ^_^
try:
self.result[2]
except:
self.flag -= 1
if self.flag == 0:
self.flag = -1
else:
self.getdata()
def switch(self): #open the laser,be careful your eyss!
self.fd.write(chr(2))
time.sleep(.813) #why? no idea!
self.fd.write('.ZXQYU') #rubbish code
def read(self):
self.result = self.fd.read(25)
if __name__ == '__main__':
print 'run...'
barcoder = BarCoder(Timeout = .5)
print '-' + str(barcoder.getdata()) + '-'
[/php] |
|