LINSetControl.cs 16 KB

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