NRZSetControl.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using Uestc.Auto6.Dso.ComModel;
  12. using Uestc.Auto6.Dso.Core;
  13. using Uestc.Auto6.Dso.Core.Decode;
  14. using Uni_Trend.MSO7000X.Common.Helper;
  15. using Uni_Trend.MSO7000X.Language;
  16. using Uni_Trend.MSO7000X.UserControls;
  17. using Uni_Trend.MSO7000X.UserControls.Style;
  18. namespace Uestc.Auto6.Dso.Protocol.NRZ
  19. {
  20. [System.ComponentModel.ToolboxItem(false)]
  21. public partial class NRZSetControl : UserControl,IProtocolView
  22. {
  23. #region 属性定义
  24. protected new Boolean DesignMode
  25. {
  26. get
  27. {
  28. Boolean rtnflag = false;
  29. #if DEBUG
  30. rtnflag = DesignTimeHelper.InDesignMode(this);
  31. #endif
  32. return rtnflag;
  33. }
  34. }
  35. public NRZDecodePrsnt DestinDecodePrsnt
  36. {
  37. get => _DestinDecodePrsnt;
  38. set
  39. {
  40. if (_DestinDecodePrsnt != value)
  41. {
  42. _DestinDecodePrsnt = value;
  43. ///当有新的同步目标时,应先将目标中参数同步到本地<see cref="NRZDecodePrsnt"/>属性中
  44. if (DecodePrsnt != null && value != null)
  45. {
  46. typeof(NRZDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite).ToList().ForEach(x =>
  47. {
  48. x.SetValue(DecodePrsnt, x.GetValue(value));
  49. });
  50. }
  51. }
  52. }
  53. }
  54. private NRZDecodePrsnt _DestinDecodePrsnt;
  55. private NRZDecodePrsnt DecodePrsnt => Presenter as NRZDecodePrsnt;
  56. public IProtocolPrsnt Presenter { get; set; }
  57. #endregion 属性定义
  58. public NRZSetControl()
  59. {
  60. InitializeComponent();
  61. }
  62. protected override void OnLoad(EventArgs e)
  63. {
  64. base.OnLoad(e);
  65. Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(this, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize);
  66. Init();
  67. }
  68. private void Init()
  69. {
  70. GetDestinPrsnt();
  71. InitSource1();
  72. InitSource2();
  73. InitSignalRate();
  74. InitRealBiteRate();
  75. InitThreshold();
  76. InitMSB();
  77. InitSourceType();
  78. InitIdelLevel();
  79. InitIdelTime();
  80. InitMode();
  81. UpdateView();
  82. }
  83. private void InitSourceType()
  84. {
  85. this.CbxSourceType.DataSource = Enum.GetValues<ProtocolNRZ.SignalType>().Select(x=>
  86. {
  87. String key = String.Empty;
  88. key = Properties.Resources.ResourceManager.GetString(x.GetType().ReflectedType.FullName + "." + x.GetType().Name + "." + x.ToString());
  89. if (String.IsNullOrEmpty(key))
  90. {
  91. key = x.ToString();
  92. }
  93. return new KeyValuePair<String, ProtocolNRZ.SignalType>(key, x);
  94. }).ToList();
  95. this.CbxSourceType.ValueMember = "Value";
  96. this.CbxSourceType.DisplayMember = "Key";
  97. this.CbxSourceType.SelectedValueChanged += (_, _) =>
  98. {
  99. DecodePrsnt.SignalType = (ProtocolNRZ.SignalType)this.CbxSourceType.SelectedValue;
  100. };
  101. }
  102. private void InitMode()
  103. {
  104. this.CbxMode.DataSource =Enum.GetValues<ProtocolNRZ.Mode>().Select(x=>
  105. {
  106. return new KeyValuePair<String, ProtocolNRZ.Mode>(x switch
  107. {
  108. ProtocolNRZ.Mode.NRZ => "NRZ",
  109. ProtocolNRZ.Mode._8_10B => "8/10B",
  110. ProtocolNRZ.Mode._64_66B => "64/66B",
  111. _ => "NRZ",
  112. },x);
  113. }).ToList();
  114. this.CbxMode.ValueMember = "Value";
  115. this.CbxMode.DisplayMember = "Key";
  116. this.CbxMode.SelectedValueChanged += (_, _) =>
  117. {
  118. DecodePrsnt.Mode = (ProtocolNRZ.Mode)this.CbxMode.SelectedValue;
  119. };
  120. }
  121. private void InitIdelTime()
  122. {
  123. this.NebIdelTime.MaxValue = DecodePrsnt.MaxIdleTime;
  124. this.NebIdelTime.MinValue = DecodePrsnt.MinIdleTime;
  125. this.NebIdelTime.Value = DecodePrsnt.IdleTime;
  126. this.NebIdelTime.Interval = 1;
  127. this.NebIdelTime.StringFormatFunc = (val) => $"{(UInt32)val}";
  128. this.NebIdelTime.ValueChanged += (sender, args) => DecodePrsnt.IdleTime = (UInt32)args.newValue;
  129. this.NebIdelTime.EditValueChicked += (_, _) => SetIdelTime();
  130. }
  131. private void SetIdelTime()
  132. {
  133. Boolean lastflag = true;
  134. if (this.FindForm() is FloatForm form)
  135. {
  136. lastflag = form.CanClose;
  137. form.CanClose = false;
  138. }
  139. FloatForm floatForm = new FloatForm();
  140. floatForm = new Uni_Trend.MSO7000X.UserControls.FloatForm();
  141. floatForm.BackColor = BackColor;
  142. floatForm.ForeColor = ForeColor;
  143. floatForm.Width = 380;
  144. floatForm.Height = 460 + floatForm.HeadHeight;
  145. floatForm.Title = "设置空闲阈值";
  146. Uni_Trend.MSO7000X.UserControls.HexNumberKeyboard numberKeyboard = new Uni_Trend.MSO7000X.UserControls.HexNumberKeyboard();
  147. numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(x, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize));
  148. numberKeyboard.Top = floatForm.HeadHeight;
  149. numberKeyboard.Height = floatForm.Height - floatForm.HeadHeight;
  150. numberKeyboard.Width = floatForm.Width;
  151. numberKeyboard.ForeColor = ForeColor;
  152. numberKeyboard.BackColor = BackColor;
  153. numberKeyboard.Value = DecodePrsnt.IdleTime;
  154. numberKeyboard.MinValue = DecodePrsnt.MinIdleTime;
  155. numberKeyboard.MaxValue = DecodePrsnt.MaxIdleTime;
  156. numberKeyboard.ValueType = HexNumberKeyboard.HexValueType.Dec;
  157. numberKeyboard.OkClick += (sender, args) =>
  158. {
  159. this.DecodePrsnt.IdleTime = (UInt32)args.Data;
  160. floatForm?.Close();
  161. };
  162. numberKeyboard.CancelClick += (sender, args) => floatForm?.Close();
  163. floatForm.Controls.Add(numberKeyboard);
  164. DefaultStyleManager.Instance.RegisterControlRecursion(floatForm, StyleFlag.FontSize);
  165. floatForm.FormClosing += (_, _) =>
  166. {
  167. if (this.FindForm() is FloatForm form)
  168. form.CanClose = lastflag;
  169. };
  170. floatForm.ShowDialogByPosition();
  171. }
  172. private void InitIdelLevel()
  173. {
  174. this.RgbIdelLevel.ButtonItems = Enum.GetValues<ProtocolNRZ.IdleLevel>().Select(x =>
  175. {
  176. String key = String.Empty;
  177. key = Properties.Resources.ResourceManager.GetString(x.GetType().ReflectedType.FullName + "." + x.GetType().Name + "." + x.ToString());
  178. if (String.IsNullOrEmpty(key))
  179. {
  180. key = x.ToString();
  181. }
  182. return new RadioButtonItem()
  183. {
  184. Tag =x,
  185. Text = key,
  186. };
  187. }).ToArray();
  188. this.RgbIdelLevel.IndexChanged += (_, _) =>
  189. {
  190. DecodePrsnt.IdleLevel = (ProtocolNRZ.IdleLevel)this.RgbIdelLevel.ButtonItems[RgbIdelLevel.ChoosedButtonIndex].Tag;
  191. };
  192. }
  193. private void InitThreshold()
  194. {
  195. this.BtnThreshold.Click += (sender, args) => SetThreshold();
  196. }
  197. private void SetThreshold()
  198. {
  199. Boolean lastflag = true;
  200. if (this.FindForm() is FloatForm form)
  201. {
  202. lastflag = form.CanClose;
  203. form.CanClose = false;
  204. }
  205. FloatForm floatForm = new FloatForm();
  206. floatForm = new Uni_Trend.MSO7000X.UserControls.FloatForm();
  207. floatForm.BackColor = BackColor;
  208. floatForm.ForeColor = ForeColor;
  209. floatForm.Width = 380;
  210. floatForm.Height = 460 + floatForm.HeadHeight;
  211. floatForm.Title = "设置门限";
  212. Uni_Trend.MSO7000X.UserControls.NumberKeyboard numberKeyboard = new Uni_Trend.MSO7000X.UserControls.NumberKeyboard();
  213. numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(x, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize));
  214. numberKeyboard.Top = floatForm.HeadHeight;
  215. numberKeyboard.Height = floatForm.Height - floatForm.HeadHeight;
  216. numberKeyboard.Width = floatForm.Width;
  217. numberKeyboard.ForeColor = ForeColor;
  218. numberKeyboard.BackColor = BackColor;
  219. numberKeyboard.DefaultValue = DecodePrsnt.Threshold;
  220. numberKeyboard.Unit = "V";
  221. numberKeyboard.MinValue = DecodePrsnt.MinThreshold;
  222. numberKeyboard.MaxValue = DecodePrsnt.MaxThreshold;
  223. numberKeyboard.DecimalNumber = 2;
  224. numberKeyboard.OkClickEvent += (sender, args) =>
  225. {
  226. this.DecodePrsnt.Threshold = args.Data;
  227. floatForm?.Close();
  228. };
  229. numberKeyboard.CancelEvent += (sender, args) => floatForm?.Close();
  230. floatForm.Controls.Add(numberKeyboard);
  231. DefaultStyleManager.Instance.RegisterControlRecursion(floatForm, StyleFlag.FontSize);
  232. floatForm.FormClosing += (_, _) =>
  233. {
  234. if (this.FindForm() is FloatForm form)
  235. form.CanClose = lastflag;
  236. };
  237. floatForm.ShowDialogByPosition();
  238. }
  239. private void InitSource1()
  240. {
  241. this.CbxSource.DataSource = ComModel.ChannelIdExt.GetAnalogs().Select(x => new ComboBoxData<ChannelId>(x.ToString(), x)).ToList();
  242. this.CbxSource.ValueMember = "Value";
  243. this.CbxSource.DisplayMember = "Key";
  244. this.CbxSource.SelectedValueChanged += (_, _) =>
  245. {
  246. DecodePrsnt.Source1 = (ChannelId)this.CbxSource.SelectedValue;
  247. };
  248. }
  249. private void InitSource2()
  250. {
  251. this.CbxSource2.DataSource = ComModel.ChannelIdExt.GetAnalogs().Select(x => new ComboBoxData<ChannelId>(x.ToString(), x)).ToList();
  252. this.CbxSource2.ValueMember = "Value";
  253. this.CbxSource2.DisplayMember = "Key";
  254. this.CbxSource2.SelectedValueChanged += (_, _) =>
  255. {
  256. DecodePrsnt.Source2 = (ChannelId)this.CbxSource2.SelectedValue;
  257. };
  258. }
  259. private void InitMSB()
  260. {
  261. this.RbgMSB.ButtonItems = Enum.GetValues<ProtocolNRZ.MSB_LSB>().Select(x =>
  262. {
  263. return new RadioButtonItem()
  264. {
  265. Tag = x,
  266. Text = x.ToString(),
  267. };
  268. }).ToArray();
  269. this.RbgMSB.IndexChanged += (_, _) =>
  270. {
  271. DecodePrsnt.MSB_LSB = (ProtocolNRZ.MSB_LSB)this.RbgMSB.ButtonItems[this.RbgMSB.ChoosedButtonIndex].Tag;
  272. };
  273. }
  274. private void InitSignalRate()
  275. {
  276. this.CbxBitRate.DataSource = Enum.GetValues<ProtocolNRZ.SignalRate>().Select(x => new KeyValuePair<String, ProtocolNRZ.SignalRate>(x switch
  277. {
  278. ProtocolNRZ.SignalRate.Speed_650M => "650Mbps",
  279. ProtocolNRZ.SignalRate.Speed_6_5G => "6.5Gbps",
  280. ProtocolNRZ.SignalRate.Speed_1G=>"1Gbps",
  281. ProtocolNRZ.SignalRate.Custom => "Custom",
  282. _ => "1Mbps",
  283. }, x)).ToList();
  284. this.CbxBitRate.ValueMember = "Value";
  285. this.CbxBitRate.DisplayMember = "Key";
  286. this.CbxBitRate.SelectedValueChanged += (_, _) =>
  287. {
  288. DecodePrsnt.SignalRate = (ProtocolNRZ.SignalRate)this.CbxBitRate.SelectedValue;
  289. };
  290. }
  291. private void InitRealBiteRate()
  292. {
  293. this.BtnRealBitRate.Click += (sender, args) => SetBitRate();
  294. }
  295. private void SetBitRate()
  296. {
  297. Boolean lastflag = true;
  298. if (this.FindForm() is FloatForm form)
  299. {
  300. lastflag = form.CanClose;
  301. form.CanClose = false;
  302. }
  303. FloatForm floatForm = new FloatForm();
  304. floatForm = new Uni_Trend.MSO7000X.UserControls.FloatForm();
  305. floatForm.BackColor = BackColor;
  306. floatForm.ForeColor = ForeColor;
  307. floatForm.Width = 380;
  308. floatForm.Height = 460 + floatForm.HeadHeight;
  309. floatForm.Title = "设置波特率";
  310. Uni_Trend.MSO7000X.UserControls.NumberKeyboard numberKeyboard = new Uni_Trend.MSO7000X.UserControls.NumberKeyboard();
  311. numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(x, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize));
  312. numberKeyboard.Top = floatForm.HeadHeight;
  313. numberKeyboard.Height = floatForm.Height - floatForm.HeadHeight;
  314. numberKeyboard.Width = floatForm.Width;
  315. numberKeyboard.ForeColor = ForeColor;
  316. numberKeyboard.BackColor = BackColor;
  317. numberKeyboard.DefaultValue = DecodePrsnt.RealSignalRate;
  318. numberKeyboard.Unit = "bps";
  319. numberKeyboard.MinValue = DecodePrsnt.MinSignalRate;
  320. numberKeyboard.MaxValue = DecodePrsnt.MaxSignalRate;
  321. numberKeyboard.DecimalNumber = 2;
  322. numberKeyboard.OkClickEvent += (sender, args) =>
  323. {
  324. this.DecodePrsnt.RealSignalRate = (Int64)args.Data;
  325. floatForm?.Close();
  326. };
  327. numberKeyboard.CancelEvent += (sender, args) => floatForm?.Close();
  328. floatForm.Controls.Add(numberKeyboard);
  329. DefaultStyleManager.Instance.RegisterControlRecursion(floatForm, StyleFlag.FontSize);
  330. floatForm.FormClosing += (_, _) =>
  331. {
  332. if (this.FindForm() is FloatForm form)
  333. form.CanClose = lastflag;
  334. };
  335. floatForm.ShowDialogByPosition();
  336. }
  337. [UpdateProperty(nameof(NRZDecodePrsnt.SignalType))]
  338. private void UpdateSignalType()
  339. {
  340. this.CbxSourceType.SelectedValue = DecodePrsnt.SignalType;
  341. LblSource2.Visible = DecodePrsnt.SignalType == ProtocolNRZ.SignalType.Difference;
  342. CbxSource2.Visible = LblSource2.Visible;
  343. }
  344. [UpdateProperty(nameof(NRZDecodePrsnt.SignalRate))]
  345. private void UpdateRate()
  346. {
  347. this.CbxBitRate.SelectedValue = DecodePrsnt.SignalRate;
  348. this.LblRealBitRate.Visible = DecodePrsnt.SignalRate == ProtocolNRZ.SignalRate.Custom;
  349. this.BtnRealBitRate.Visible = this.LblRealBitRate.Visible;
  350. }
  351. [UpdateProperty(nameof(NRZDecodePrsnt.RealSignalRate))]
  352. private void UpdateRealRate()
  353. {
  354. this.BtnRealBitRate.Text = SIHelper.ValueChangeToSI(DecodePrsnt.RealSignalRate, 2, "bps");
  355. }
  356. [UpdateProperty(nameof(NRZDecodePrsnt.Threshold))]
  357. private void UpdateThreshold()
  358. {
  359. this.BtnThreshold.Text = SIHelper.ValueChangeToSI(DecodePrsnt.Threshold, 2, "V");
  360. }
  361. [UpdateProperty(nameof(NRZDecodePrsnt.Mode))]
  362. private void UpdateMode()
  363. {
  364. this.CbxMode.SelectedValue = DecodePrsnt.Mode;
  365. }
  366. [UpdateProperty(nameof(NRZDecodePrsnt.IdleTime))]
  367. private void UpdateIdleTime()
  368. {
  369. this.NebIdelTime.Value = DecodePrsnt.IdleTime;
  370. }
  371. [UpdateProperty(nameof(NRZDecodePrsnt.IdleLevel))]
  372. private void UpdateIdelLevel()
  373. {
  374. this.RgbIdelLevel.ChoosedButtonIndex = Enum.GetValues<ProtocolNRZ.IdleLevel>().ToList().FindIndex(x => x == DecodePrsnt.IdleLevel);
  375. }
  376. [UpdateProperty(nameof(NRZDecodePrsnt.MSB_LSB))]
  377. private void UpdatePolarity()
  378. {
  379. this.RbgMSB.ChoosedButtonIndex = Enum.GetValues<ProtocolNRZ.MSB_LSB>().ToList().FindIndex(x => x == DecodePrsnt.MSB_LSB);
  380. }
  381. [UpdateProperty(nameof(NRZDecodePrsnt.Source1))]
  382. private void UpdateSource()
  383. {
  384. this.CbxSource.SelectedValue = DecodePrsnt.Source1;
  385. GetDestinPrsnt();
  386. }
  387. [UpdateProperty(nameof(NRZDecodePrsnt.Source2))]
  388. private void UpdateSource2()
  389. {
  390. this.CbxSource2.SelectedValue = DecodePrsnt.Source2;
  391. }
  392. [UpdateProperty(nameof(ProtocolNRZ.SignalType))]
  393. private void UpdateSourceType()
  394. {
  395. this.CbxSourceType.SelectedValue = DecodePrsnt.SignalType;
  396. }
  397. /// <summary>
  398. /// 获取需要同步的Prsnt
  399. /// 当<see cref="DecodePrsnt"/>为<see cref="null"/>时表示不需要与其他Prsnt进行同步
  400. /// </summary>
  401. private void GetDestinPrsnt()
  402. {
  403. if (DecodePrsnt == null)
  404. return;
  405. if (DecodePrsnt.IsTrigger)
  406. DestinDecodePrsnt = Presenter.GetChannlesDecodePrsnt().Where(x => x is NRZDecodePrsnt).Cast<NRZDecodePrsnt>().Where(x => x.Source1 == DecodePrsnt.Source1).OrderBy(x => x.Source1).FirstOrDefault();
  407. else
  408. {
  409. if (Presenter.GetTriggerDecodePrsnt() is NRZDecodePrsnt rs232decodePrsnt && rs232decodePrsnt.Source1 == DecodePrsnt.Source1)
  410. DestinDecodePrsnt = rs232decodePrsnt;
  411. else
  412. DestinDecodePrsnt = null;
  413. }
  414. }
  415. /// <summary>
  416. /// 同步两个Prsnt
  417. /// </summary>
  418. /// <param name="propertyName">属性名称</param>
  419. private void DecodeSynchronization(String propertyName)
  420. {
  421. if (DestinDecodePrsnt == null)
  422. return;
  423. if (propertyName == nameof(NRZDecodePrsnt.Source1))
  424. return;
  425. if (String.IsNullOrEmpty(propertyName))
  426. {
  427. typeof(NRZDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite).ToList().ForEach(x =>
  428. {
  429. x.SetValue(DestinDecodePrsnt, x.GetValue(DecodePrsnt));
  430. });
  431. }
  432. else
  433. {
  434. PropertyInfo propertyInfo = typeof(NRZDecodePrsnt).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
  435. if (propertyInfo != null && propertyInfo.CanWrite && propertyInfo.CanRead)
  436. propertyInfo.SetValue(DestinDecodePrsnt, propertyInfo.GetValue(DecodePrsnt));
  437. }
  438. }
  439. public void UpdateView(Object presenter, String propertyName)
  440. {
  441. DecodeSynchronization(propertyName);
  442. if (String.IsNullOrEmpty(propertyName))
  443. {
  444. UpdateView();
  445. return;
  446. }
  447. this.GetType()
  448. .GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
  449. .Where(x => x.GetCustomAttribute<UpdatePropertyAttribute>() != null && x.GetCustomAttribute<UpdatePropertyAttribute>().PropertyName == propertyName)
  450. .ToList()
  451. .ForEach(x => x?.Invoke(this, null));
  452. }
  453. private void UpdateView()
  454. {
  455. this.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic).Where(x => x.GetCustomAttribute<UpdatePropertyAttribute>() != null).ToList().ForEach(x => x?.Invoke(this, null));
  456. this.Refresh();
  457. }
  458. }
  459. }