CUGWatch.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // *****************************************************************************
  2. // 版权所有(C)2023~2099 上海骄成超声波技术有限公司
  3. // 保留所有权利
  4. // *****************************************************************************
  5. // 作者 : 杨坚
  6. // 版本 : 1.0
  7. // 功能说明:
  8. //
  9. // *****************************************************************************
  10. #include "CUGWatch.h"
  11. #include "../JLogAllOutput.h"
  12. CUGWatch::CUGWatch()
  13. {
  14. ReadConfig();
  15. }
  16. CUGWatch::~CUGWatch()
  17. {
  18. }
  19. void CUGWatch::ReadConfig()
  20. {
  21. ComParameter com = {};
  22. com.nSerialPortNumber = 1;
  23. com.nBaudRate = 65535;
  24. m_comPar.push_back(com);
  25. m_cUGSerial.reset(new CRS232());
  26. }
  27. bool CUGWatch::GetUserParaUT100(int& iMode, int& iVolFullScale, int& iCurFullScale, int& iPowFullScale, int& iSignSns, int& iTestLev, int iOverTime/* = 2000*/)
  28. {
  29. bool bRtn = false;
  30. iMode = 0;
  31. iVolFullScale = 0;
  32. iCurFullScale = 0;
  33. iPowFullScale = 0;
  34. iSignSns = 0;
  35. iTestLev = 0;
  36. if (COM_OK == JOpenPort())
  37. {
  38. m_cUGSerial->ClearBuffer();
  39. Sleep(10);
  40. // 读取当前编码器
  41. unsigned char uiDataBuf[100] = { 0 };
  42. int iDataLen = 0;
  43. uiDataBuf[iDataLen++] = 0xFF; //起始标志
  44. uiDataBuf[iDataLen++] = 0x90; //命令
  45. //发送
  46. if (iDataLen != m_cUGSerial->SerialPortWrite(iDataLen, (char*)uiDataBuf))
  47. {
  48. JLogAllOutput::cmd_debug("Write Error");
  49. }
  50. else
  51. {
  52. Sleep(200);
  53. //读串口
  54. unsigned char bufRev[100] = { 0 };
  55. int len = sizeof(bufRev);
  56. int bytes = m_cUGSerial->SerialPortRead(len, (char*)bufRev, 1000);
  57. if (bytes >= 1)
  58. {
  59. if (9 == bytes)
  60. {
  61. iSignSns = (bufRev[2] & 0xF0) >> 4;
  62. iVolFullScale = (bufRev[3] & 0xF0) >> 4;
  63. iCurFullScale = (bufRev[4] & 0xF0) >> 4;
  64. iPowFullScale = (bufRev[5] & 0xF0) >> 4;
  65. iMode = (bufRev[6] & 0xF0) >> 4;
  66. iMode += 1; //为了和UT60统一
  67. iTestLev = (bufRev[8] & 0xF0) >> 4;
  68. bRtn = TRUE;
  69. }
  70. }
  71. }
  72. }
  73. JClosePort();
  74. return bRtn;
  75. }
  76. bool CUGWatch::SetUserParaUT100(int iMode, int iVolFullScale, int iCurFullScale, int iPowFullScale, int iSignSns, int iTestLev, int iOverTime/* = 2000*/)
  77. {
  78. bool bRtn = false;
  79. if (COM_OK == JOpenPort())
  80. {
  81. // 写入
  82. unsigned char uiDataBuf[100] = { 0 };
  83. int iDataLen = 0;
  84. uiDataBuf[iDataLen++] = 0xFF; //起始标志
  85. uiDataBuf[iDataLen++] = 0x50; //命令
  86. uiDataBuf[iDataLen++] = iSignSns << 4; //信号极性
  87. uiDataBuf[iDataLen++] = iVolFullScale << 4; //电压量程
  88. uiDataBuf[iDataLen++] = iCurFullScale << 4; //电流量程
  89. uiDataBuf[iDataLen++] = iPowFullScale << 4; //功率量程
  90. uiDataBuf[iDataLen++] = (iMode - 1) << 4; //工作模式,为了和UT60统一
  91. uiDataBuf[iDataLen++] = 0x00; //00
  92. uiDataBuf[iDataLen++] = iTestLev << 4; //测试输出等级
  93. //发送
  94. if (iDataLen != m_cUGSerial->SerialPortWrite(iDataLen, (char*)uiDataBuf))
  95. {
  96. JLogAllOutput::cmd_debug("Write Error");
  97. }
  98. else
  99. {
  100. Sleep(200);
  101. while (1)
  102. {
  103. //读串口
  104. unsigned char bufRev[100] = { 0 };
  105. int len = sizeof(bufRev);
  106. int bytes = m_cUGSerial->SerialPortRead(len, (char*)bufRev, 1000);
  107. if (bytes >= 1)
  108. {
  109. if (2 == bytes && 0xFF == bufRev[0] && 0x50 == bufRev[1])
  110. {
  111. bRtn = TRUE;
  112. }
  113. goto _label_exit;
  114. }
  115. Sleep(5);
  116. }
  117. }
  118. }
  119. _label_exit:
  120. JClosePort();
  121. return bRtn;
  122. }
  123. bool CUGWatch::SetCaliCenterFrqUT100(int iCenterFrq, int iOverTime /*= 2000*/)
  124. {
  125. bool bRtn = false;
  126. if (COM_OK == JOpenPort())
  127. {
  128. unsigned char uiDataBuf[100] = { 0 };
  129. int iDataLen = 0;
  130. uiDataBuf[iDataLen++] = 0xFF; //起始标志
  131. uiDataBuf[iDataLen++] = 0x60; //命令
  132. uiDataBuf[iDataLen++] = (iCenterFrq & 0XFF000000) >> 24; //A通道中心频率:2位最高位,5为最低位
  133. uiDataBuf[iDataLen++] = (iCenterFrq & 0XFF0000) >> 16; //
  134. uiDataBuf[iDataLen++] = (iCenterFrq & 0XFF00) >> 8; //
  135. uiDataBuf[iDataLen++] = iCenterFrq & 0XFF; //
  136. uiDataBuf[iDataLen++] = (iCenterFrq & 0XFF000000) >> 24; //B通道中心频率:6位最高位,9为最低位
  137. uiDataBuf[iDataLen++] = (iCenterFrq & 0XFF0000) >> 16; //
  138. uiDataBuf[iDataLen++] = (iCenterFrq & 0XFF00) >> 8; //
  139. uiDataBuf[iDataLen++] = iCenterFrq & 0XFF; //
  140. //发送
  141. if (iDataLen != m_cUGSerial->SerialPortWrite(iDataLen, (char*)uiDataBuf))
  142. {
  143. JLogAllOutput::cmd_debug("Write Error");
  144. }
  145. else
  146. {
  147. Sleep(200);
  148. while (1)
  149. {
  150. //读串口
  151. unsigned char bufRev[100] = { 0 };
  152. int len = sizeof(bufRev);
  153. int bytes = m_cUGSerial->SerialPortRead(len, (char*)bufRev, 1000);
  154. if (bytes >= 1)
  155. {
  156. if (2 == bytes && 0xFF == bufRev[0] && 0x60 == bufRev[1])
  157. {
  158. bRtn = TRUE;
  159. }
  160. goto _label_exit;
  161. }
  162. Sleep(5);
  163. }
  164. }
  165. }
  166. _label_exit:
  167. JClosePort();
  168. return bRtn;
  169. }
  170. bool CUGWatch::CalibrationUG_UT100()
  171. {
  172. bool bRtn = false;
  173. if (COM_OK == JOpenPort())
  174. {
  175. //Write 发送校准
  176. unsigned char uiDataBuf[10] = { 0 };
  177. int iDataLen = 0;
  178. uiDataBuf[iDataLen++] = 0xFF; //起始标志
  179. uiDataBuf[iDataLen++] = 0x61; //命令
  180. uiDataBuf[iDataLen++] = 0x01;
  181. //发送
  182. if (iDataLen != m_cUGSerial->SerialPortWrite(iDataLen, (char*)uiDataBuf))
  183. {
  184. JLogAllOutput::cmd_debug("Write Error");
  185. }
  186. else
  187. {
  188. //Read
  189. unsigned char ucDataBuf[256] = { 0 };
  190. long lCount = 0;
  191. bool bReading = FALSE;
  192. while (1)
  193. {
  194. //读串口
  195. unsigned char bufRev;
  196. int len = sizeof(bufRev);
  197. int bytes = m_cUGSerial->SerialPortRead(1, (char*)bufRev, 1000);
  198. if (bytes >= 1)
  199. {
  200. bReading = TRUE;
  201. ucDataBuf[lCount] = bufRev;
  202. lCount++;
  203. }
  204. else if (bReading)
  205. {
  206. break;
  207. }
  208. }
  209. //校验下
  210. {
  211. if (ucDataBuf[0] != 0xFF || ucDataBuf[1] != 0x61)
  212. {
  213. goto _label_exit;
  214. }
  215. else
  216. {
  217. bRtn = TRUE;
  218. }
  219. }
  220. }
  221. }
  222. _label_exit:
  223. JClosePort();
  224. return bRtn;
  225. }
  226. bool CUGWatch::CheckCaliFinish()
  227. {
  228. bool bRtn = false;
  229. if (COM_OK == JOpenPort())
  230. {
  231. unsigned char uiDataBuf[10] = { 0 };
  232. int iDataLen = 0;
  233. uiDataBuf[iDataLen++] = 0xFF; //起始标志
  234. uiDataBuf[iDataLen++] = 0x62; //命令
  235. //Write 发送校准
  236. if (iDataLen != m_cUGSerial->SerialPortWrite(iDataLen, (char*)uiDataBuf))
  237. {
  238. JLogAllOutput::cmd_debug("Write Error");
  239. goto _label_exit;
  240. }
  241. //Read
  242. unsigned char ucDataBuf[256] = { 0 };
  243. long lCount = 0;
  244. bool bReading = FALSE;
  245. while (1)
  246. {
  247. //读串口
  248. unsigned char bufRev;
  249. int len = sizeof(bufRev);
  250. int bytes = m_cUGSerial->SerialPortRead(1, (char*)bufRev, 1000);
  251. if (bytes >= 1)
  252. {
  253. bReading = TRUE;
  254. ucDataBuf[lCount] = bufRev;
  255. lCount++;
  256. }
  257. else if (bReading)
  258. {
  259. break;
  260. }
  261. }
  262. //校验下
  263. {
  264. if (ucDataBuf[0] != 0xFF || ucDataBuf[1] != 0x62)
  265. {
  266. goto _label_exit;
  267. }
  268. else
  269. {
  270. bRtn = true;
  271. }
  272. }
  273. }
  274. _label_exit:
  275. JClosePort();
  276. return bRtn;
  277. }
  278. int CUGWatch::JOpenPort()
  279. {
  280. JClosePort();
  281. ComParameter comOne = m_comPar[EN_COM_NUMBER::ONE];
  282. return m_cUGSerial->OpenPort(comOne.nSerialPortNumber,
  283. comOne.nBaudRate,
  284. comOne.nDataBits,//xDataSize?
  285. comOne.nParity,
  286. comOne.nStopBits,
  287. MAX_COM_BUFFER_LEN, MAX_COM_BUFFER_LEN, 1000
  288. );
  289. }
  290. void CUGWatch::JClosePort()
  291. {
  292. m_cUGSerial->ClearBuffer();
  293. m_cUGSerial->ClosePort();
  294. }