python--serial

有此类,去用python解决各种串口问题吧:


[python] view plain copy
  1. from Queue import Queue  
  2. import random  
  3. import time  
  4. import  thread  
  5.   
  6. import struct    
  7. import serial   
  8.   
  9. # Producer thread  
  10. class ComDev:  
  11.     def __init__(self):  
  12.         self.sharedata = Queue()  
  13.         self.dt = 1  
  14.         self.nStep = 0  
  15.         self.com = None  
  16.           
  17.     def Open(self,com):  
  18.         try:  
  19.             self.com = serial.Serial(com,baudrate=4800, bytesize=8,parity='N',  
  20.             stopbits=1,xonxoff=0, timeout=1)  
  21.         except:  
  22.             self.com = None  
  23.             print 'Open %s fail!' %com  
  24.           
  25.     def Close(self):  
  26.         if type(self.com) != type(None):  
  27.             self.com.close()  
  28.             self.com = None  
  29.             return True  
  30.         return False  
  31.       
  32.     def ReadData(self,CmdID,SubCmdID,RevBytes):  
  33.         if type(self.com) != type(None):  
  34.             try:  
  35.                 self.com.write(struct.pack('B', CmdID))  
  36.                 tmp = self.com.read(1)  
  37.                 self.com.write(struct.pack('B',SubCmdID))  
  38.                 data = self.com.read(RevBytes)  
  39.                 return data  
  40.             except:  
  41.                 print 'ReadData fail!'  
  42.                 self.Close()  
  43.                 return None  
  44.         return None  
  45.               
  46.     def SendData(self,CmdID,SubCmdID,Data,SendBytes):  
  47.         if type(self.com) != type(None):  
  48.             try:  
  49.                 self.com.write(struct.pack('B', CmdID))  
  50.                   
  51.                 tmp = self.com.read(1)  
  52.                   
  53.                 print tmp  
  54.                 self.com.write(struct.pack('B',SubCmdID))  
  55.                   
  56.                 self.com.write(struct.pack('B',Data))  
  57.                 print 1  
  58.                 return True  
  59.             except:  
  60.                 print 'SendData fail!'  
  61.                 self.Close()  
  62.                 return False  
  63.         return False  
  64.       
  65.     def Transform(self,inData):  
  66.         global outData  
  67.         try:  
  68.             tmpdata= inData[::-1]  
  69.             outData = struct.unpack('ffff',tmpdata)  
  70.             return outData  
  71.         except:  
  72.             print 'Transform fail!'  
  73.             self.Close()  
  74.             return None  
  75.   
  76.     def DevInit(self):  
  77.         step1 = self.ReadData(0x0A,0x0B,26)  
  78.         step2 = self.ReadData(0x0A,0x0B,26)  
  79.         step3 = self.ReadData(0x0A,0x0D,16)  
  80.         step4 = self.ReadData(0x0A,0x0B,26)  
  81.         step5 = self.ReadData(0x09,0x0B,26)  
  82.         step6 = self.ReadData(0x0A,0x09,60)  
  83.         step7 = self.ReadData(0x09,0x09,60)  
  84.         return True        
  85.           
  86.     def SetDevParam(self,param):  
  87.         self.SendData(0X09,0x09,Param)  
  88.           
  89.     def Begin(self,dt_ms):  
  90.         self.keepGoing = self.running = True  
  91.         self.nStep = 0  
  92.         self.dt = dt_ms/1000.0  
  93.         thread.start_new_thread(self.Run, ())  
  94.   
  95.     def Stop(self):  
  96.         self.keepGoing = False  
  97.   
  98.     def IsRunning(self):  
  99.         return self.running  
  100.       
  101.     def IsOpen(self):  
  102.             return type(self.com) != type(None)  
  103.   
  104.     def Run(self):  
  105.         while self.keepGoing:  
  106.             #data=(random.randint(0,1000),random.randint(0,300))  
  107.             self.ReadData(0X0A,0x07,1)  
  108.             tmpdata = self.ReadData(0X0A,0x0F,16)  
  109.             data = self.Transform(tmpdata)  
  110.             self.sharedata.put(data)  
  111.             time.sleep(self.dt)  
  112.         self.running = False  
  113.       
  114.     def GetData(self):  
  115.         nsize = self.sharedata.qsize()  
  116.         if nsize > 0:  
  117.             return self.sharedata.get()  
  118.         else:  
  119.             return None 

你可能感兴趣的:(PYTHON)