RS232SetControl.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. using System;
  2. using System.Windows.Forms;
  3. using Uni_Trend.MSO7000X.Common.Helper;
  4. using System.Linq;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using Uni_Trend.MSO7000X.UserControls;
  8. using Uestc.Auto6.Dso.Core;
  9. using Uestc.Auto6.Dso.ComModel;
  10. using Uestc.Auto6.Dso.Core.Decode;
  11. using Uni_Trend.MSO7000X.Language;
  12. using System.Reflection;
  13. namespace Uestc.Auto6.Dso.Protocol.RS232
  14. {
  15. public partial class RS232SetControl : UserControl, IProtocolView
  16. {
  17. public RS232SetControl()
  18. {
  19. InitializeComponent();
  20. InitView();
  21. }
  22. protected override void OnLoad(EventArgs e)
  23. {
  24. base.OnLoad(e);
  25. Init();
  26. }
  27. private void InitView()
  28. {
  29. Int32 rowcount = 1;
  30. Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControl(x));
  31. this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(x => x.GetCustomAttribute<RowColAttribute>() != null && x.GetValue(this) is Control).Select(x =>
  32. {
  33. RowColAttribute attribute = x.GetCustomAttribute<RowColAttribute>();
  34. Control control = x.GetValue(this) as Control;
  35. return (attribute, control);
  36. }).GroupBy(x => x.attribute, (x => x.control)).ToList().ForEach(x =>
  37. {
  38. Control lable = x.First(x => x is UestcLabel);
  39. Control control = x.First(x => !(x is UestcLabel));
  40. lable.Left = Width / _ColnumCount * x.Key.Colnum;
  41. lable.Width = control.Width;
  42. lable.Height = lable.Font.Height;
  43. lable.Top = 10 + _RowHeight * x.Key.Row;
  44. control.Left = lable.Left;
  45. control.Top = lable.Bottom + 2;
  46. lable.Width = 120;
  47. if (control is UIRadioButtonGroup)
  48. control.Width = 140;
  49. else
  50. control.Width = 120;
  51. rowcount = Math.Max(rowcount, x.Key.Row + 1);
  52. });
  53. Height = rowcount * _RowHeight;
  54. }
  55. private void Init()
  56. {
  57. GetDestinPrsnt();
  58. InitSignalType();
  59. InitSource();
  60. InitSourceL();
  61. InitBiteRate();
  62. InitRealBiteRate();
  63. InitDataBits();
  64. InitByteOrder();
  65. InitStopBits();
  66. InitParity();
  67. InitThreshold();
  68. InitPolarity();
  69. }
  70. /// <summary>
  71. /// 获取需要同步的Prsnt
  72. /// 当<see cref="DestinDecodePrsnt"/>为<see cref="null"/>时表示不需要与其他Prsnt进行同步
  73. /// </summary>
  74. private void GetDestinPrsnt()
  75. {
  76. if (_RS232DecodePrsnt == null)
  77. return;
  78. if (_RS232DecodePrsnt.IsTrigger)
  79. DestinDecodePrsnt = Presenter.GetChannlesDecodePrsnt().Where(x => x is RS232DecodePrsnt).Cast<RS232DecodePrsnt>().Where(x => x.ChannelId == _RS232DecodePrsnt.ChannelId).OrderBy(x => x.ChannelL).FirstOrDefault();
  80. else
  81. {
  82. if (Presenter.GetTriggerDecodePrsnt() is RS232DecodePrsnt rs232decodePrsnt && rs232decodePrsnt.ChannelId == _RS232DecodePrsnt.ChannelId)
  83. DestinDecodePrsnt = rs232decodePrsnt;
  84. else
  85. DestinDecodePrsnt = null;
  86. }
  87. }
  88. #region 属性定义
  89. protected new Boolean DesignMode
  90. {
  91. get
  92. {
  93. Boolean rtnflag = false;
  94. #if DEBUG
  95. rtnflag = DesignTimeHelper.InDesignMode(this);
  96. #endif
  97. return rtnflag;
  98. }
  99. }
  100. private RS232DecodePrsnt DestinDecodePrsnt
  101. {
  102. get => _DestinDecodePrsnt;
  103. set
  104. {
  105. if (_DestinDecodePrsnt != value)
  106. {
  107. _DestinDecodePrsnt = value;
  108. ///当有新的同步目标时,应先将目标中参数同步到本地<see cref="_RS232DecodePrsnt"/>属性中
  109. if (_RS232DecodePrsnt != null && value != null)
  110. {
  111. typeof(RS232DecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x=>x.CanWrite).ToList().ForEach(x =>
  112. {
  113. x.SetValue(_RS232DecodePrsnt, x.GetValue(value));
  114. });
  115. }
  116. }
  117. }
  118. }
  119. private RS232DecodePrsnt _DestinDecodePrsnt;
  120. private RS232DecodePrsnt _RS232DecodePrsnt => Presenter as RS232DecodePrsnt;
  121. public IProtocolPrsnt Presenter { get; set; }
  122. private FloatForm _FloatForm;
  123. private Int32 _RowHeight = 56;
  124. private Int32 _ColnumCount = 3;
  125. #endregion 属性定义
  126. #region 控件初始化
  127. private void InitSourceL()
  128. {
  129. this.CbxSourceL.DataSource = ChannelIdExt.GetAnalogs().Select(x => new ComboBoxData<ChannelId>(x.GetEnumValueResourceProperty(), x, x != _RS232DecodePrsnt.ChannelId)).ToList();
  130. this.CbxSourceL.DisplayMember = "Key";
  131. this.CbxSourceL.ValueMember = "Value";
  132. this.CbxSourceL.SelectedValueChanged += (_, _) =>
  133. {
  134. _RS232DecodePrsnt.ChannelL = (ChannelId)this.CbxSourceL.SelectedValue;
  135. };
  136. this.CbxSourceL.SelectedValue = _RS232DecodePrsnt.ChannelL;
  137. }
  138. private void InitPolarity()
  139. {
  140. this.RbgPolarity.ButtonItems = Enum.GetValues<ProtocolRS232.Polarity>().Select(x => new RadioButtonItem()
  141. {
  142. Tag = x,
  143. Text = x.GetDescription(),
  144. }).ToArray();
  145. this.RbgPolarity.IndexChanged += (sender, args) =>
  146. {
  147. _RS232DecodePrsnt.Polarity = (ProtocolRS232.Polarity)RbgPolarity.ButtonItems[RbgPolarity.ChoosedButtonIndex].Tag;
  148. };
  149. this.RbgPolarity.ChoosedButtonIndex = Enum.GetValues<ProtocolRS232.Polarity>().ToList().FindIndex(x => x == _RS232DecodePrsnt.Polarity);
  150. }
  151. private void InitThreshold()
  152. {
  153. this.BtnThreshold.Text = SIHelper.ValueChangeToSI(_RS232DecodePrsnt.Threshold, 2, "V");
  154. this.BtnThreshold.Click += (sender, args) => SetThreshold();
  155. }
  156. private void InitParity()
  157. {
  158. this.CbxParity.DataSource = Enum.GetValues<ProtocolRS232.OddEvenCheck>().Select(x => new KeyValuePair<string, ProtocolRS232.OddEvenCheck>(x.GetDescription(), x)).ToList();
  159. this.CbxParity.ValueMember = "Value";
  160. this.CbxParity.DisplayMember = "Key";
  161. this.CbxParity.SelectedValueChanged += (sender, args) =>
  162. {
  163. _RS232DecodePrsnt.Parity = (ProtocolRS232.OddEvenCheck)CbxParity.SelectedValue;
  164. };
  165. this.CbxParity.SelectedValue = _RS232DecodePrsnt.Parity;
  166. }
  167. private void InitStopBits()
  168. {
  169. this.CbxStopBits.ButtonItems = Enum.GetValues<ProtocolRS232.StopBit>().Select(x => new RadioButtonItem()
  170. {
  171. Text = x switch
  172. {
  173. ProtocolRS232.StopBit.StopBit_1bit => "1.0",
  174. ProtocolRS232.StopBit.StopBit_2bit => "2.0",
  175. _ => "",
  176. },
  177. Tag = x
  178. }).ToArray();
  179. this.CbxStopBits.IndexChanged += (sender, args) =>
  180. {
  181. _RS232DecodePrsnt.StopBits = (ProtocolRS232.StopBit)CbxStopBits.ButtonItems[CbxStopBits.ChoosedButtonIndex].Tag;
  182. };
  183. this.CbxStopBits.ChoosedButtonIndex = Enum.GetValues<ProtocolRS232.StopBit>().ToList().FindIndex(x => x == _RS232DecodePrsnt.StopBits);
  184. }
  185. private void InitByteOrder()
  186. {
  187. this.CbxByteOrder.ButtonItems = Enum.GetValues<ProtocolRS232.MSB_LSB>().Select(x => new RadioButtonItem()
  188. {
  189. Text = x.GetDescription(),
  190. Tag = x,
  191. }).ToArray();
  192. this.CbxByteOrder.IndexChanged += (sender, args) =>
  193. {
  194. _RS232DecodePrsnt.ByteOrder = (ProtocolRS232.MSB_LSB)CbxByteOrder.ButtonItems[CbxByteOrder.ChoosedButtonIndex].Tag;
  195. };
  196. this.CbxByteOrder.ChoosedButtonIndex = (Int32)_RS232DecodePrsnt.ByteOrder;
  197. }
  198. private void InitDataBits()
  199. {
  200. this.CbxDataBits.ButtonItems = Enum.GetValues<ProtocolRS232.DataBitWidth>().Select(x =>
  201. {
  202. String des = x switch
  203. {
  204. ProtocolRS232.DataBitWidth.DataBitWidth_5Bit => "5",
  205. ProtocolRS232.DataBitWidth.DataBitWidth_6Bit => "6",
  206. ProtocolRS232.DataBitWidth.DataBitWidth_7Bit => "7",
  207. ProtocolRS232.DataBitWidth.DataBitWidth_8Bit => "8",
  208. _ => "",
  209. };
  210. return new RadioButtonItem() { Text = des, Tag = x };
  211. }).ToArray();
  212. this.CbxDataBits.IndexChanged += (sender, args) =>
  213. {
  214. _RS232DecodePrsnt.DataBits = (ProtocolRS232.DataBitWidth)CbxDataBits.ButtonItems[CbxDataBits.ChoosedButtonIndex].Tag;
  215. };
  216. this.CbxDataBits.ChoosedButtonIndex = Enum.GetValues<ProtocolRS232.DataBitWidth>().ToList().FindIndex(x => x == _RS232DecodePrsnt.DataBits);
  217. }
  218. private void InitRealBiteRate()
  219. {
  220. this.BtnRealBitRate.Click += (sender, args) => SetBitRate();
  221. }
  222. private void InitBiteRate()
  223. {
  224. this.CbxBitRate.DataSource = Enum.GetValues<ProtocolRS232.BPSList>().Select(x => new KeyValuePair<string, ProtocolRS232.BPSList>(x switch
  225. {
  226. ProtocolRS232.BPSList.BPSList_Custom => "Custom",
  227. ProtocolRS232.BPSList.BPSList_115200 =>"115200bps",
  228. ProtocolRS232.BPSList.BPSList_19200=>"19200bps",
  229. ProtocolRS232.BPSList.BPSList_2400=>"2400bps",
  230. ProtocolRS232.BPSList.BPSList_38400=>"38400bps",
  231. ProtocolRS232.BPSList.BPSList_4800=>"4800bps",
  232. ProtocolRS232.BPSList.BPSList_57600=>"57600bps",
  233. ProtocolRS232.BPSList.BPSList_9600=>"9600bps",
  234. _ => ((UInt32)x).ToString() + "bps",
  235. }, x)).ToArray();
  236. this.CbxBitRate.ValueMember = "Value";
  237. this.CbxBitRate.DisplayMember = "Key";
  238. this.CbxBitRate.SelectedValueChanged += (sender, args) =>
  239. {
  240. ProtocolRS232.BPSList bitrate = (ProtocolRS232.BPSList)this.CbxBitRate.SelectedValue;
  241. SetRealBitRateVisibility(bitrate == ProtocolRS232.BPSList.BPSList_Custom);
  242. if (bitrate != ProtocolRS232.BPSList.BPSList_Custom)
  243. {
  244. this.BtnRealBitRate.Text = _RS232DecodePrsnt.BitRate.ToString();
  245. _RS232DecodePrsnt.BitRate = bitrate switch
  246. {
  247. ProtocolRS232.BPSList.BPSList_115200 =>115200,
  248. ProtocolRS232.BPSList.BPSList_19200 => 19200,
  249. ProtocolRS232.BPSList.BPSList_2400 => 2400,
  250. ProtocolRS232.BPSList.BPSList_38400 => 38400,
  251. ProtocolRS232.BPSList.BPSList_4800 => 4800,
  252. ProtocolRS232.BPSList.BPSList_57600 => 57600,
  253. ProtocolRS232.BPSList.BPSList_9600 => 9600,
  254. _=>9600,
  255. };
  256. }
  257. System.Diagnostics.Debug.WriteLine(bitrate);
  258. };
  259. UpdateBitRate();
  260. }
  261. private void InitSignalType()
  262. {
  263. this.CbxSignalType.DataSource = ProtocolRS232.SignalType.Difference.GetEnumList();
  264. this.CbxSignalType.DisplayMember = "Key";
  265. this.CbxSignalType.ValueMember = "Value";
  266. this.CbxSignalType.SelectedValueChanged += (_, _) =>
  267. {
  268. _RS232DecodePrsnt.ChType = (ProtocolRS232.SignalType)this.CbxSignalType.SelectedValue;
  269. ChangeSourceL();
  270. CbxSourceL.Visible = _RS232DecodePrsnt.ChType == ProtocolRS232.SignalType.Difference;
  271. LblSourceL.Visible = CbxSourceL.Visible;
  272. };
  273. this.CbxSignalType.SelectedValue = _RS232DecodePrsnt.ChType;
  274. }
  275. private void InitSource()
  276. {
  277. this.CbxSource.DataSource = ChannelIdExt.GetAnalogs().GetEnumList();
  278. this.CbxSource.DisplayMember = "Key";
  279. this.CbxSource.ValueMember = "Value";
  280. this.CbxSource.SelectedValueChanged += (_, _) =>
  281. {
  282. _RS232DecodePrsnt.ChannelId = (ChannelId)this.CbxSource.SelectedValue;
  283. SourceChanged();
  284. };
  285. this.CbxSource.SelectedValue = _RS232DecodePrsnt.ChannelId;
  286. }
  287. #endregion 控件初始化
  288. private void SourceChanged()
  289. {
  290. //通道发生改变后重新获取需要同步的Prsnt
  291. GetDestinPrsnt();
  292. ChangeSourceL();
  293. }
  294. /// <summary>
  295. /// 当<see cref="IRS232DecodePrsnt.ChType"/>属性为<see cref="RS232ChType.Differential"/>时,
  296. /// <see cref="IRS232DecodePrsnt.ChannelL"/>与<see cref="IRS232DecodePrsnt.ChannelId"/>中值不能相同
  297. /// </summary>
  298. private void ChangeSourceL()
  299. {
  300. if (_RS232DecodePrsnt.ChType == ProtocolRS232.SignalType.Difference)
  301. {
  302. if (_RS232DecodePrsnt.ChannelL == _RS232DecodePrsnt.ChannelId)
  303. {
  304. if (_RS232DecodePrsnt.ChannelL == ChannelIdExt.GetAnalogs().Last()) _RS232DecodePrsnt.ChannelL = ChannelIdExt.GetAnalogs().Last() - 1;
  305. else if (_RS232DecodePrsnt.ChannelL == ChannelIdExt.GetAnalogs().First()) _RS232DecodePrsnt.ChannelL = ChannelIdExt.GetAnalogs().First() + 1;
  306. else
  307. {
  308. _RS232DecodePrsnt.ChannelL++;
  309. }
  310. }
  311. if (this.CbxSourceL.DataSource is IList list)
  312. {
  313. foreach (var val in list)
  314. {
  315. (val as dynamic).Visible = (val as dynamic).Value != _RS232DecodePrsnt.ChannelId;
  316. }
  317. }
  318. }
  319. }
  320. /// <summary>
  321. /// 同步两个Prsnt
  322. /// </summary>
  323. /// <param name="propertyName">属性名称</param>
  324. private void DecodeSynchronization(String propertyName)
  325. {
  326. if (DestinDecodePrsnt == null)
  327. return;
  328. ///由于使用了<see cref="IRS232DecodePrsnt.ChannelId"/>进行判断具体同步的目标
  329. ///因此<see cref="IRS232DecodePrsnt.ChannelId"/>参数不进行同步
  330. if (propertyName == nameof(_RS232DecodePrsnt.ChannelId))
  331. return;
  332. if (String.IsNullOrEmpty(propertyName))
  333. {
  334. typeof(RS232DecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x=>x.CanWrite).ToList().ForEach(x =>
  335. {
  336. x.SetValue(DestinDecodePrsnt, x.GetValue(_RS232DecodePrsnt));
  337. });
  338. }
  339. else
  340. {
  341. PropertyInfo propertyInfo = typeof(RS232DecodePrsnt).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
  342. if (propertyInfo != null)
  343. propertyInfo.SetValue(DestinDecodePrsnt, propertyInfo.GetValue(_RS232DecodePrsnt));
  344. }
  345. }
  346. private void SetBitRate()
  347. {
  348. Boolean lastflag = true;
  349. if (this.FindForm() is FloatForm form)
  350. {
  351. lastflag = form.CanClose;
  352. form.CanClose = false;
  353. }
  354. if (_FloatForm != null && !_FloatForm.IsDisposed)
  355. _FloatForm?.Close();
  356. _FloatForm = new FloatForm();
  357. _FloatForm.BackColor = BackColor;
  358. _FloatForm.ForeColor = ForeColor;
  359. _FloatForm.Title = "设置波特率";
  360. _FloatForm.Width = 380;
  361. _FloatForm.Height = 500 + _FloatForm.HeadHeight;
  362. HexNumberKeyboard numberKeyboard = new HexNumberKeyboard();
  363. numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControl(x));
  364. numberKeyboard.Top = _FloatForm.HeadHeight;
  365. numberKeyboard.Height = _FloatForm.Height - _FloatForm.HeadHeight;
  366. numberKeyboard.Width = _FloatForm.Width;
  367. numberKeyboard.ForeColor = ForeColor;
  368. numberKeyboard.BackColor = BackColor;
  369. numberKeyboard.MaxValue = (Int64)1E9;
  370. numberKeyboard.MinValue = 0;
  371. numberKeyboard.ValueType = HexNumberKeyboard.HexValueType.Dec;
  372. numberKeyboard.Value = _RS232DecodePrsnt.BitRate;
  373. numberKeyboard.OkClick += (sender, args) =>
  374. {
  375. _RS232DecodePrsnt.BitRate = (UInt32)args.Data;
  376. this.BtnRealBitRate.Text = _RS232DecodePrsnt.BitRate.ToString();
  377. _FloatForm.Close();
  378. };
  379. numberKeyboard.CancelClick += (_, _) => _FloatForm.Close();
  380. _FloatForm.Controls.Add(numberKeyboard);
  381. _FloatForm.FormClosing += (_, _) =>
  382. {
  383. if (this.FindForm() is FloatForm form)
  384. form.CanClose = lastflag;
  385. };
  386. EventBus.EventBroker.Instance.GetEvent<Uni_Trend.MSO7000X.Common.Structs.FormEventArgs>().Publish(this, new Uni_Trend.MSO7000X.Common.Structs.FormEventArgs()
  387. {
  388. Current = _FloatForm,
  389. Type = Uni_Trend.MSO7000X.Common.Structs.FormType.InfoForm,
  390. });
  391. }
  392. private void SetThreshold()
  393. {
  394. Boolean lastflag = true;
  395. if (this.FindForm() is FloatForm form)
  396. {
  397. lastflag = form.CanClose;
  398. form.CanClose = false;
  399. }
  400. if (_FloatForm != null && !_FloatForm.IsDisposed)
  401. _FloatForm?.Close();
  402. _FloatForm = new Uni_Trend.MSO7000X.UserControls.FloatForm();
  403. _FloatForm.BackColor = BackColor;
  404. _FloatForm.ForeColor = ForeColor;
  405. _FloatForm.Width = 380;
  406. _FloatForm.Height = 460 + _FloatForm.HeadHeight;
  407. _FloatForm.Title = "设置门限";
  408. Uni_Trend.MSO7000X.UserControls.NumberKeyboard numberKeyboard = new Uni_Trend.MSO7000X.UserControls.NumberKeyboard();
  409. numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControl(x));
  410. numberKeyboard.Top = _FloatForm.HeadHeight;
  411. numberKeyboard.Height = _FloatForm.Height - _FloatForm.HeadHeight;
  412. numberKeyboard.Width = _FloatForm.Width;
  413. numberKeyboard.ForeColor = ForeColor;
  414. numberKeyboard.BackColor = BackColor;
  415. numberKeyboard.DefaultValue = _RS232DecodePrsnt.Threshold;
  416. numberKeyboard.Unit = "V";
  417. numberKeyboard.MinValue = -10;
  418. numberKeyboard.MaxValue = 10;
  419. numberKeyboard.DecimalNumber = 2;
  420. numberKeyboard.OkClickEvent += (sender, args) =>
  421. {
  422. this._RS232DecodePrsnt.Threshold = args.Data;
  423. this.BtnThreshold.Text = SIHelper.ValueChangeToSI(_RS232DecodePrsnt.Threshold, 2, "V");
  424. _FloatForm?.Close();
  425. };
  426. numberKeyboard.CancelEvent += (sender, args) => _FloatForm?.Close();
  427. _FloatForm.Controls.Add(numberKeyboard);
  428. _FloatForm.FormClosing += (_, _) =>
  429. {
  430. if (this.FindForm() is FloatForm form)
  431. form.CanClose = lastflag;
  432. };
  433. EventBus.EventBroker.Instance.GetEvent<Uni_Trend.MSO7000X.Common.Structs.FormEventArgs>().Publish(this, new Uni_Trend.MSO7000X.Common.Structs.FormEventArgs()
  434. {
  435. Current = _FloatForm,
  436. Type = Uni_Trend.MSO7000X.Common.Structs.FormType.InfoForm,
  437. });
  438. }
  439. private void SetRealBitRateVisibility(Boolean visibility = false)
  440. {
  441. this.BtnRealBitRate.Visible = visibility;
  442. this.LblRealBitRate.Visible = visibility;
  443. }
  444. public void UpdateView(String propertyName)
  445. {
  446. DecodeSynchronization(propertyName);
  447. if (String.IsNullOrEmpty(propertyName))
  448. {
  449. UpdateView();
  450. return;
  451. }
  452. this.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Where(x =>
  453. {
  454. UpdatePropertyAttribute attribute = x.GetCustomAttribute<UpdatePropertyAttribute>();
  455. return attribute != null && attribute.PropertyName == propertyName;
  456. }).FirstOrDefault()?.Invoke(this, null);
  457. }
  458. private void UpdateView()
  459. {
  460. this.GetType().GetMethods().Where(x => x.GetCustomAttribute<UpdatePropertyAttribute>() != null).ToList().ForEach(x => x?.Invoke(this, null));
  461. this.Refresh();
  462. }
  463. //private class ComboBoxData<T>
  464. //{
  465. // public String Key { get; set; }
  466. // public T Value { get; set; }
  467. // public Boolean Visible { get; set; }
  468. // public ComboBoxData(String key, T value, Boolean visible = true)
  469. // {
  470. // Key = key;
  471. // Value = value;
  472. // Visible = visible;
  473. // }
  474. //}
  475. //[AttributeUsage(AttributeTargets.Method)]
  476. //private class UpdatePropertyAttribute : Attribute
  477. //{
  478. // public UpdatePropertyAttribute(String propertyName) => this.PropertyName = propertyName;
  479. // public String PropertyName { get; set; }
  480. //}
  481. #region UpdateFunction
  482. [UpdateProperty(nameof(RS232DecodePrsnt.ChannelId))]
  483. private void UpdataChannelId()
  484. {
  485. this.CbxSource.SelectedValue = _RS232DecodePrsnt.ChannelId;
  486. }
  487. [UpdateProperty(nameof(RS232DecodePrsnt.ChannelL))]
  488. private void UpdateChannelL()
  489. {
  490. this.CbxSourceL.SelectedValue = _RS232DecodePrsnt.ChannelL;
  491. }
  492. [UpdateProperty(nameof(RS232DecodePrsnt.BitRate))]
  493. private void UpdateBitRate()
  494. {
  495. Int32 index = Enum.GetValues<ProtocolRS232.BPSList>().Select(x => x switch
  496. {
  497. ProtocolRS232.BPSList.BPSList_115200=>115200,
  498. ProtocolRS232.BPSList.BPSList_19200=>19200,
  499. ProtocolRS232.BPSList.BPSList_2400=>2400,
  500. ProtocolRS232.BPSList.BPSList_38400=>38400,
  501. ProtocolRS232.BPSList.BPSList_4800=>4800,
  502. ProtocolRS232.BPSList.BPSList_57600=>57600,
  503. ProtocolRS232.BPSList.BPSList_9600=>9600,
  504. _ => 0,
  505. }).ToList().FindIndex(x => x == _RS232DecodePrsnt.BitRate);
  506. if (index >= 0)
  507. {
  508. CbxBitRate.SelectedValue = Enum.GetValues<ProtocolRS232.BPSList>()[index];
  509. SetRealBitRateVisibility(false);
  510. }
  511. else
  512. {
  513. CbxBitRate.SelectedValue = ProtocolRS232.BPSList.BPSList_Custom;
  514. SetRealBitRateVisibility(true);
  515. }
  516. BtnRealBitRate.Text = _RS232DecodePrsnt.BitRate.ToString();
  517. }
  518. [UpdateProperty(nameof(RS232DecodePrsnt.ByteOrder))]
  519. private void UpdateByteOrder()
  520. {
  521. this.CbxByteOrder.ChoosedButtonIndex = Enum.GetValues<ProtocolRS232.MSB_LSB>().ToList().FindIndex(x => x == _RS232DecodePrsnt.ByteOrder);
  522. }
  523. [UpdateProperty(nameof(RS232DecodePrsnt.DataBits))]
  524. private void UpdateDataBits()
  525. {
  526. this.CbxDataBits.ChoosedButtonIndex = Enum.GetValues<ProtocolRS232.DataBitWidth>().ToList().FindIndex(x => x == _RS232DecodePrsnt.DataBits);
  527. }
  528. [UpdateProperty(nameof(RS232DecodePrsnt.Parity))]
  529. private void UpdateParity()
  530. {
  531. this.CbxParity.SelectedValue = _RS232DecodePrsnt.Parity;
  532. }
  533. [UpdateProperty(nameof(RS232DecodePrsnt.StopBits))]
  534. private void UpdateStopBits()
  535. {
  536. this.CbxStopBits.ChoosedButtonIndex = Enum.GetValues<ProtocolRS232.StopBit>().ToList().FindIndex(x => x == _RS232DecodePrsnt.StopBits);
  537. }
  538. [UpdateProperty(nameof(RS232DecodePrsnt.ChType))]
  539. private void UpdateChType()
  540. {
  541. this.CbxSignalType.SelectedValue = _RS232DecodePrsnt.ChType;
  542. }
  543. [UpdateProperty(nameof(RS232DecodePrsnt.Threshold))]
  544. private void UpdateThreshold()
  545. {
  546. this.BtnThreshold.Text = SIHelper.ValueChangeToSI(_RS232DecodePrsnt.Threshold, 2, "V");
  547. }
  548. [UpdateProperty(nameof(RS232DecodePrsnt.Polarity))]
  549. private void UpdatePolarity()
  550. {
  551. this.RbgPolarity.ChoosedButtonIndex = Enum.GetValues<ProtocolRS232.Polarity>().ToList().FindIndex(x => x == _RS232DecodePrsnt.Polarity);
  552. }
  553. #endregion UpdateFunction
  554. }
  555. [AttributeUsage(AttributeTargets.Field)]
  556. partial class RowColAttribute : Attribute
  557. {
  558. public RowColAttribute(Int32 row = 0, Int32 col = 0)
  559. {
  560. Row = row;
  561. Colnum = col;
  562. }
  563. public Int32 Row { get; set; }
  564. public Int32 Colnum { get; set; }
  565. public override Boolean Equals(Object obj)
  566. {
  567. if (obj == null)
  568. return false;
  569. if (!(obj is RowColAttribute attribute))
  570. return false;
  571. return attribute.Colnum == Colnum && attribute.Row == Row;
  572. }
  573. public override Int32 GetHashCode()
  574. {
  575. Int32 hashCode = Row.GetHashCode();
  576. if (Row.GetHashCode() != Colnum.GetHashCode())
  577. hashCode ^= Colnum.GetHashCode();
  578. return hashCode;
  579. }
  580. }
  581. }