SENTSetControl.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using System.Windows.Forms;
  5. using Uestc.Auto6.Dso.Core;
  6. using Uestc.Auto6.Dso.Core.Decode;
  7. using Uni_Trend.MSO7000X.Common.Helper;
  8. using Uni_Trend.MSO7000X.UserControls;
  9. using Uni_Trend.MSO7000X.Language;
  10. namespace Uestc.Auto6.Dso.Protocol.SENT
  11. {
  12. [System.ComponentModel.ToolboxItem(false)]
  13. public partial class SENTSetControl : UserControl, IProtocolView
  14. {
  15. #region 属性定义
  16. public SENTSetControl()
  17. {
  18. InitializeComponent();
  19. }
  20. protected override void OnLoad(EventArgs e)
  21. {
  22. base.OnLoad(e);
  23. Init();
  24. }
  25. protected new Boolean DesignMode
  26. {
  27. get
  28. {
  29. Boolean rtnflag = false;
  30. #if DEBUG
  31. rtnflag = DesignTimeHelper.InDesignMode(this);
  32. #endif
  33. return rtnflag;
  34. }
  35. }
  36. FloatForm _FloatForm;
  37. private SENTDecodePrsnt DestinDecodePrsnt
  38. {
  39. get => _DestinDecodePrsnt;
  40. set
  41. {
  42. if (_DestinDecodePrsnt != value)
  43. {
  44. _DestinDecodePrsnt = value;
  45. ///当有新的同步目标时,应先将目标中参数同步到本地<see cref="SENTDecodePrsnt"/>属性中
  46. if (SENTDecodePrsnt != null && value != null)
  47. {
  48. typeof(SENTDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite&&x.CanRead).ToList().ForEach(x =>
  49. {
  50. x.SetValue(SENTDecodePrsnt, x.GetValue(value));
  51. });
  52. }
  53. }
  54. }
  55. }
  56. private SENTDecodePrsnt _DestinDecodePrsnt;
  57. private SENTDecodePrsnt SENTDecodePrsnt => Presenter as SENTDecodePrsnt;
  58. public IProtocolPrsnt Presenter { get; set; }
  59. #endregion 属性定义
  60. private void Init()
  61. {
  62. GetDestinPrsnt();
  63. Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControl(x, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize));
  64. SetRealRateVisible(false);
  65. InitSource();
  66. InitThreshold();
  67. InitPolarity();
  68. InitDataLength();
  69. InitCRC();
  70. InitSignalRate();
  71. InitRealSignalRate();
  72. InitAinTerval();
  73. }
  74. private void InitSource()
  75. {
  76. this.CbxSource.DataSource =ComModel.ChannelIdExt.GetAnalogs().GetEnumList();
  77. this.CbxSource.ValueMember = "Value";
  78. this.CbxSource.DisplayMember = "Key";
  79. this.CbxSource.SelectedValueChanged += (_, _) =>
  80. {
  81. this.SENTDecodePrsnt.Source = (ComModel.ChannelId)this.CbxSource.SelectedValue;
  82. };
  83. SetSource();
  84. }
  85. private void InitThreshold()
  86. {
  87. SetThreshold();
  88. this.BtnThreshold.Click += (_, _) =>
  89. {
  90. Boolean lastflag = true;
  91. if (this.FindForm() is FloatForm form)
  92. {
  93. lastflag = form.CanClose;
  94. form.CanClose = false;
  95. }
  96. if (_FloatForm != null && !_FloatForm.IsDisposed)
  97. _FloatForm?.Close();
  98. _FloatForm = new FloatForm();
  99. _FloatForm.BackColor = BackColor;
  100. _FloatForm.ForeColor = ForeColor;
  101. _FloatForm.Title = "设置阈值";
  102. _FloatForm.Width = 380;
  103. _FloatForm.Height = 500 + _FloatForm.HeadHeight;
  104. NumberKeyboard numberKeyboard = new NumberKeyboard();
  105. numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControl(x));
  106. numberKeyboard.Top = _FloatForm.HeadHeight;
  107. numberKeyboard.Height = _FloatForm.Height - _FloatForm.HeadHeight;
  108. numberKeyboard.Width = _FloatForm.Width;
  109. numberKeyboard.ForeColor = ForeColor;
  110. numberKeyboard.BackColor = BackColor;
  111. numberKeyboard.MaxValue = 12;
  112. numberKeyboard.MinValue = -12;
  113. numberKeyboard.Unit = "V";
  114. numberKeyboard.DefaultValue = SENTDecodePrsnt.Threshold;
  115. numberKeyboard.OkClickEvent += (sender, args) =>
  116. {
  117. SENTDecodePrsnt.Threshold = args.Data;
  118. _FloatForm.Close();
  119. };
  120. numberKeyboard.CancelEvent += (_, _) => _FloatForm.Close();
  121. _FloatForm.Controls.Add(numberKeyboard);
  122. _FloatForm.FormClosing += (_, _) =>
  123. {
  124. if (this.FindForm() is FloatForm form)
  125. form.CanClose = lastflag;
  126. };
  127. _FloatForm.ShowDialogByPosition();
  128. };
  129. }
  130. private void InitPolarity()
  131. {
  132. this.RbgPolarity.ButtonItems = Enum.GetValues<ComModel.ProtocolSENT.Polarity>().Select(x =>
  133. {
  134. return new RadioButtonItem()
  135. {
  136. Tag =x,
  137. Text =x.GetDescription(),
  138. };
  139. }).ToArray();
  140. this.RbgPolarity.IndexChanged += (_, _) =>
  141. {
  142. this.SENTDecodePrsnt.Polarity = (ComModel.ProtocolSENT.Polarity)this.RbgPolarity.ButtonItems[this.RbgPolarity.ChoosedButtonIndex].Tag;
  143. };
  144. SetPolarity();
  145. }
  146. private void InitDataLength()
  147. {
  148. this.RbgDataLength.ButtonItems = Enum.GetValues<ComModel.ProtocolSENT.DataLength>().Select(x =>
  149. {
  150. return new RadioButtonItem()
  151. {
  152. Tag = x,
  153. Text = x switch
  154. {
  155. ComModel.ProtocolSENT.DataLength.Nibbles_5=>"5",
  156. ComModel.ProtocolSENT.DataLength.Nibbles_8=>"8",
  157. _=>"5",
  158. },
  159. };
  160. }).ToArray();
  161. this.RbgDataLength.IndexChanged += (_, _) =>
  162. {
  163. this.SENTDecodePrsnt.DataLength = (ComModel.ProtocolSENT.DataLength)this.RbgDataLength.ButtonItems[this.RbgDataLength.ChoosedButtonIndex].Tag;
  164. };
  165. SetDataLength();
  166. }
  167. private void InitCRC()
  168. {
  169. this.RbgCRC.ButtonItems = Enum.GetValues<ComModel.ProtocolSENT.SENTCRC>().Select(x =>
  170. {
  171. return new RadioButtonItem()
  172. {
  173. Tag = x,
  174. Text = x.GetDescription(),
  175. };
  176. }).ToArray();
  177. this.RbgCRC.IndexChanged += (_, _) =>
  178. {
  179. this.SENTDecodePrsnt.CRC = (ComModel.ProtocolSENT.SENTCRC)this.RbgCRC.ButtonItems[this.RbgCRC.ChoosedButtonIndex].Tag;
  180. };
  181. SetCRC();
  182. }
  183. private void InitSignalRate()
  184. {
  185. this.CbxSignalRate.DataSource = Enum.GetValues<ComModel.ProtocolSENT.SignalRate>().Select(x =>
  186. {
  187. return new System.Collections.Generic.KeyValuePair<string, ComModel.ProtocolSENT.SignalRate>(x == ComModel.ProtocolSENT.SignalRate.SignalRate_custom?"Custom":SIHelper.ValueChangeToSI(GetRealRate(x),2,"bps"),x);
  188. }).ToList();
  189. this.CbxSignalRate.ValueMember = "Value";
  190. this.CbxSignalRate.DisplayMember = "Key";
  191. this.CbxSignalRate.SelectedValueChanged += (_, _) =>
  192. {
  193. this.SENTDecodePrsnt.SignalRate = (ComModel.ProtocolSENT.SignalRate)this.CbxSignalRate.SelectedValue;
  194. };
  195. SetSignalRate();
  196. }
  197. private void InitRealSignalRate()
  198. {
  199. SetRealSignalRate();
  200. this.BtnRealSignalRate.Click += (_, _) =>
  201. {
  202. Boolean lastflag = true;
  203. if (this.FindForm() is FloatForm form)
  204. {
  205. lastflag = form.CanClose;
  206. form.CanClose = false;
  207. }
  208. if (_FloatForm != null && !_FloatForm.IsDisposed)
  209. _FloatForm?.Close();
  210. _FloatForm = new FloatForm();
  211. _FloatForm.BackColor = BackColor;
  212. _FloatForm.ForeColor = ForeColor;
  213. _FloatForm.Title = "设置波特率";
  214. _FloatForm.Width = 380;
  215. _FloatForm.Height = 500 + _FloatForm.HeadHeight;
  216. NumberKeyboard numberKeyboard = new NumberKeyboard();
  217. numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControl(x));
  218. numberKeyboard.Top = _FloatForm.HeadHeight;
  219. numberKeyboard.Height = _FloatForm.Height - _FloatForm.HeadHeight;
  220. numberKeyboard.Width = _FloatForm.Width;
  221. numberKeyboard.ForeColor = ForeColor;
  222. numberKeyboard.BackColor = BackColor;
  223. numberKeyboard.MaxValue = 10E6;
  224. numberKeyboard.MinValue = 10E3;
  225. numberKeyboard.Unit = "bps";
  226. numberKeyboard.DecimalNumber = 0;
  227. numberKeyboard.DefaultValue = SENTDecodePrsnt.RealSignalRate;
  228. numberKeyboard.OkClickEvent += (sender, args) =>
  229. {
  230. SENTDecodePrsnt.RealSignalRate = (Int64)args.Data;
  231. _FloatForm.Close();
  232. };
  233. numberKeyboard.CancelEvent += (_, _) => _FloatForm.Close();
  234. _FloatForm.Controls.Add(numberKeyboard);
  235. _FloatForm.FormClosing += (_, _) =>
  236. {
  237. if (this.FindForm() is FloatForm form)
  238. form.CanClose = lastflag;
  239. };
  240. _FloatForm.ShowDialogByPosition();
  241. };
  242. }
  243. private void InitAinTerval()
  244. {
  245. this.RbgAinTerval.ButtonItems = Enum.GetValues<ComModel.ProtocolSENT.AppendInterval>().Select(x =>
  246. {
  247. return new RadioButtonItem()
  248. {
  249. Tag = x,
  250. Text = x.GetDescription(),
  251. };
  252. }).ToArray();
  253. this.RbgAinTerval.IndexChanged += (_, _) =>
  254. {
  255. SENTDecodePrsnt.Interval = (ComModel.ProtocolSENT.AppendInterval)this.RbgAinTerval.ButtonItems[this.RbgAinTerval.ChoosedButtonIndex].Tag;
  256. };
  257. SetAinTerval();
  258. }
  259. /// <summary>
  260. /// 获取需要同步的Prsnt
  261. /// 当<see cref="DestinDecodePrsnt"/>为<see cref="null"/>时表示不需要与其他Prsnt进行同步
  262. /// 当<see cref="SENTDecodePrsnt.Source"/> 相同时进行同步
  263. /// </summary>
  264. private void GetDestinPrsnt()
  265. {
  266. if (this.SENTDecodePrsnt == null)
  267. return;
  268. if (SENTDecodePrsnt.IsTrigger)
  269. DestinDecodePrsnt = Presenter.GetChannlesDecodePrsnt().Where(x => x is SENTDecodePrsnt).Cast<SENTDecodePrsnt>().Where(x => CheckRules(x, SENTDecodePrsnt)).OrderBy(x => x.Source).FirstOrDefault();
  270. else
  271. {
  272. if (Presenter.GetTriggerDecodePrsnt() is SENTDecodePrsnt decodePrsnt && CheckRules(decodePrsnt, SENTDecodePrsnt))
  273. DestinDecodePrsnt = decodePrsnt;
  274. else
  275. DestinDecodePrsnt = null;
  276. }
  277. }
  278. /// 判断两个Prsnt是否同步的规则
  279. /// </summary>
  280. /// <param name="fisrt"></param>
  281. /// <param name="second"></param>
  282. /// <returns></returns>
  283. private Boolean CheckRules(SENTDecodePrsnt fisrt, SENTDecodePrsnt second)
  284. {
  285. if (fisrt == null || second == null)
  286. return false;
  287. return fisrt.Source == second.Source;
  288. }
  289. public void UpdateView(Object presenter, String propertyName)
  290. {
  291. DecodeSynchronization(propertyName);
  292. if (String.IsNullOrEmpty(propertyName))
  293. {
  294. UpdateView();
  295. return;
  296. }
  297. this.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Where(x =>
  298. {
  299. UpdatePropertyAttribute attribute = x.GetCustomAttribute<UpdatePropertyAttribute>();
  300. return attribute != null && attribute.PropertyName == propertyName;
  301. }).FirstOrDefault()?.Invoke(this, null);
  302. }
  303. private void UpdateView()
  304. {
  305. this.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Where(x => x.GetCustomAttribute<UpdatePropertyAttribute>() != null).ToList().ForEach(x => x?.Invoke(this, null));
  306. this.Refresh();
  307. }
  308. /// <summary>
  309. /// 同步两个Prsnt
  310. /// </summary>
  311. /// <param name="propertyName">属性名称</param>
  312. private void DecodeSynchronization(String propertyName)
  313. {
  314. if (DestinDecodePrsnt == null)
  315. return;
  316. ///由于使用了<see cref="SENTDecodePrsnt.Source"/>进行判断具体同步的目标
  317. ///因此<see cref="SENTDecodePrsnt.Source"/>参数不进行同步
  318. if (propertyName == nameof(Core.Decode.SENTDecodePrsnt.Source))
  319. return;
  320. if (String.IsNullOrEmpty(propertyName))
  321. {
  322. typeof(SENTDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite&&x.CanRead).ToList().ForEach(x =>
  323. {
  324. x.SetValue(DestinDecodePrsnt, x.GetValue(this.SENTDecodePrsnt));
  325. });
  326. }
  327. else
  328. {
  329. PropertyInfo propertyInfo = typeof(SENTDecodePrsnt).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
  330. if (propertyInfo != null&&propertyInfo.CanWrite&&propertyInfo.CanRead)
  331. propertyInfo.SetValue(DestinDecodePrsnt, propertyInfo.GetValue(this.SENTDecodePrsnt));
  332. }
  333. }
  334. [UpdateProperty(nameof(Uestc.Auto6.Dso.Core.Decode.SENTDecodePrsnt.Source))]
  335. private void SetSource()
  336. {
  337. this.CbxSource.SelectedValue = SENTDecodePrsnt.Source;
  338. GetDestinPrsnt();
  339. }
  340. [UpdateProperty(nameof(Core.Decode.SENTDecodePrsnt.Threshold))]
  341. private void SetThreshold()
  342. {
  343. this.BtnThreshold.Text = SIHelper.ValueChangeToSI(SENTDecodePrsnt.Threshold, 2, "V");
  344. }
  345. [UpdateProperty(nameof(Core.Decode.SENTDecodePrsnt.Polarity))]
  346. private void SetPolarity()
  347. {
  348. this.RbgPolarity.ChoosedButtonIndex = Enum.GetValues<ComModel.ProtocolSENT.Polarity>().ToList().FindIndex(x => x == SENTDecodePrsnt.Polarity);
  349. }
  350. [UpdateProperty(nameof(Core.Decode.SENTDecodePrsnt.DataLength))]
  351. private void SetDataLength()
  352. {
  353. this.RbgDataLength.ChoosedButtonIndex = Enum.GetValues<ComModel.ProtocolSENT.DataLength>().ToList().FindIndex(x => x == SENTDecodePrsnt.DataLength);
  354. }
  355. [UpdateProperty(nameof(Core.Decode.SENTDecodePrsnt.CRC))]
  356. private void SetCRC()
  357. {
  358. this.RbgCRC.ChoosedButtonIndex = Enum.GetValues<ComModel.ProtocolSENT.SENTCRC>().ToList().FindIndex(x => x == SENTDecodePrsnt.CRC);
  359. }
  360. [UpdateProperty(nameof(Core.Decode.SENTDecodePrsnt.SignalRate))]
  361. private void SetSignalRate()
  362. {
  363. this.CbxSignalRate.SelectedValue = this.SENTDecodePrsnt.SignalRate;
  364. SetRealRateVisible(this.SENTDecodePrsnt.SignalRate == ComModel.ProtocolSENT.SignalRate.SignalRate_custom);
  365. if (this.SENTDecodePrsnt.SignalRate != ComModel.ProtocolSENT.SignalRate.SignalRate_custom)
  366. this.SENTDecodePrsnt.RealSignalRate = GetRealRate(SENTDecodePrsnt.SignalRate);
  367. }
  368. [UpdateProperty(nameof(Core.Decode.SENTDecodePrsnt.RealSignalRate))]
  369. private void SetRealSignalRate()
  370. {
  371. this.BtnRealSignalRate.Text = SIHelper.ValueChangeToSI(this.SENTDecodePrsnt.RealSignalRate, 2, "bps");
  372. Int32 index = Enum.GetValues<ComModel.ProtocolSENT.SignalRate>().ToList().FindIndex(x => GetRealRate(x) == SENTDecodePrsnt.RealSignalRate);
  373. if (index != -1)
  374. this.SENTDecodePrsnt.SignalRate = Enum.GetValues<ComModel.ProtocolSENT.SignalRate>().ToList()[index];
  375. }
  376. [UpdateProperty(nameof(Core.Decode.SENTDecodePrsnt.Interval))]
  377. private void SetAinTerval()
  378. {
  379. this.RbgAinTerval.ChoosedButtonIndex = Enum.GetValues<ComModel.ProtocolSENT.AppendInterval>().ToList().FindIndex(x => x == this.SENTDecodePrsnt.Interval);
  380. }
  381. private void SetRealRateVisible(Boolean visible = false)
  382. {
  383. this.LblRealSignalRate.Visible = visible;
  384. this.BtnRealSignalRate.Visible = this.LblRealSignalRate.Visible;
  385. }
  386. private Int64 GetRealRate(ComModel.ProtocolSENT.SignalRate rate)
  387. {
  388. return rate switch
  389. {
  390. ComModel.ProtocolSENT.SignalRate.SignalRate_100k => 100000,
  391. ComModel.ProtocolSENT.SignalRate.SignalRate_10k => 10000,
  392. ComModel.ProtocolSENT.SignalRate.SignalRate_125k => 125000,
  393. ComModel.ProtocolSENT.SignalRate.SignalRate_1m => 1000000,
  394. ComModel.ProtocolSENT.SignalRate.SignalRate_20k => 20000,
  395. ComModel.ProtocolSENT.SignalRate.SignalRate_33_3k => 33300,
  396. ComModel.ProtocolSENT.SignalRate.SignalRate_50k => 50000,
  397. ComModel.ProtocolSENT.SignalRate.SignalRate_62_5k => 62500,
  398. ComModel.ProtocolSENT.SignalRate.SignalRate_83_3k => 83300,
  399. _ => -1,
  400. };
  401. }
  402. }
  403. }