caSerail.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # -*- coding: UTF-8 -*-
  2. import re
  3. import time
  4. from baseSerail import BaseSerial
  5. class CASerail(BaseSerial):
  6. def __init__(self):
  7. BaseSerial.__init__(self)
  8. def __del__(self):
  9. self.close()
  10. # 用于发命令前,检测串口是否打开;
  11. def checkport(self):
  12. if not self.ser.is_open:
  13. self.reOpen()
  14. return self.ser.is_open
  15. def sendcmd(self, cmd:str):
  16. self.write(cmd.encode('utf-8'))
  17. return self.read()
  18. '''开启通讯'''
  19. def startCommunication(self):
  20. self.sendcmd('COM,1\r')
  21. '''结束通讯'''
  22. def endCommunication(self):
  23. self.sendcmd('COM,0\r')
  24. '''设置用于测量的校准通道'''
  25. def setChannel(self, channel:str):
  26. self.sendcmd('MCH,%s\r'%(channel))
  27. '''
  28. 设置显示模式
  29. mode:0-8
  30. 0: x,y,Lv
  31. 1: Tcp,duv,Lv
  32. 5: u',v',Lv
  33. 6: Flicker mode
  34. 7: X,Y,Z
  35. 8: λd,Pe,Lv
  36. '''
  37. def setDisplayMode(self, mode:int):
  38. return self.sendcmd('MDS,%d\r'%(mode))
  39. '''设置显示模式为XYZ'''
  40. def set_XYZ_Display(self):
  41. return self.sendcmd('MDS,%d\r'%(7))
  42. '''设置显示模式为xyLv'''
  43. def set_xyLv_Display(self):
  44. return self.sendcmd('MDS,%d\r'%(0))
  45. '''
  46. 设置同步模式和同步频率
  47. mode:同步模式,0-5
  48. 0:NTSC
  49. 1:PAL
  50. 2: EXTERNAL
  51. 3: UNIVERSAL
  52. 4: INTERNAL
  53. 5: MANUAL
  54. freq:同步频率,1-6;
  55. '''
  56. def setSynchMode(self, mode: int, freq: int = None):
  57. if freq == None:
  58. return self.sendcmd('SCS,%d\r'%(mode))
  59. else:
  60. return self.sendcmd('SCS,%d,%d\r'%(mode,freq))
  61. '''
  62. 设置测量速度
  63. speed: 0-3
  64. 0: SLOW
  65. 1: FAST
  66. 2: LTD.AUTO
  67. 3: AUTO
  68. '''
  69. def setMeasureSpeed(self, speed: int):
  70. return self.sendcmd('FSC,%d\r'%(speed))
  71. '''执行零点校准'''
  72. def setZeroCalibration(self):
  73. return self.sendcmd('ZRC\r')
  74. '''读取当前显示模式下的三值'''
  75. def readDisplay(self):
  76. data = self.sendcmd('MES\r')
  77. x,y,z = self.__get_display_data(data.decode('utf-8'))
  78. return float(x), float(y), float(z)
  79. def __readxyLv2(self):
  80. return self.sendcmd('MES,1\r')
  81. def __readXYZ(self):
  82. data = self.sendcmd('MES,2\r')
  83. # bytearray转str:使用decode。
  84. result, des, X,Y,Z = self.__get_XYZ_data(data.decode('utf-8'))
  85. return X,Y,Z
  86. '''获取当前显示模式值'''
  87. def __get_display_data(self, data):
  88. # 正则表达式;
  89. # OK00,P1 3716;3956;.0091\r
  90. p = re.compile(r"(\D*)(\d+),P1 (.*);(.*);(.*)", re.DOTALL)
  91. mo = p.search(data)
  92. if mo is None:
  93. print("无匹配正则")
  94. pw = re.compile(r"(\D*)(\d+)", re.DOTALL)
  95. mo = pw.search(data)
  96. if mo is None:
  97. # print("短匹配失败")
  98. return None, None, None
  99. else:
  100. return None, None, None
  101. else:
  102. print(mo.group(1), mo.group(2), mo.group(3), mo.group(4), mo.group(5))
  103. return mo.group(3), mo.group(4), mo.group(5)
  104. '''读取MES,2模式的值'''
  105. def __get_XYZ_data(self, data):
  106. # 正则表达式;
  107. # 'OK00,P1,0,0.2265919,0.1769892,0.0437458,-0.03,-99999999,0.0560060,0.0437458,0.1474149\r'
  108. p = re.compile(r"(\D*)(\d+),P1,(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*)\r", re.DOTALL)
  109. mo = p.search(data)
  110. if mo is None:
  111. print("无匹配正则")
  112. pw = re.compile(r"(\D*)(\d+)", re.DOTALL)
  113. mo = pw.search(data)
  114. if mo is None:
  115. # print("短匹配失败")
  116. return False, u"异常", None, None, None,
  117. else:
  118. return False, mo.group(2), None, None, None
  119. else:
  120. print(mo.group(1), mo.group(2), mo.group(3), mo.group(4), mo.group(5), mo.group(6), mo.group(7), mo.group(8), mo.group(9), mo.group(10), mo.group(11))
  121. return True, mo.group(1), mo.group(9), mo.group(10), mo.group(11)
  122. def xyY_to_XYZ( x, y, Y):
  123. X = x * Y / y
  124. Z = (1 - x - y) * Y / y
  125. return X, Y, Z
  126. def test_ca410():
  127. print('\r\rtest ca410\r\r')
  128. ca = CASerail()
  129. ca.open("COM8", 19200, 7, 'E', 2)
  130. ca.startCommunication()
  131. ca.set_XYZ_Display()
  132. ca.setSynchMode(3)
  133. ca.setMeasureSpeed(2)
  134. ca.setZeroCalibration()
  135. ca.setChannel('01')
  136. X,Y,Z=ca.readDisplay()
  137. print("\rthe XYZ = ",float(X), float(Y), float(Z))
  138. ca.endCommunication()
  139. def test_ca310():
  140. print('\r\rtest ca310\r\r')
  141. ca = CASerail()
  142. ca.open("COM6", 19200, 7, 'E', 2)
  143. ca.startCommunication()
  144. ca.setChannel('01')
  145. ca.setSynchMode(3)
  146. ca.setMeasureSpeed(2)
  147. #ca.set_xyLv_Display()
  148. ca.set_XYZ_Display()
  149. X,Y,Z=ca.readDisplay()
  150. print("\rthe XYZ = ",float(X), float(Y), float(Z))
  151. ca.endCommunication()
  152. if __name__ == "__main__":
  153. test_ca410()
  154. test_ca310()
  155. X,Y,Z = xyY_to_XYZ(0.226, 0.171, 0.040)
  156. print("xyLv=(%f,%f,%f), XYZ=(%f,%f,%f)"%(0.226, 0.171, 0.04, X,Y,Z))