RS232TriggerSetControl.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using Uestc.Auto6.Dso.Core;
  11. using Uestc.Auto6.Dso.Core.Decode;
  12. using Uestc.Auto6.Dso.ComModel;
  13. using Uni_Trend.MSO7000X.Common.Helper;
  14. using Uni_Trend.MSO7000X.Common.Structs;
  15. using Uni_Trend.MSO7000X.UserControls;
  16. using Uni_Trend.MSO7000X.Language;
  17. namespace Uestc.Auto6.Dso.Protocol.RS232
  18. {
  19. [System.ComponentModel.ToolboxItem(false)]
  20. public partial class RS232TriggerSetControl : UserControl, ITriggerSerialView
  21. {
  22. public RS232TriggerSetControl()
  23. {
  24. InitializeComponent();
  25. InitView();
  26. this.Load += (_, _) =>
  27. {
  28. Init();
  29. this.BtnValue.Click += (_, _) => Action?.Invoke();
  30. SetControl(Presenter.Conditions);
  31. this.SizeChanged += (_, _) => SetControl(Presenter.Conditions);
  32. };
  33. }
  34. private void InitView()
  35. {
  36. Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(this, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize);
  37. }
  38. protected new Boolean DesignMode
  39. {
  40. get
  41. {
  42. Boolean rtnflag = false;
  43. #if DEBUG
  44. rtnflag = DesignTimeHelper.InDesignMode(this);
  45. #endif
  46. return rtnflag;
  47. }
  48. }
  49. private Int32 _RowHeight = 76;
  50. private void SetControl(ProtocolRS232.Conditions conditions)
  51. {
  52. switch (conditions)
  53. {
  54. case ProtocolRS232.Conditions.SpecialData:
  55. DataAutoLayout();
  56. break;
  57. case ProtocolRS232.Conditions.PackageEnd:
  58. EOPAutoLayout();
  59. break;
  60. case ProtocolRS232.Conditions.FrameStart:
  61. case ProtocolRS232.Conditions.CheckError:
  62. StartAutoLayout();
  63. break;
  64. }
  65. }
  66. private Action Action;
  67. private void EditData()
  68. {
  69. Boolean lastflag = true;
  70. if(this.FindForm() is FloatForm form)
  71. {
  72. lastflag = form.CanClose;
  73. form.CanClose = false;
  74. }
  75. FloatForm floatForm = new FloatForm();
  76. floatForm.BackColor = BackColor;
  77. floatForm.ForeColor = ForeColor;
  78. floatForm.Title = "设置数据";
  79. floatForm.Width = 380;
  80. floatForm.Height = 500 + floatForm.HeadHeight;
  81. HexNumberKeyboard numberKeyboard = new HexNumberKeyboard();
  82. Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(numberKeyboard, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize);
  83. //numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(x, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize));
  84. //numberKeyboard.Dock = DockStyle.Fill;
  85. numberKeyboard.Top = floatForm.HeadHeight;
  86. numberKeyboard.Height = floatForm.Height - floatForm.HeadHeight;
  87. numberKeyboard.Width = floatForm.Width;
  88. numberKeyboard.ForeColor = ForeColor;
  89. numberKeyboard.BackColor = BackColor;
  90. numberKeyboard.MaxValue = 0xFF;
  91. numberKeyboard.MinValue = 0x00;
  92. numberKeyboard.ValueType = Uni_Trend.MSO7000X.UserControls.HexNumberKeyboard.HexValueType.Hex;
  93. numberKeyboard.Value = Presenter.Data;
  94. numberKeyboard.OkClick += (sender, args) =>
  95. {
  96. Presenter.Data = (Char)args.Data;
  97. this.BtnValue.Text = "0x" + String.Format("{0:X2}", (Int32)Presenter.Data);
  98. floatForm.Close();
  99. };
  100. numberKeyboard.CancelClick += (_, _) => floatForm.Close();
  101. floatForm.FormClosing += (_, _) =>
  102. {
  103. if (this.FindForm() is FloatForm form)
  104. form.CanClose = lastflag;
  105. };
  106. floatForm.Controls.Add(numberKeyboard);
  107. floatForm.ShowDialogByPosition();
  108. }
  109. private void EditEOP()
  110. {
  111. FloatForm floatForm = new FloatForm();
  112. Boolean lastflag = true;
  113. if (this.FindForm() is FloatForm form)
  114. {
  115. lastflag = form.CanClose;
  116. form.CanClose = false;
  117. }
  118. floatForm.BackColor = BackColor;
  119. floatForm.ForeColor = ForeColor;
  120. floatForm.Title = "设置EOP";
  121. floatForm.Width = 380;
  122. floatForm.Height = 500 + floatForm.HeadHeight;
  123. HexNumberKeyboard numberKeyboard = new HexNumberKeyboard();
  124. //numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControl(x));
  125. //numberKeyboard.Dock = DockStyle.Fill;
  126. numberKeyboard.Top = floatForm.HeadHeight;
  127. numberKeyboard.Height = floatForm.Height - floatForm.HeadHeight;
  128. numberKeyboard.Width = floatForm.Width;
  129. numberKeyboard.ForeColor = ForeColor;
  130. numberKeyboard.BackColor = BackColor;
  131. numberKeyboard.MaxValue = 0xFF;
  132. numberKeyboard.MinValue = 0x00;
  133. numberKeyboard.ValueType = Uni_Trend.MSO7000X.UserControls.HexNumberKeyboard.HexValueType.Hex;
  134. numberKeyboard.Value = Presenter.EOPChar;
  135. numberKeyboard.OkClick += (sender, args) =>
  136. {
  137. Presenter.EOPChar = (Char)args.Data;
  138. String charstring = ASCIIConverter.GetASCIIStr(Presenter.EOPChar);
  139. if(String.IsNullOrEmpty(charstring))
  140. this.BtnValue.Text = "0x" + String.Format("{0:X2}", (Int32)Presenter.EOPChar);
  141. else
  142. this.BtnValue.Text = "0x" + String.Format("{0:X2}({1})", (Int32)Presenter.EOPChar, charstring);
  143. floatForm.Close();
  144. };
  145. numberKeyboard.CancelClick += (_, _) => floatForm.Close();
  146. floatForm.FormClosing += (_, _) =>
  147. {
  148. if (this.FindForm() is FloatForm form)
  149. form.CanClose = lastflag;
  150. };
  151. floatForm.Controls.Add(numberKeyboard);
  152. floatForm.ShowDialogByPosition();
  153. }
  154. private void Init()
  155. {
  156. SetControl(Presenter.Conditions);
  157. this.CbxCompare.DataSource = Enum.GetValues<PulseCondition>().Select(x => new KeyValuePair<string, PulseCondition>(x.GetDescription(), x)).ToList();
  158. this.CbxCompare.DisplayMember = "Key";
  159. this.CbxCompare.ValueMember = "Value";
  160. this.CbxCompare.SelectedValueChanged += (sender, args) =>
  161. {
  162. if (ParentForm == null)
  163. return;
  164. Presenter.Compare = (PulseCondition)this.CbxCompare.SelectedValue;
  165. };
  166. this.CbxCompare.SelectedValue = Presenter.Compare;
  167. }
  168. public RS232TrigSerialPrsnt Presenter
  169. {
  170. get => (RS232TrigSerialPrsnt)(ParentForm as ITriggerView).Presenter;
  171. set => (ParentForm as ITriggerView).Presenter = value;
  172. }
  173. ITriggerPrsnt IView<ITriggerPrsnt>.Presenter
  174. {
  175. get => Presenter;
  176. set => Presenter = (RS232TrigSerialPrsnt)value;
  177. }
  178. private void EOPAutoLayout()
  179. {
  180. this.LblValue.Text = "结束标志";
  181. String charstring = ASCIIConverter.GetASCIIStr(Presenter.EOPChar);
  182. if (String.IsNullOrEmpty(charstring))
  183. this.BtnValue.Text = "0x" + String.Format("{0:X2}", (Int32)Presenter.EOPChar);
  184. else
  185. this.BtnValue.Text = "0x" + String.Format("{0:X2}({1})", (Int32)Presenter.EOPChar, charstring);
  186. this.LblValue.Visible = true;
  187. this.BtnValue.Visible = true;
  188. this.LblCompare.Visible = false;
  189. this.CbxCompare.Visible = false;
  190. SetControlPosition(BtnValue, LblValue, 0, 0);
  191. this.Height = BtnValue.Bottom;
  192. Visible = true;
  193. Action = () => EditEOP();
  194. }
  195. private void StartAutoLayout()
  196. {
  197. this.LblValue.Visible = false;
  198. this.BtnValue.Visible = false;
  199. this.CbxCompare.Visible = false;
  200. this.LblCompare.Visible = false;
  201. Height = 0;
  202. Visible = false;
  203. Action = null;
  204. }
  205. private void DataAutoLayout()
  206. {
  207. this.LblValue.Text = "数据(Hex)";
  208. this.BtnValue.Text = "0x" + String.Format("{0:X2}", (Byte)Presenter.Data);
  209. this.BtnValue.Visible = true;
  210. this.LblValue.Visible = true;
  211. this.CbxCompare.Visible = true;
  212. this.LblCompare.Visible = true;
  213. SetControlPosition(CbxCompare,LblCompare, 0, 0);
  214. SetControlPosition(BtnValue,LblValue, 0, 1);
  215. this.Height = BtnValue.Bottom;
  216. Visible = true;
  217. Action = () => EditData();
  218. }
  219. private void SetControlPosition<TControl>(TControl control, UestcLabel label, Int32 row = 0, Int32 col = 0) where TControl : Control
  220. {
  221. label.Height = label.Font.Height;
  222. Int32 tempheight = control.Height + label.Height;
  223. label.Left = Width / 2 * col + 10;
  224. label.Top = row * _RowHeight + (_RowHeight - tempheight) / 2;
  225. control.Left = label.Left;
  226. control.Top = label.Bottom + 6;
  227. }
  228. public void UpdateView(Object presenter, String propertyName)
  229. {
  230. if(String.IsNullOrEmpty(propertyName))
  231. {
  232. UpdateView();
  233. return;
  234. }
  235. switch(propertyName)
  236. {
  237. case nameof(RS232TrigSerialPrsnt.Compare):
  238. if(Presenter.Conditions == ProtocolRS232.Conditions.SpecialData)
  239. CbxCompare.SelectedValue = Presenter.Compare;
  240. break;
  241. case nameof(RS232TrigSerialPrsnt.Data):
  242. if(Presenter.Conditions == ProtocolRS232.Conditions.SpecialData)
  243. BtnValue.Text = "0x" + String.Format("{0:X2}", (Byte)Presenter.Data);
  244. break;
  245. //case nameof(RS232TrigSerialPrsnt.DataIndex):
  246. // if (Presenter.RS232Conditions == ProtocolRS232.Conditions.SpecialData) CbxDataIndex.SelectedValue = Presenter.DataIndex;
  247. // break;
  248. case nameof(RS232TrigSerialPrsnt.EOPChar):
  249. if(Presenter.Conditions == ProtocolRS232.Conditions.CheckError)
  250. {
  251. String charstring = ASCIIConverter.GetASCIIStr(Presenter.EOPChar);
  252. if (String.IsNullOrEmpty(charstring))
  253. this.BtnValue.Text = "0x" + String.Format("{0:X2}", (Int32)Presenter.EOPChar);
  254. else
  255. this.BtnValue.Text = "0x" + String.Format("{0:X2}({1})", (Int32)Presenter.EOPChar, charstring);
  256. }
  257. break;
  258. case nameof(RS232TrigSerialPrsnt.Conditions):
  259. SetControl(Presenter.Conditions);
  260. break;
  261. default:
  262. break;
  263. }
  264. }
  265. private void UpdateView()
  266. {
  267. }
  268. }
  269. }