# -*- coding: UTF-8 -*- import serial from ssat_sdk.utils.AbnormalClient import abnormal_client class Serial_TG39BX1(): def __init__(self, port): self.ser = serial.Serial() self.ser.port = port self.ser.baudrate=9600 self.ser.bytesize=8 self.ser.parity=serial.PARITY_NONE self.ser.stopbits=1 # TG39BX1_91200093076-1E.pdf stopbits=2 错误,实际上 stopbits=1 def open(self): self.ser.open() if self.ser.is_open == True : print "%s : Open Success" % self.ser.port print self.ser.get_settings() print "rts=" + str(self.ser.rts) print "cts=" + str(self.ser.cts) else: print "%s : Open Fail" % self.ser.port def writeCmd(self, cmdstr): try: len = self.ser.write(cmdstr + b"\r") # 注意后面要回车键(\r),命令才会发送出去执行 print "Sending cmd:" + cmdstr + " len=" + str(len) except Exception,e: abnormal_client.sendAbnormal('ATV', str(e)) def readResult(self): """ If the result is normal, "OK" will be output. OK [CR][LF] In the normal case If the result is an error, the following message will be output. ERROR COMMAND [CR][LF] In the case of a command error ERROR PARAMETER [CR][LF] In the case of a paremeter error ERROR [CR][LF] In the case of an error processing the command """ try: result = self.ser.readline() print "Cmd result :" + result return result except Exception,e: abnormal_client.sendAbnormal('ATV', str(e)) def execmd(self, cmdstr): self.writeCmd(cmdstr) result = self.readResult() return result def close(self): self.ser.close() if self.ser.is_open == False: print "%s :Close Success" % self.ser.port else : print "%s :Close Fail" % self.ser.port def __del__(self): if self.ser.is_open == True: self.ser.close() # - - - - - - - - - - - - - - - - - - - - - - - - - if __name__ == "__main__": ser = Serial_TG39BX1("COM6") ser.open() ser.execmd("VSIGCB 1") ser.execmd("VSIGCB 2") ser.execmd("VSIGCB 3") ser.close()