123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- using System;
- using System.Linq;
- using System.Reflection;
- using System.Windows.Forms;
- using Uestc.Auto6.Dso.Core;
- using Uestc.Auto6.Dso.Core.Decode;
- using Uni_Trend.MSO7000X.Common.Helper;
- using Uni_Trend.MSO7000X.UserControls;
- using Uni_Trend.MSO7000X.Language;
- namespace Uestc.Auto6.Dso.Protocol.SENT
- {
- [System.ComponentModel.ToolboxItem(false)]
- public partial class SENTSetControl : UserControl, IProtocolView
- {
- #region 属性定义
- public SENTSetControl()
- {
- InitializeComponent();
- }
- protected override void OnLoad(EventArgs e)
- {
- base.OnLoad(e);
- Init();
- }
- protected new Boolean DesignMode
- {
- get
- {
- Boolean rtnflag = false;
- #if DEBUG
- rtnflag = DesignTimeHelper.InDesignMode(this);
- #endif
- return rtnflag;
- }
- }
- FloatForm _FloatForm;
- private SENTDecodePrsnt DestinDecodePrsnt
- {
- get => _DestinDecodePrsnt;
- set
- {
- if (_DestinDecodePrsnt != value)
- {
- _DestinDecodePrsnt = value;
- ///当有新的同步目标时,应先将目标中参数同步到本地<see cref="SENTDecodePrsnt"/>属性中
- if (SENTDecodePrsnt != null && value != null)
- {
- typeof(SENTDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite&&x.CanRead).ToList().ForEach(x =>
- {
- x.SetValue(SENTDecodePrsnt, x.GetValue(value));
- });
- }
- }
- }
- }
- private SENTDecodePrsnt _DestinDecodePrsnt;
- private SENTDecodePrsnt SENTDecodePrsnt => Presenter as SENTDecodePrsnt;
- public IProtocolPrsnt Presenter { get; set; }
- #endregion 属性定义
-
- private void Init()
- {
- GetDestinPrsnt();
- Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControl(x, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize));
- SetRealRateVisible(false);
- InitSource();
- InitThreshold();
- InitPolarity();
- InitDataLength();
- InitCRC();
- InitSignalRate();
- InitRealSignalRate();
- InitAinTerval();
- }
- private void InitSource()
- {
- this.CbxSource.DataSource =ComModel.ChannelIdExt.GetAnalogs().GetEnumList();
- this.CbxSource.ValueMember = "Value";
- this.CbxSource.DisplayMember = "Key";
- this.CbxSource.SelectedValueChanged += (_, _) =>
- {
- this.SENTDecodePrsnt.Source = (ComModel.ChannelId)this.CbxSource.SelectedValue;
- };
- SetSource();
- }
- private void InitThreshold()
- {
- SetThreshold();
- this.BtnThreshold.Click += (_, _) =>
- {
- Boolean lastflag = true;
- if (this.FindForm() is FloatForm form)
- {
- lastflag = form.CanClose;
- form.CanClose = false;
- }
- if (_FloatForm != null && !_FloatForm.IsDisposed)
- _FloatForm?.Close();
- _FloatForm = new FloatForm();
- _FloatForm.BackColor = BackColor;
- _FloatForm.ForeColor = ForeColor;
- _FloatForm.Title = "设置阈值";
- _FloatForm.Width = 380;
- _FloatForm.Height = 500 + _FloatForm.HeadHeight;
- NumberKeyboard numberKeyboard = new 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.MaxValue = 12;
- numberKeyboard.MinValue = -12;
- numberKeyboard.Unit = "V";
- numberKeyboard.DefaultValue = SENTDecodePrsnt.Threshold;
- numberKeyboard.OkClickEvent += (sender, args) =>
- {
- SENTDecodePrsnt.Threshold = args.Data;
- _FloatForm.Close();
- };
- numberKeyboard.CancelEvent += (_, _) => _FloatForm.Close();
- _FloatForm.Controls.Add(numberKeyboard);
- _FloatForm.FormClosing += (_, _) =>
- {
- if (this.FindForm() is FloatForm form)
- form.CanClose = lastflag;
- };
- _FloatForm.ShowDialogByPosition();
- };
- }
- private void InitPolarity()
- {
- this.RbgPolarity.ButtonItems = Enum.GetValues<ComModel.ProtocolSENT.Polarity>().Select(x =>
- {
- return new RadioButtonItem()
- {
- Tag =x,
- Text =x.GetDescription(),
- };
- }).ToArray();
- this.RbgPolarity.IndexChanged += (_, _) =>
- {
- this.SENTDecodePrsnt.Polarity = (ComModel.ProtocolSENT.Polarity)this.RbgPolarity.ButtonItems[this.RbgPolarity.ChoosedButtonIndex].Tag;
- };
- SetPolarity();
- }
- private void InitDataLength()
- {
- this.RbgDataLength.ButtonItems = Enum.GetValues<ComModel.ProtocolSENT.DataLength>().Select(x =>
- {
- return new RadioButtonItem()
- {
- Tag = x,
- Text = x switch
- {
- ComModel.ProtocolSENT.DataLength.Nibbles_5=>"5",
- ComModel.ProtocolSENT.DataLength.Nibbles_8=>"8",
- _=>"5",
- },
- };
- }).ToArray();
- this.RbgDataLength.IndexChanged += (_, _) =>
- {
- this.SENTDecodePrsnt.DataLength = (ComModel.ProtocolSENT.DataLength)this.RbgDataLength.ButtonItems[this.RbgDataLength.ChoosedButtonIndex].Tag;
- };
- SetDataLength();
- }
- private void InitCRC()
- {
- this.RbgCRC.ButtonItems = Enum.GetValues<ComModel.ProtocolSENT.SENTCRC>().Select(x =>
- {
- return new RadioButtonItem()
- {
- Tag = x,
- Text = x.GetDescription(),
- };
- }).ToArray();
- this.RbgCRC.IndexChanged += (_, _) =>
- {
- this.SENTDecodePrsnt.CRC = (ComModel.ProtocolSENT.SENTCRC)this.RbgCRC.ButtonItems[this.RbgCRC.ChoosedButtonIndex].Tag;
- };
- SetCRC();
- }
- private void InitSignalRate()
- {
- this.CbxSignalRate.DataSource = Enum.GetValues<ComModel.ProtocolSENT.SignalRate>().Select(x =>
- {
- return new System.Collections.Generic.KeyValuePair<string, ComModel.ProtocolSENT.SignalRate>(x == ComModel.ProtocolSENT.SignalRate.SignalRate_custom?"Custom":SIHelper.ValueChangeToSI(GetRealRate(x),2,"bps"),x);
- }).ToList();
- this.CbxSignalRate.ValueMember = "Value";
- this.CbxSignalRate.DisplayMember = "Key";
- this.CbxSignalRate.SelectedValueChanged += (_, _) =>
- {
- this.SENTDecodePrsnt.SignalRate = (ComModel.ProtocolSENT.SignalRate)this.CbxSignalRate.SelectedValue;
- };
- SetSignalRate();
- }
- private void InitRealSignalRate()
- {
- SetRealSignalRate();
- this.BtnRealSignalRate.Click += (_, _) =>
- {
- Boolean lastflag = true;
- if (this.FindForm() is FloatForm form)
- {
- lastflag = form.CanClose;
- form.CanClose = false;
- }
- if (_FloatForm != null && !_FloatForm.IsDisposed)
- _FloatForm?.Close();
- _FloatForm = new FloatForm();
- _FloatForm.BackColor = BackColor;
- _FloatForm.ForeColor = ForeColor;
- _FloatForm.Title = "设置波特率";
- _FloatForm.Width = 380;
- _FloatForm.Height = 500 + _FloatForm.HeadHeight;
- NumberKeyboard numberKeyboard = new 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.MaxValue = 10E6;
- numberKeyboard.MinValue = 10E3;
- numberKeyboard.Unit = "bps";
- numberKeyboard.DecimalNumber = 0;
- numberKeyboard.DefaultValue = SENTDecodePrsnt.RealSignalRate;
- numberKeyboard.OkClickEvent += (sender, args) =>
- {
- SENTDecodePrsnt.RealSignalRate = (Int64)args.Data;
- _FloatForm.Close();
- };
- numberKeyboard.CancelEvent += (_, _) => _FloatForm.Close();
- _FloatForm.Controls.Add(numberKeyboard);
- _FloatForm.FormClosing += (_, _) =>
- {
- if (this.FindForm() is FloatForm form)
- form.CanClose = lastflag;
- };
- _FloatForm.ShowDialogByPosition();
- };
- }
- private void InitAinTerval()
- {
- this.RbgAinTerval.ButtonItems = Enum.GetValues<ComModel.ProtocolSENT.AppendInterval>().Select(x =>
- {
- return new RadioButtonItem()
- {
- Tag = x,
- Text = x.GetDescription(),
- };
- }).ToArray();
- this.RbgAinTerval.IndexChanged += (_, _) =>
- {
- SENTDecodePrsnt.Interval = (ComModel.ProtocolSENT.AppendInterval)this.RbgAinTerval.ButtonItems[this.RbgAinTerval.ChoosedButtonIndex].Tag;
- };
- SetAinTerval();
- }
- /// <summary>
- /// 获取需要同步的Prsnt
- /// 当<see cref="DestinDecodePrsnt"/>为<see cref="null"/>时表示不需要与其他Prsnt进行同步
- /// 当<see cref="SENTDecodePrsnt.Source"/> 相同时进行同步
- /// </summary>
- private void GetDestinPrsnt()
- {
- if (this.SENTDecodePrsnt == null)
- return;
- if (SENTDecodePrsnt.IsTrigger)
- DestinDecodePrsnt = Presenter.GetChannlesDecodePrsnt().Where(x => x is SENTDecodePrsnt).Cast<SENTDecodePrsnt>().Where(x => CheckRules(x, SENTDecodePrsnt)).OrderBy(x => x.Source).FirstOrDefault();
- else
- {
- if (Presenter.GetTriggerDecodePrsnt() is SENTDecodePrsnt decodePrsnt && CheckRules(decodePrsnt, SENTDecodePrsnt))
- DestinDecodePrsnt = decodePrsnt;
- else
- DestinDecodePrsnt = null;
- }
- }
- /// 判断两个Prsnt是否同步的规则
- /// </summary>
- /// <param name="fisrt"></param>
- /// <param name="second"></param>
- /// <returns></returns>
- private Boolean CheckRules(SENTDecodePrsnt fisrt, SENTDecodePrsnt second)
- {
- if (fisrt == null || second == null)
- return false;
- return fisrt.Source == second.Source;
- }
- public void UpdateView(Object presenter, String propertyName)
- {
- DecodeSynchronization(propertyName);
- if (String.IsNullOrEmpty(propertyName))
- {
- UpdateView();
- return;
- }
- this.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Where(x =>
- {
- UpdatePropertyAttribute attribute = x.GetCustomAttribute<UpdatePropertyAttribute>();
- return attribute != null && attribute.PropertyName == propertyName;
- }).FirstOrDefault()?.Invoke(this, null);
- }
- private void UpdateView()
- {
- this.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Where(x => x.GetCustomAttribute<UpdatePropertyAttribute>() != null).ToList().ForEach(x => x?.Invoke(this, null));
- this.Refresh();
- }
- /// <summary>
- /// 同步两个Prsnt
- /// </summary>
- /// <param name="propertyName">属性名称</param>
- private void DecodeSynchronization(String propertyName)
- {
- if (DestinDecodePrsnt == null)
- return;
- ///由于使用了<see cref="SENTDecodePrsnt.Source"/>进行判断具体同步的目标
- ///因此<see cref="SENTDecodePrsnt.Source"/>参数不进行同步
- if (propertyName == nameof(Core.Decode.SENTDecodePrsnt.Source))
- return;
- if (String.IsNullOrEmpty(propertyName))
- {
- typeof(SENTDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite&&x.CanRead).ToList().ForEach(x =>
- {
- x.SetValue(DestinDecodePrsnt, x.GetValue(this.SENTDecodePrsnt));
- });
- }
- else
- {
- PropertyInfo propertyInfo = typeof(SENTDecodePrsnt).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
- if (propertyInfo != null&&propertyInfo.CanWrite&&propertyInfo.CanRead)
- propertyInfo.SetValue(DestinDecodePrsnt, propertyInfo.GetValue(this.SENTDecodePrsnt));
- }
- }
- [UpdateProperty(nameof(Uestc.Auto6.Dso.Core.Decode.SENTDecodePrsnt.Source))]
- private void SetSource()
- {
- this.CbxSource.SelectedValue = SENTDecodePrsnt.Source;
- GetDestinPrsnt();
- }
- [UpdateProperty(nameof(Core.Decode.SENTDecodePrsnt.Threshold))]
- private void SetThreshold()
- {
- this.BtnThreshold.Text = SIHelper.ValueChangeToSI(SENTDecodePrsnt.Threshold, 2, "V");
- }
- [UpdateProperty(nameof(Core.Decode.SENTDecodePrsnt.Polarity))]
- private void SetPolarity()
- {
- this.RbgPolarity.ChoosedButtonIndex = Enum.GetValues<ComModel.ProtocolSENT.Polarity>().ToList().FindIndex(x => x == SENTDecodePrsnt.Polarity);
- }
- [UpdateProperty(nameof(Core.Decode.SENTDecodePrsnt.DataLength))]
- private void SetDataLength()
- {
- this.RbgDataLength.ChoosedButtonIndex = Enum.GetValues<ComModel.ProtocolSENT.DataLength>().ToList().FindIndex(x => x == SENTDecodePrsnt.DataLength);
- }
- [UpdateProperty(nameof(Core.Decode.SENTDecodePrsnt.CRC))]
- private void SetCRC()
- {
- this.RbgCRC.ChoosedButtonIndex = Enum.GetValues<ComModel.ProtocolSENT.SENTCRC>().ToList().FindIndex(x => x == SENTDecodePrsnt.CRC);
- }
- [UpdateProperty(nameof(Core.Decode.SENTDecodePrsnt.SignalRate))]
- private void SetSignalRate()
- {
- this.CbxSignalRate.SelectedValue = this.SENTDecodePrsnt.SignalRate;
- SetRealRateVisible(this.SENTDecodePrsnt.SignalRate == ComModel.ProtocolSENT.SignalRate.SignalRate_custom);
- if (this.SENTDecodePrsnt.SignalRate != ComModel.ProtocolSENT.SignalRate.SignalRate_custom)
- this.SENTDecodePrsnt.RealSignalRate = GetRealRate(SENTDecodePrsnt.SignalRate);
- }
- [UpdateProperty(nameof(Core.Decode.SENTDecodePrsnt.RealSignalRate))]
- private void SetRealSignalRate()
- {
- this.BtnRealSignalRate.Text = SIHelper.ValueChangeToSI(this.SENTDecodePrsnt.RealSignalRate, 2, "bps");
- Int32 index = Enum.GetValues<ComModel.ProtocolSENT.SignalRate>().ToList().FindIndex(x => GetRealRate(x) == SENTDecodePrsnt.RealSignalRate);
- if (index != -1)
- this.SENTDecodePrsnt.SignalRate = Enum.GetValues<ComModel.ProtocolSENT.SignalRate>().ToList()[index];
- }
- [UpdateProperty(nameof(Core.Decode.SENTDecodePrsnt.Interval))]
- private void SetAinTerval()
- {
- this.RbgAinTerval.ChoosedButtonIndex = Enum.GetValues<ComModel.ProtocolSENT.AppendInterval>().ToList().FindIndex(x => x == this.SENTDecodePrsnt.Interval);
- }
- private void SetRealRateVisible(Boolean visible = false)
- {
- this.LblRealSignalRate.Visible = visible;
- this.BtnRealSignalRate.Visible = this.LblRealSignalRate.Visible;
- }
- private Int64 GetRealRate(ComModel.ProtocolSENT.SignalRate rate)
- {
- return rate switch
- {
- ComModel.ProtocolSENT.SignalRate.SignalRate_100k => 100000,
- ComModel.ProtocolSENT.SignalRate.SignalRate_10k => 10000,
- ComModel.ProtocolSENT.SignalRate.SignalRate_125k => 125000,
- ComModel.ProtocolSENT.SignalRate.SignalRate_1m => 1000000,
- ComModel.ProtocolSENT.SignalRate.SignalRate_20k => 20000,
- ComModel.ProtocolSENT.SignalRate.SignalRate_33_3k => 33300,
- ComModel.ProtocolSENT.SignalRate.SignalRate_50k => 50000,
- ComModel.ProtocolSENT.SignalRate.SignalRate_62_5k => 62500,
- ComModel.ProtocolSENT.SignalRate.SignalRate_83_3k => 83300,
- _ => -1,
- };
- }
- }
- }
|