123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- using System.Reflection;
- using System.Windows.Forms;
- using Uestc.Auto6.Dso.Core;
- using Uestc.Auto6.Dso.Core.Decode;
- using Uestc.Auto6.Dso.ComModel;
- using System;
- using System.Linq;
- using Uni_Trend.MSO7000X.Common.Helper;
- using Uni_Trend.MSO7000X.Language;
- using Uni_Trend.MSO7000X.UserControls;
- using System.Collections.Generic;
- namespace Uestc.Auto6.Dso.Protocol.LIN
- {
- [System.ComponentModel.ToolboxItem(false)]
- public partial class LINSetControl : UserControl, IProtocolView
- {
- #region 属性定义
- protected new Boolean DesignMode
- {
- get
- {
- Boolean rtnflag = false;
- #if DEBUG
- rtnflag = DesignTimeHelper.InDesignMode(this);
- #endif
- return rtnflag;
- }
- }
- public LINDecodePrsnt DestinDecodePrsnt
- {
- get => _DestinDecodePrsnt;
- set
- {
- if (_DestinDecodePrsnt != value)
- {
- _DestinDecodePrsnt = value;
- ///当有新的同步目标时,应先将目标中参数同步到本地<see cref="LINDecodePrsnt"/>属性中
- if (LINDecodePrsnt != null && value != null)
- {
- typeof(LINDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite&& x.CanRead).ToList().ForEach(x =>
- {
- x.SetValue(LINDecodePrsnt, x.GetValue(value));
- });
- }
- }
- }
- }
- private LINDecodePrsnt _DestinDecodePrsnt;
- private LINDecodePrsnt LINDecodePrsnt => Presenter as LINDecodePrsnt;
- public IProtocolPrsnt Presenter { get; set; }
- #endregion 属性定义
- public LINSetControl()
- {
- InitializeComponent();
- }
- protected override void OnLoad(EventArgs e)
- {
- base.OnLoad(e);
- InitView();
- Init();
- }
- private void InitView()
- {
- Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(this, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize);
- }
- private void Init()
- {
- GetDestinPrsnt();
- InitSource();
- InitBPS();
- InitCustomBPS();
- InitPolarity();
- InitSource();
- InitThreshold();
- InitStandard();
- InitDataCount();
- }
- private void InitSource()
- {
- CbxSource.DataSource = ChannelIdExt.GetAnalogs().GetEnumList();
- CbxSource.ValueMember = "Value";
- CbxSource.DisplayMember = "Key";
- CbxSource.SelectedValueChanged += (_, _) =>
- {
- LINDecodePrsnt.Source = (ChannelId)CbxSource.SelectedValue;
- };
- UpdateSource();
- }
- private void InitThreshold()
- {
- BtnThreshold.Click += (_, _) =>
- {
- SetThreshold();
- };
- UpdateThreshold();
- }
- private void InitPolarity()
- {
- RbgPolarity.ButtonItems = Enum.GetValues<ProtocolLIN.Polarity>().Select(x =>
- {
- return new RadioButtonItem()
- {
- Text = x.ToString(),
- Tag =x,
- };
- }).ToArray();
- RbgPolarity.IndexChanged += (_, _) =>
- {
- LINDecodePrsnt.Polarity = (ProtocolLIN.Polarity)RbgPolarity.ButtonItems[RbgPolarity.ChoosedButtonIndex].Tag;
- };
- }
- private void InitBPS()
- {
- CbxBPS.DataSource = Enum.GetValues<ProtocolLIN.BPS_ID>().Select(x =>
- {
- return new KeyValuePair<String, ProtocolLIN.BPS_ID>(x switch
- {
- ProtocolLIN.BPS_ID.BPS_2400 => "2400bps",
- ProtocolLIN.BPS_ID.BPS_4800 => "4800bps",
- ProtocolLIN.BPS_ID.BPS_9600 => "9600bps",
- ProtocolLIN.BPS_ID.BPS_19200 => "19200bps",
- ProtocolLIN.BPS_ID.BPS_Special => "Custom",
- _ => "Custom",
- }, x);
- }).ToList();
- CbxBPS.DisplayMember = "Key";
- CbxBPS.ValueMember = "Value";
- CbxBPS.SelectedValueChanged += (_, _) =>
- {
- LINDecodePrsnt.BPS = (ProtocolLIN.BPS_ID)CbxBPS.SelectedValue;
- };
- UpdateBPS();
- }
- private void InitCustomBPS()
- {
- BtnCustomBPS.Click += (_, _) =>
- {
- SetCustomBPS();
- };
- UpdateCustomBPS();
- }
- private void InitStandard()
- {
- CbxStandard.DataSource = Enum.GetValues<ProtocolLIN.Standard>().Select(x =>
- {
- return new KeyValuePair<String, ProtocolLIN.Standard>(x switch
- {
- ProtocolLIN.Standard.V1=>"LIN1.0",
- ProtocolLIN.Standard.V2=>"LIN2.0",
- _=>"LIN1.0",
- }, x);
- }).ToList();
- CbxStandard.DisplayMember = "Key";
- CbxStandard.ValueMember="Value";
- CbxStandard.SelectedValueChanged += (_, _) =>
- {
- LINDecodePrsnt.Standard = (ProtocolLIN.Standard)CbxStandard.SelectedValue;
- };
- UpdateStandard();
- }
- private void InitDataCount()
- {
- BtnDataCount.Click += (_, _) =>
- {
- SetDataCount();
- };
- UpdateDataCount();
- }
- private void SetDataCount()
- {
- Boolean lastflag = true;
- if (this.FindForm() is FloatForm form)
- {
- lastflag = form.CanClose;
- form.CanClose = false;
- }
- FloatForm floatForm = new FloatForm();
- floatForm = new Uni_Trend.MSO7000X.UserControls.FloatForm();
- floatForm.BackColor = BackColor;
- floatForm.ForeColor = ForeColor;
- floatForm.Width = 380;
- floatForm.Height = 460 + floatForm.HeadHeight;
- floatForm.Title = "设置字节数";
- Uni_Trend.MSO7000X.UserControls.NumberKeyboard numberKeyboard = new Uni_Trend.MSO7000X.UserControls.NumberKeyboard();
- numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControl(x));
- numberKeyboard.Top = floatForm.HeadHeight;
- numberKeyboard.Height = floatForm.Height - floatForm.HeadHeight;
- numberKeyboard.Width = floatForm.Width;
- numberKeyboard.ForeColor = ForeColor;
- numberKeyboard.BackColor = BackColor;
- numberKeyboard.DefaultValue = LINDecodePrsnt.DataCount;
- numberKeyboard.Unit = "";
- numberKeyboard.MinValue = LINDecodePrsnt.MinDataCount;
- numberKeyboard.MaxValue = LINDecodePrsnt.MaxDataCount;
- numberKeyboard.DecimalNumber = 2;
- numberKeyboard.OkClickEvent += (sender, args) =>
- {
- this.LINDecodePrsnt.DataCount = (Int32)args.Data;
- floatForm?.Close();
- };
- numberKeyboard.CancelEvent += (sender, args) => floatForm?.Close();
- floatForm.Controls.Add(numberKeyboard);
- floatForm.FormClosing += (_, _) =>
- {
- if (this.FindForm() is FloatForm form)
- form.CanClose = lastflag;
- };
- floatForm.ShowDialogByPosition();
- }
- private void SetCustomBPS()
- {
- Boolean lastflag = true;
- if (this.FindForm() is FloatForm form)
- {
- lastflag = form.CanClose;
- form.CanClose = false;
- }
- FloatForm floatForm = new FloatForm();
- floatForm = new Uni_Trend.MSO7000X.UserControls.FloatForm();
- floatForm.BackColor = BackColor;
- floatForm.ForeColor = ForeColor;
- floatForm.Width = 380;
- floatForm.Height = 460 + floatForm.HeadHeight;
- floatForm.Title = "设置波特率";
- Uni_Trend.MSO7000X.UserControls.NumberKeyboard numberKeyboard = new Uni_Trend.MSO7000X.UserControls.NumberKeyboard();
- numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControl(x));
- numberKeyboard.Top = floatForm.HeadHeight;
- numberKeyboard.Height = floatForm.Height - floatForm.HeadHeight;
- numberKeyboard.Width = floatForm.Width;
- numberKeyboard.ForeColor = ForeColor;
- numberKeyboard.BackColor = BackColor;
- numberKeyboard.DefaultValue = LINDecodePrsnt.CustomBPS;
- numberKeyboard.Unit = "bps";
- numberKeyboard.MinValue = LINDecodePrsnt.MinBPS;
- numberKeyboard.MaxValue = LINDecodePrsnt.MaxBPS;
- numberKeyboard.DecimalNumber = 2;
- numberKeyboard.OkClickEvent += (sender, args) =>
- {
- this.LINDecodePrsnt.CustomBPS = (Int32)args.Data;
- floatForm?.Close();
- };
- numberKeyboard.CancelEvent += (sender, args) => floatForm?.Close();
- floatForm.Controls.Add(numberKeyboard);
- floatForm.FormClosing += (_, _) =>
- {
- if (this.FindForm() is FloatForm form)
- form.CanClose = lastflag;
- };
- floatForm.ShowDialogByPosition();
- }
- private void SetThreshold()
- {
- Boolean lastflag = true;
- if (this.FindForm() is FloatForm form)
- {
- lastflag = form.CanClose;
- form.CanClose = false;
- }
- FloatForm floatForm = new FloatForm();
- floatForm = new Uni_Trend.MSO7000X.UserControls.FloatForm();
- floatForm.BackColor = BackColor;
- floatForm.ForeColor = ForeColor;
- floatForm.Width = 380;
- floatForm.Height = 460 + floatForm.HeadHeight;
- floatForm.Title = "设置门限";
- Uni_Trend.MSO7000X.UserControls.NumberKeyboard numberKeyboard = new Uni_Trend.MSO7000X.UserControls.NumberKeyboard();
- numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControl(x));
- numberKeyboard.Top = floatForm.HeadHeight;
- numberKeyboard.Height = floatForm.Height - floatForm.HeadHeight;
- numberKeyboard.Width = floatForm.Width;
- numberKeyboard.ForeColor = ForeColor;
- numberKeyboard.BackColor = BackColor;
- numberKeyboard.DefaultValue = LINDecodePrsnt.Threshold;
- numberKeyboard.Unit = "V";
- numberKeyboard.MinValue = LINDecodePrsnt.MinThreshold;
- numberKeyboard.MaxValue = LINDecodePrsnt.MaxThreshold;
- numberKeyboard.DecimalNumber = 2;
- numberKeyboard.OkClickEvent += (sender, args) =>
- {
- this.LINDecodePrsnt.Threshold = args.Data;
- this.BtnThreshold.Text = SIHelper.ValueChangeToSI(LINDecodePrsnt.Threshold, 2, "V");
- floatForm?.Close();
- };
- numberKeyboard.CancelEvent += (sender, args) => floatForm?.Close();
- floatForm.Controls.Add(numberKeyboard);
- floatForm.FormClosing += (_, _) =>
- {
- if (this.FindForm() is FloatForm form)
- form.CanClose = lastflag;
- };
- floatForm.ShowDialogByPosition();
- }
- /// <summary>
- /// 获取需要同步的Prsnt
- /// 当<see cref="LINDecodePrsnt"/>为<see cref="null"/>时表示不需要与其他Prsnt进行同步
- /// </summary>
- private void GetDestinPrsnt()
- {
- if (LINDecodePrsnt == null)
- return;
- if (LINDecodePrsnt.IsTrigger)
- DestinDecodePrsnt = Presenter.GetChannlesDecodePrsnt().Where(x => x is LINDecodePrsnt).Cast<LINDecodePrsnt>().Where(x => x.Source == LINDecodePrsnt.Source).OrderBy(x => x.Source).FirstOrDefault();
- else
- {
- if (Presenter.GetTriggerDecodePrsnt() is LINDecodePrsnt decodePrsnt && decodePrsnt.Source == LINDecodePrsnt.Source)
- DestinDecodePrsnt = decodePrsnt;
- else
- DestinDecodePrsnt = null;
- }
- }
- /// <summary>
- /// 同步两个Prsnt
- /// </summary>
- /// <param name="propertyName">属性名称</param>
- private void DecodeSynchronization(String propertyName)
- {
- if (DestinDecodePrsnt == null)
- return;
- ///由于使用了<see cref="ILINDecodePrsnt.SDA"/>进行判断具体同步的目标
- ///因此<see cref="ILINDecodePrsnt.SDA"/>参数不进行同步
- if (propertyName == nameof(LINDecodePrsnt.Source))
- return;
- if (String.IsNullOrEmpty(propertyName))
- {
- typeof(LINDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite&&x.CanRead).ToList().ForEach(x =>
- {
- x.SetValue(DestinDecodePrsnt, x.GetValue(LINDecodePrsnt));
- });
- }
- else
- {
- PropertyInfo propertyInfo = typeof(LINDecodePrsnt).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
- if (propertyInfo != null&&propertyInfo.CanWrite&&propertyInfo.CanRead)
- propertyInfo.SetValue(DestinDecodePrsnt, propertyInfo.GetValue(LINDecodePrsnt));
- }
- }
-
- public void UpdateView(Object presenter, String propertyName)
- {
- DecodeSynchronization(propertyName);
- if (String.IsNullOrEmpty(propertyName))
- {
- UpdateView();
- return;
- }
- this.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(x =>
- {
- var attr = x.GetCustomAttribute<UpdatePropertyAttribute>();
- return attr != null && attr.PropertyName == propertyName;
- }).FirstOrDefault()?.Invoke(this, null);
- }
- private void UpdateView()
- {
- this.GetType().GetMethods( BindingFlags.Public| BindingFlags.Instance| BindingFlags.NonPublic).Where(x => x.GetCustomAttribute<UpdatePropertyAttribute>() != null).ToList().ForEach(x => x?.Invoke(this, null));
- this.Refresh();
- }
- [UpdateProperty(nameof(Uestc.Auto6.Dso.Core.Decode.LINDecodePrsnt.Source))]
- private void UpdateSource()
- {
- CbxSource.SelectedValue = LINDecodePrsnt.Source;
- GetDestinPrsnt();
- }
- [UpdateProperty(nameof(Uestc.Auto6.Dso.Core.Decode.LINDecodePrsnt.Threshold))]
- private void UpdateThreshold()
- {
- this.BtnThreshold.Text = SIHelper.ValueChangeToSI(LINDecodePrsnt.Threshold, 2, "V");
- }
- [UpdateProperty(nameof(Uestc.Auto6.Dso.Core.Decode.LINDecodePrsnt.Polarity))]
- private void UpdatePolarity()
- {
- this.RbgPolarity.ChoosedButtonIndex = Enum.GetValues<ProtocolLIN.Polarity>().ToList().FindIndex(x => x == LINDecodePrsnt.Polarity);
- }
- [UpdateProperty(nameof(Uestc.Auto6.Dso.Core.Decode.LINDecodePrsnt.BPS))]
- private void UpdateBPS()
- {
- this.CbxBPS.SelectedValue = LINDecodePrsnt.BPS;
- BtnCustomBPS.Visible = LINDecodePrsnt.BPS == ProtocolLIN.BPS_ID.BPS_Special;
- LblCustomBPS.Visible = BtnCustomBPS.Visible;
- }
- [UpdateProperty(nameof(Uestc.Auto6.Dso.Core.Decode.LINDecodePrsnt.CustomBPS))]
- private void UpdateCustomBPS()
- {
- this.BtnCustomBPS.Text = LINDecodePrsnt.CustomBPS + "bps";
- }
- [UpdateProperty(nameof(Uestc.Auto6.Dso.Core.Decode.LINDecodePrsnt.Standard))]
- private void UpdateStandard()
- {
- this.CbxStandard.SelectedValue = LINDecodePrsnt.Standard;
- }
- [UpdateProperty(nameof(Uestc.Auto6.Dso.Core.Decode.LINDecodePrsnt.DataCount))]
- private void UpdateDataCount()
- {
- this.BtnDataCount.Text = LINDecodePrsnt.DataCount + "";
- }
- }
- }
|