import binascii import re import time import serial from time import sleep import threading class SerialPort(object): def __init__(self,Name,BaudRate,timeout=0.1): # 串口通信同步锁 self.lockSend=threading.Lock() self.ser = None self.encoding = '' # 编码格式 try: self.ser = serial.Serial(Name, BaudRate, timeout=timeout,) except Exception as e: print('serialserialserial',str(e)) # 发送的指令'00 01 00 00 23 ...' def send_cmd_hex(self,cmd,readCount=0): try: self.lockSend.acquire() data=None cmd = bytes.fromhex(cmd) # 将发送的 retry_times = 0 # 默认读取一次 self.ser.flushInput() self.ser.write(cmd) time.sleep(0.2) while retry_times ==0: count = self.ser.inWaiting() #获取接收缓存区的字节数 retry_times += 1 if(readCount==0): data = self.ser.readall() if data == None: continue else: break else: if(readCount==1): data = self.ser.read(count) else: data = self.ser.read(readCount) if data == None: continue else: break #sleep(0.002) # self.ser.flushInput() #print('ggggggggggg::',data) # b'\x01\x03\x04\x00\x00\x00\x00\xfa3' return data except Exception as e: raise e except OSError as e: raise e finally: self.lockSend.release() # 发送给的指令 #~000~qst~start~@,对字符串进行转换成byte类型再发送传给 def send_cmd(self,cmd,readCount=0): try: self.lockSend.acquire() data=None if self.encoding: cmd = cmd.encode(self.encoding) else: cmd = cmd.encode() retry_times = 0 # 默认读取一次 self.ser.flushInput() self.ser.write(cmd) time.sleep(0.2) while retry_times ==0: count = self.ser.inWaiting() #获取接收缓存区的字节数 retry_times += 1 if(readCount==0): data = self.ser.readall() if self.encoding: data = data.decode(self.encoding) else: data = data.decode() if data == None: continue else: break else: if(readCount==1): data = self.ser.read(count) else: data = self.ser.read(readCount) if data == None: continue else: break #sleep(0.002) # self.ser.flushInput() print('ggggggggggg::',data) return data.replace("\r\n", '') except Exception as e: raise e except OSError as e: raise e finally: self.lockSend.release() def Read(self,readCount=0): try: self.lockSend.acquire() data=None while True: count = self.ser.inWaiting() #获取接收缓存区的字节数 if(readCount==0): data = self.ser.readall() if data == None: continue else: break else: if(readCount==1): data = self.ser.read(count) else: data = self.ser.read(readCount) if data == None: continue else: break #sleep(0.002) # self.ser.flushInput() return data except Exception as e: raise e except OSError as e: raise e finally: self.lockSend.release() def ReadAll(self,readCount=0): try: self.lockSend.acquire() data=None while True: #count = self.ser.inWaiting() #获取接收缓存区的字节数 if(readCount==0): data = self.ser.readall() if data == None: continue else: break else: data = self.ser.read(readCount) if data == None: continue else: break #sleep(0.002) #self.ser.flushInput() return data except Exception as e: raise e except OSError as e: raise e finally: self.lockSend.release() def FlushInput(self): #清空缓存 try: self.lockSend.acquire() self.ser.flushInput() except Exception as e: raise e except OSError as e: raise e finally: self.lockSend.release() def Write(self,data): try: self.lockSend.acquire() self.ser.flushInput() self.ser.write(data) except Exception as e: raise e except OSError as e: raise e finally: self.lockSend.release() def Close(self): try: self.ser.close() except Exception as e: print('serialClose:',e) if __name__ == "__main__": # sp = SerialPort.SerialPort('/dev/ttyS0',115200) # while True: # #cmd=input('输入发送:') # sp.Write('#~000~qst~lock0~@\r\n'.encode()) # #print("发送:"+cmd) # d = sp.Read() # print("接收:"+bytes.decode(d)) # import SerialPort ser = SerialPort('com1', 9600) # ser = SerialPort('/dev/ttyUSB0', 9600) print('rrrrrrrrrrrrrrrrrrrrrrrrrrrr:::::',ser) cmd = "01 03 12 07 00 14 F1 7C" data = ser.send_cmd_hex(cmd) print('ssssssssssssssssssssssssssssssssssssssss:::',data)