StubFuncs_FactoryCommand.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using UESTC.Auto.SCPIManager;
  8. using Uestc.Auto6.Dso.Hardware.Calibration.Data.Base;
  9. using Uestc.Auto6.Dso.ComModel;
  10. using Uestc.Auto6.Dso.Core;
  11. using Uestc.Auto6.Dso.Core.Hardware;
  12. using CalibrationData = Uestc.Auto6.Dso.Hardware.Calibration.Data.Base;
  13. namespace Uestc.Auto6.Dso.Scpi
  14. {
  15. partial class StubFunc
  16. {
  17. private static string ConvertParamToString(SCPICommandProcessFuncParam analyResult)
  18. {
  19. StringBuilder sb = new StringBuilder();
  20. if (analyResult.Params != null)
  21. {
  22. bool bFirst = true;
  23. foreach (byte[] param in analyResult.Params)
  24. {
  25. if (!bFirst)
  26. sb.Append(",");
  27. if (param != null)
  28. {
  29. foreach (byte b in param)
  30. {
  31. char c = (char)b;
  32. if (c != ' ' && c != '\r' && c != '\n')
  33. sb.Append(c);
  34. }
  35. }
  36. bFirst = false;
  37. }
  38. }
  39. return sb.ToString();
  40. }
  41. private static void PushCaliDataChangedHdCmd(CaliDataType caliDataType)
  42. {
  43. CaliDataManager.DataChangedCaliDataType = CaliDataManager.DataChangedCaliDataType | caliDataType;
  44. CalibrationPrsnt.Push(HdCmd.CaliDataChanged);
  45. }
  46. public static bool scpiQuy_FPGA_Version(SCPICommandProcessFuncParam analyResult, ref SCPISendMessage sendMessage)
  47. {
  48. sendMessage.IsDataBlock = false;
  49. sendMessage.SendData = System.Text.Encoding.UTF8.GetBytes(ExportHdFuncs.GetAllFPGAVersionInfo());
  50. return true;
  51. }
  52. public static bool scpiQuy_FPGA_AllWriteRegisterValue(SCPICommandProcessFuncParam analyResult, ref SCPISendMessage sendMessage)
  53. {
  54. sendMessage.IsDataBlock = false;
  55. sendMessage.SendData = System.Text.Encoding.UTF8.GetBytes(ExportHdFuncs.ReadbackAllWritedRegisterValue());
  56. return true;
  57. }
  58. public static bool scpiSet_FPGA_WriteRegister(SCPICommandProcessFuncParam analyResult)
  59. {
  60. string paramStr = ConvertParamToString(analyResult);
  61. string[] paramList = paramStr.Split(',');
  62. if (paramList.Length < 3)
  63. return false;
  64. if (!UInt32.TryParse(paramList[0], out UInt32 addr))
  65. return false;
  66. if (!UInt32.TryParse(paramList[1], out UInt32 data))
  67. return false;
  68. bool bIsAcq = paramList[2] == "1" ? true : false;
  69. return ExportHdFuncs.FPGARegister_WriteValue(addr, data, bIsAcq);
  70. }
  71. #region 校准数据
  72. public static bool scpiQuy_CaliData_Get(SCPICommandProcessFuncParam analyResult, ref SCPISendMessage sendMessage)
  73. {
  74. sendMessage.IsDataBlock = true;
  75. sendMessage.SendData = CalibrationData.Helper.GetICaliData((CaliDataType)analyResult.ChannelIndex).Serialize();
  76. PrintDebug(ConvertInputData(analyResult), ConvertOutputData(sendMessage.SendData, true));
  77. return true;
  78. }
  79. private static byte[] bigCaliData = new byte[128 * 1024];
  80. private static CaliDataType lastCaliDataType = CaliDataType.None;
  81. private static int bigCaliDataIndex = 0;
  82. public static bool scpiSet_CaliData_Set(SCPICommandProcessFuncParam analyResult)
  83. {
  84. PrintDebug(ConvertInputData(analyResult), null);
  85. if (lastCaliDataType == CaliDataType.None)
  86. {
  87. bigCaliDataIndex = 0;
  88. lastCaliDataType = (CaliDataType)analyResult.ChannelIndex;
  89. }
  90. if (analyResult.Params[0].Length == 0)
  91. {
  92. CaliDataType caliDataType = (CaliDataType)analyResult.ChannelIndex;
  93. CalibrationData.Helper.GetICaliData(caliDataType).Deserialize(bigCaliData);
  94. PushCaliDataChangedHdCmd(caliDataType);
  95. lastCaliDataType = CaliDataType.None;
  96. bigCaliDataIndex = 0;
  97. }
  98. else
  99. {
  100. byte[] data = ConvertBinDataFromScpiData(analyResult.Params[0]);
  101. Array.Copy(data, 0, bigCaliData, bigCaliDataIndex, data.Length);
  102. bigCaliDataIndex += data.Length;
  103. }
  104. return true;
  105. }
  106. public static bool scpiSet_CaliData_SaveToFile(SCPICommandProcessFuncParam analyResult)
  107. {
  108. PrintDebug(ConvertInputData(analyResult), null);
  109. CalibrationData.Helper.GetICaliData((CaliDataType)analyResult.ChannelIndex).SaveToFile();
  110. return true;
  111. }
  112. public static bool scpiSet_CaliData_LoadFromFile(SCPICommandProcessFuncParam analyResult)
  113. {
  114. PrintDebug(ConvertInputData(analyResult), null);
  115. CaliDataType caliDataType= (CaliDataType)analyResult.ChannelIndex;
  116. PushCaliDataChangedHdCmd(caliDataType);
  117. CalibrationData.Helper.GetICaliData((CaliDataType)analyResult.ChannelIndex).LoadFromFile();
  118. return true;
  119. }
  120. #endregion
  121. #region 波形数据
  122. static ushort[] allCoreData = new ushort[ChannelIdExt.AnaChnlNum * Constants.ADC_NUM * CaliConstants.PerAdcCoreCount * CaliConstants.PerAdcCoreWaveDots];
  123. public static bool scpiQuy_Factory_GetWaveData_Adc(SCPICommandProcessFuncParam analyResult, ref SCPISendMessage sendMessage)
  124. {
  125. if (ExportHdFuncs.TakeAdcWaveform(out List<List<ushort>> waveData))
  126. {
  127. Int64 posIndex = 0;
  128. for (int coreIndex = 0; coreIndex < ChannelIdExt.AnaChnlNum * Constants.ADC_NUM * CaliConstants.PerAdcCoreCount; coreIndex++)
  129. {
  130. Array.Copy(waveData[coreIndex].ToArray(), 0, allCoreData, posIndex, CaliConstants.PerAdcCoreWaveDots);
  131. posIndex += CaliConstants.PerAdcCoreWaveDots;
  132. }
  133. MemoryStream memoryStream = new MemoryStream();
  134. foreach (ushort s in allCoreData)
  135. memoryStream.Write(BitConverter.GetBytes(s));
  136. sendMessage.IsDataBlock = true;
  137. sendMessage.SendData = memoryStream.ToArray();
  138. memoryStream.Close();
  139. PrintDebug(ConvertInputData(analyResult), ConvertOutputData(sendMessage.SendData, true));
  140. return true;
  141. }
  142. else
  143. return false;
  144. }
  145. static ushort[] allChannelData = new ushort[CaliConstants.PerPhysicsChannelDotsCount * CaliConstants.MaxPhysicsChannelCount];
  146. public static bool scpiQuy_Factory_GetWaveData_Channel(SCPICommandProcessFuncParam analyResult, ref SCPISendMessage sendMessage)
  147. {
  148. if (ExportHdFuncs.TakeChannelWaveform(out List<List<ushort>> waveData))
  149. {
  150. Int64 posIndex = 0;
  151. for (int adcIndex = 0; adcIndex < CaliConstants.MaxPhysicsChannelCount; adcIndex++)
  152. {
  153. Array.Copy(waveData[adcIndex].ToArray(), 0, allChannelData, posIndex, CaliConstants.PerPhysicsChannelDotsCount);
  154. posIndex += CaliConstants.PerPhysicsChannelDotsCount;
  155. }
  156. MemoryStream memoryStream = new MemoryStream();
  157. foreach (ushort s in allChannelData)
  158. memoryStream.Write(BitConverter.GetBytes(s));
  159. sendMessage.IsDataBlock = true;
  160. sendMessage.SendData = memoryStream.ToArray();
  161. memoryStream.Close();
  162. PrintDebug(ConvertInputData(analyResult), ConvertOutputData(sendMessage.SendData, true));
  163. return true;
  164. }
  165. else
  166. return false;
  167. }
  168. #endregion
  169. #region 厂家校准用逻辑参数
  170. public static bool scpiQuy_FactoryCaliLogicValue(SCPICommandProcessFuncParam analyResult, ref SCPISendMessage sendMessage)
  171. {
  172. sendMessage.SendData = System.Text.Encoding.UTF8.GetBytes(ExportHdFuncs.FactoryCaliScpiProc_LogicValue_Get());
  173. return true;
  174. }
  175. public static bool scpiSet_FactoryCaliLogicValue(SCPICommandProcessFuncParam analyResult)
  176. {
  177. string paramStr = ConvertParamToString(analyResult);
  178. return ExportHdFuncs.FactoryCaliScpiProc_LogicValue_Set(paramStr);
  179. }
  180. #endregion 厂家校准用逻辑参数
  181. #region 厂家校准用特殊数据
  182. public static bool scpiQuy_FactoryCaliSpecialData(SCPICommandProcessFuncParam analyResult, ref SCPISendMessage sendMessage)
  183. {
  184. string paramStr = ConvertParamToString(analyResult);
  185. sendMessage.SendData = System.Text.Encoding.UTF8.GetBytes(ExportHdFuncs.FactoryCaliScpiProc_SpecialData_Get(paramStr));
  186. return true;
  187. }
  188. public static bool scpiSet_FactoryCaliSpecialData(SCPICommandProcessFuncParam analyResult)
  189. {
  190. string paramStr = ConvertParamToString(analyResult);
  191. return ExportHdFuncs.FactoryCaliScpiProc_SpecialData_Set(paramStr);
  192. }
  193. public static bool scpiQuy_FactoryCaliSpecialReadBackAdcRegister(SCPICommandProcessFuncParam analyResult, ref SCPISendMessage sendMessage)
  194. {
  195. string paramStr = ConvertParamToString(analyResult);
  196. sendMessage.SendData = System.Text.Encoding.UTF8.GetBytes(ExportHdFuncs.ReadbackAdcRegisterData());
  197. return true;
  198. }
  199. public static bool scpiSet_FactoryCaliApplySource(SCPICommandProcessFuncParam analyResult)
  200. {
  201. bool TryGetScaleIndex(int valueByuV, out Core.AnaChnlScaleIndex scaleIndex)
  202. {
  203. scaleIndex = Core.AnaChnlScaleIndex.Lv1;
  204. for (int i = 0; i < AnalogChannel.PhyChCoarseLevelTableByuV.Count; i++)
  205. {
  206. if (AnalogChannel.PhyChCoarseLevelTableByuV[i] == valueByuV)
  207. {
  208. scaleIndex = (Core.AnaChnlScaleIndex)i;
  209. return true;
  210. }
  211. }
  212. return false;
  213. }
  214. string paramStr = ConvertParamToString(analyResult);
  215. string[] paramList = paramStr.Split(',');
  216. //打开[0,1],幅度档(mv数),基线位置[0],耦合[0=DC,1=AC,2=DC50],阻抗[0=1M,1=50],带宽限制[0=关闭,1=20M,2=100M],偏置[mv数],探头倍率[枚举],反向[0=off,1=on],单位[0=V,1=A]
  217. // 0 1 2 3 4 5 6 7 8 9
  218. if (Presenter.TryGetChannel((ChannelId)(analyResult.ChannelIndex - 1), out var ch))
  219. {
  220. AnalogPrsnt anaChnlPrsnt = (AnalogPrsnt)ch;
  221. anaChnlPrsnt.Active = paramList[0] == "1";
  222. int scaleBymV = int.Parse(paramList[1]);
  223. if (TryGetScaleIndex(scaleBymV * 1_000, out Core.AnaChnlScaleIndex scaleIndex))
  224. anaChnlPrsnt.ScaleIndex = (int)scaleIndex;
  225. anaChnlPrsnt.PosIndexBymDiv = double.Parse(paramList[2]);
  226. anaChnlPrsnt.Coupling = (AnaChnlCoupling)int.Parse(paramList[3]);
  227. anaChnlPrsnt.Bandwidth = paramList[5] switch
  228. {
  229. "0" => AnaChnlBandwidth.Full,
  230. "1" => AnaChnlBandwidth.Bw20MHz,
  231. "2" => AnaChnlBandwidth.Bw500MHz,
  232. _ => AnaChnlBandwidth.Full
  233. };
  234. anaChnlPrsnt.Bias = double.Parse(paramList[6]) * 1000; //mV==>uV
  235. anaChnlPrsnt.IsInverted = (paramList[8] == "1");
  236. }
  237. else
  238. return false;
  239. return true;
  240. }
  241. public static bool scpiSet_FactoryCaliApplyAllSource(SCPICommandProcessFuncParam analyResult)
  242. {
  243. bool TryGetScaleIndex(int valueByuV, out Core.AnaChnlScaleIndex scaleIndex)
  244. {
  245. scaleIndex = Core.AnaChnlScaleIndex.Lv1;
  246. for (int i = 0; i < AnalogChannel.PhyChCoarseLevelTableByuV.Count; i++)
  247. {
  248. if (AnalogChannel.PhyChCoarseLevelTableByuV[i] == valueByuV)
  249. {
  250. scaleIndex = (Core.AnaChnlScaleIndex)i;
  251. return true;
  252. }
  253. }
  254. return false;
  255. }
  256. string paramStr = ConvertParamToString(analyResult);
  257. string[] paramList = paramStr.Split(',');
  258. //打开[0,1],幅度档(mv数),基线位置[0],耦合[直流、交流、接地,枚举],阻抗[0=1M_om,1=5O_om],带宽限制[0=关闭,1=20M,2=100M],偏置[mv数],探头倍率[枚举],反向[0=off,1=on],单位[0=V,1=A]
  259. // 0 1 2 3 4 5 6 7 8 9
  260. int scaleBymV = int.Parse(paramList[1]);
  261. if (!TryGetScaleIndex(scaleBymV * 1_000, out Core.AnaChnlScaleIndex scaleIndex))
  262. return false;
  263. AnaChnlCoupling anaChnlCoupling = (AnaChnlCoupling)int.Parse(paramList[3]);
  264. AnaChnlBandwidth anaChnlBandwidth = paramList[5] switch
  265. {
  266. "0" => AnaChnlBandwidth.Full,
  267. "1" => AnaChnlBandwidth.Bw20MHz,
  268. "2" => AnaChnlBandwidth.Bw500MHz,
  269. _ => AnaChnlBandwidth.Full
  270. };
  271. double bias = double.Parse(paramList[6]) * 1000; //mV==>uV
  272. bool IsInverted = (paramList[8] == "1");
  273. bool bActive = paramList[0] == "1";
  274. double posIndex = double.Parse(paramList[2]);
  275. for (ChannelId channelId = ChannelId.C1; channelId < ChannelId.C1 + ChannelIdExt.AnaChnlNum; channelId++)
  276. {
  277. if (Presenter.TryGetChannel(channelId, out var ch))
  278. {
  279. AnalogPrsnt anaChnlPrsnt = (AnalogPrsnt)ch;
  280. try
  281. {
  282. anaChnlPrsnt.Active = bActive;
  283. anaChnlPrsnt.ScaleIndex = (int)scaleIndex;
  284. anaChnlPrsnt.PosIndexBymDiv = posIndex;
  285. anaChnlPrsnt.Coupling = anaChnlCoupling;
  286. anaChnlPrsnt.Bandwidth = anaChnlBandwidth;
  287. anaChnlPrsnt.Bias = bias;
  288. anaChnlPrsnt.IsInverted = IsInverted;
  289. }
  290. catch
  291. {
  292. }
  293. }
  294. }
  295. return true;
  296. }
  297. #endregion 厂家校准用特殊数据
  298. }
  299. }