serial_TG39BX1.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # -*- coding: UTF-8 -*-
  2. import serial
  3. from ssat_sdk.utils.AbnormalClient import abnormal_client
  4. class Serial_TG39BX1():
  5. def __init__(self, port):
  6. self.ser = serial.Serial()
  7. self.ser.port = port
  8. self.ser.baudrate=9600
  9. self.ser.bytesize=8
  10. self.ser.parity=serial.PARITY_NONE
  11. self.ser.stopbits=1 # TG39BX1_91200093076-1E.pdf stopbits=2 错误,实际上 stopbits=1
  12. def open(self):
  13. self.ser.open()
  14. if self.ser.is_open == True :
  15. print "%s : Open Success" % self.ser.port
  16. print self.ser.get_settings()
  17. print "rts=" + str(self.ser.rts)
  18. print "cts=" + str(self.ser.cts)
  19. else:
  20. print "%s : Open Fail" % self.ser.port
  21. def writeCmd(self, cmdstr):
  22. try:
  23. len = self.ser.write(cmdstr + b"\r") # 注意后面要回车键(\r),命令才会发送出去执行
  24. print "Sending cmd:" + cmdstr + " len=" + str(len)
  25. except Exception,e:
  26. abnormal_client.sendAbnormal('ATV', str(e))
  27. def readResult(self):
  28. """
  29. If the result is normal, "OK" will be output.
  30. OK [CR][LF] In the normal case
  31. If the result is an error, the following message will be output.
  32. ERROR COMMAND [CR][LF] In the case of a command error
  33. ERROR PARAMETER [CR][LF] In the case of a paremeter error
  34. ERROR [CR][LF] In the case of an error processing the command
  35. """
  36. try:
  37. result = self.ser.readline()
  38. print "Cmd result :" + result
  39. return result
  40. except Exception,e:
  41. abnormal_client.sendAbnormal('ATV', str(e))
  42. def execmd(self, cmdstr):
  43. self.writeCmd(cmdstr)
  44. result = self.readResult()
  45. return result
  46. def close(self):
  47. self.ser.close()
  48. if self.ser.is_open == False:
  49. print "%s :Close Success" % self.ser.port
  50. else :
  51. print "%s :Close Fail" % self.ser.port
  52. def __del__(self):
  53. if self.ser.is_open == True:
  54. self.ser.close()
  55. # - - - - - - - - - - - - - - - - - - - - - - - - -
  56. if __name__ == "__main__":
  57. ser = Serial_TG39BX1("COM6")
  58. ser.open()
  59. ser.execmd("VSIGCB 1")
  60. ser.execmd("VSIGCB 2")
  61. ser.execmd("VSIGCB 3")
  62. ser.close()