using System; using System.Windows.Forms; using Uni_Trend.MSO7000X.Common.Helper; using System.Linq; using System.Collections; using System.Collections.Generic; using Uni_Trend.MSO7000X.UserControls; using Uestc.Auto6.Dso.Core; using Uestc.Auto6.Dso.ComModel; using Uestc.Auto6.Dso.Core.Decode; using Uni_Trend.MSO7000X.Language; using System.Reflection; namespace Uestc.Auto6.Dso.Protocol.RS232 { public partial class RS232SetControl : UserControl, IProtocolView { public RS232SetControl() { InitializeComponent(); InitView(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); Init(); } private void InitView() { Int32 rowcount = 1; Controls.Cast().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControl(x)); this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(x => x.GetCustomAttribute() != null && x.GetValue(this) is Control).Select(x => { RowColAttribute attribute = x.GetCustomAttribute(); Control control = x.GetValue(this) as Control; return (attribute, control); }).GroupBy(x => x.attribute, (x => x.control)).ToList().ForEach(x => { Control lable = x.First(x => x is UestcLabel); Control control = x.First(x => !(x is UestcLabel)); lable.Left = Width / _ColnumCount * x.Key.Colnum; lable.Width = control.Width; lable.Height = lable.Font.Height; lable.Top = 10 + _RowHeight * x.Key.Row; control.Left = lable.Left; control.Top = lable.Bottom + 2; lable.Width = 120; if (control is UIRadioButtonGroup) control.Width = 140; else control.Width = 120; rowcount = Math.Max(rowcount, x.Key.Row + 1); }); Height = rowcount * _RowHeight; } private void Init() { GetDestinPrsnt(); InitSignalType(); InitSource(); InitSourceL(); InitBiteRate(); InitRealBiteRate(); InitDataBits(); InitByteOrder(); InitStopBits(); InitParity(); InitThreshold(); InitPolarity(); } /// /// 获取需要同步的Prsnt /// 当时表示不需要与其他Prsnt进行同步 /// private void GetDestinPrsnt() { if (_RS232DecodePrsnt == null) return; if (_RS232DecodePrsnt.IsTrigger) DestinDecodePrsnt = Presenter.GetChannlesDecodePrsnt().Where(x => x is RS232DecodePrsnt).Cast().Where(x => x.ChannelId == _RS232DecodePrsnt.ChannelId).OrderBy(x => x.ChannelL).FirstOrDefault(); else { if (Presenter.GetTriggerDecodePrsnt() is RS232DecodePrsnt rs232decodePrsnt && rs232decodePrsnt.ChannelId == _RS232DecodePrsnt.ChannelId) DestinDecodePrsnt = rs232decodePrsnt; else DestinDecodePrsnt = null; } } #region 属性定义 protected new Boolean DesignMode { get { Boolean rtnflag = false; #if DEBUG rtnflag = DesignTimeHelper.InDesignMode(this); #endif return rtnflag; } } private RS232DecodePrsnt DestinDecodePrsnt { get => _DestinDecodePrsnt; set { if (_DestinDecodePrsnt != value) { _DestinDecodePrsnt = value; ///当有新的同步目标时,应先将目标中参数同步到本地属性中 if (_RS232DecodePrsnt != null && value != null) { typeof(RS232DecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x=>x.CanWrite).ToList().ForEach(x => { x.SetValue(_RS232DecodePrsnt, x.GetValue(value)); }); } } } } private RS232DecodePrsnt _DestinDecodePrsnt; private RS232DecodePrsnt _RS232DecodePrsnt => Presenter as RS232DecodePrsnt; public IProtocolPrsnt Presenter { get; set; } private FloatForm _FloatForm; private Int32 _RowHeight = 56; private Int32 _ColnumCount = 3; #endregion 属性定义 #region 控件初始化 private void InitSourceL() { this.CbxSourceL.DataSource = ChannelIdExt.GetAnalogs().Select(x => new ComboBoxData(x.GetEnumValueResourceProperty(), x, x != _RS232DecodePrsnt.ChannelId)).ToList(); this.CbxSourceL.DisplayMember = "Key"; this.CbxSourceL.ValueMember = "Value"; this.CbxSourceL.SelectedValueChanged += (_, _) => { _RS232DecodePrsnt.ChannelL = (ChannelId)this.CbxSourceL.SelectedValue; }; this.CbxSourceL.SelectedValue = _RS232DecodePrsnt.ChannelL; } private void InitPolarity() { this.RbgPolarity.ButtonItems = Enum.GetValues().Select(x => new RadioButtonItem() { Tag = x, Text = x.GetDescription(), }).ToArray(); this.RbgPolarity.IndexChanged += (sender, args) => { _RS232DecodePrsnt.Polarity = (ProtocolRS232.Polarity)RbgPolarity.ButtonItems[RbgPolarity.ChoosedButtonIndex].Tag; }; this.RbgPolarity.ChoosedButtonIndex = Enum.GetValues().ToList().FindIndex(x => x == _RS232DecodePrsnt.Polarity); } private void InitThreshold() { this.BtnThreshold.Text = SIHelper.ValueChangeToSI(_RS232DecodePrsnt.Threshold, 2, "V"); this.BtnThreshold.Click += (sender, args) => SetThreshold(); } private void InitParity() { this.CbxParity.DataSource = Enum.GetValues().Select(x => new KeyValuePair(x.GetDescription(), x)).ToList(); this.CbxParity.ValueMember = "Value"; this.CbxParity.DisplayMember = "Key"; this.CbxParity.SelectedValueChanged += (sender, args) => { _RS232DecodePrsnt.Parity = (ProtocolRS232.OddEvenCheck)CbxParity.SelectedValue; }; this.CbxParity.SelectedValue = _RS232DecodePrsnt.Parity; } private void InitStopBits() { this.CbxStopBits.ButtonItems = Enum.GetValues().Select(x => new RadioButtonItem() { Text = x switch { ProtocolRS232.StopBit.StopBit_1bit => "1.0", ProtocolRS232.StopBit.StopBit_2bit => "2.0", _ => "", }, Tag = x }).ToArray(); this.CbxStopBits.IndexChanged += (sender, args) => { _RS232DecodePrsnt.StopBits = (ProtocolRS232.StopBit)CbxStopBits.ButtonItems[CbxStopBits.ChoosedButtonIndex].Tag; }; this.CbxStopBits.ChoosedButtonIndex = Enum.GetValues().ToList().FindIndex(x => x == _RS232DecodePrsnt.StopBits); } private void InitByteOrder() { this.CbxByteOrder.ButtonItems = Enum.GetValues().Select(x => new RadioButtonItem() { Text = x.GetDescription(), Tag = x, }).ToArray(); this.CbxByteOrder.IndexChanged += (sender, args) => { _RS232DecodePrsnt.ByteOrder = (ProtocolRS232.MSB_LSB)CbxByteOrder.ButtonItems[CbxByteOrder.ChoosedButtonIndex].Tag; }; this.CbxByteOrder.ChoosedButtonIndex = (Int32)_RS232DecodePrsnt.ByteOrder; } private void InitDataBits() { this.CbxDataBits.ButtonItems = Enum.GetValues().Select(x => { String des = x switch { ProtocolRS232.DataBitWidth.DataBitWidth_5Bit => "5", ProtocolRS232.DataBitWidth.DataBitWidth_6Bit => "6", ProtocolRS232.DataBitWidth.DataBitWidth_7Bit => "7", ProtocolRS232.DataBitWidth.DataBitWidth_8Bit => "8", _ => "", }; return new RadioButtonItem() { Text = des, Tag = x }; }).ToArray(); this.CbxDataBits.IndexChanged += (sender, args) => { _RS232DecodePrsnt.DataBits = (ProtocolRS232.DataBitWidth)CbxDataBits.ButtonItems[CbxDataBits.ChoosedButtonIndex].Tag; }; this.CbxDataBits.ChoosedButtonIndex = Enum.GetValues().ToList().FindIndex(x => x == _RS232DecodePrsnt.DataBits); } private void InitRealBiteRate() { this.BtnRealBitRate.Click += (sender, args) => SetBitRate(); } private void InitBiteRate() { this.CbxBitRate.DataSource = Enum.GetValues().Select(x => new KeyValuePair(x switch { ProtocolRS232.BPSList.BPSList_Custom => "Custom", ProtocolRS232.BPSList.BPSList_115200 =>"115200bps", ProtocolRS232.BPSList.BPSList_19200=>"19200bps", ProtocolRS232.BPSList.BPSList_2400=>"2400bps", ProtocolRS232.BPSList.BPSList_38400=>"38400bps", ProtocolRS232.BPSList.BPSList_4800=>"4800bps", ProtocolRS232.BPSList.BPSList_57600=>"57600bps", ProtocolRS232.BPSList.BPSList_9600=>"9600bps", _ => ((UInt32)x).ToString() + "bps", }, x)).ToArray(); this.CbxBitRate.ValueMember = "Value"; this.CbxBitRate.DisplayMember = "Key"; this.CbxBitRate.SelectedValueChanged += (sender, args) => { ProtocolRS232.BPSList bitrate = (ProtocolRS232.BPSList)this.CbxBitRate.SelectedValue; SetRealBitRateVisibility(bitrate == ProtocolRS232.BPSList.BPSList_Custom); if (bitrate != ProtocolRS232.BPSList.BPSList_Custom) { this.BtnRealBitRate.Text = _RS232DecodePrsnt.BitRate.ToString(); _RS232DecodePrsnt.BitRate = bitrate switch { ProtocolRS232.BPSList.BPSList_115200 =>115200, ProtocolRS232.BPSList.BPSList_19200 => 19200, ProtocolRS232.BPSList.BPSList_2400 => 2400, ProtocolRS232.BPSList.BPSList_38400 => 38400, ProtocolRS232.BPSList.BPSList_4800 => 4800, ProtocolRS232.BPSList.BPSList_57600 => 57600, ProtocolRS232.BPSList.BPSList_9600 => 9600, _=>9600, }; } System.Diagnostics.Debug.WriteLine(bitrate); }; UpdateBitRate(); } private void InitSignalType() { this.CbxSignalType.DataSource = ProtocolRS232.SignalType.Difference.GetEnumList(); this.CbxSignalType.DisplayMember = "Key"; this.CbxSignalType.ValueMember = "Value"; this.CbxSignalType.SelectedValueChanged += (_, _) => { _RS232DecodePrsnt.ChType = (ProtocolRS232.SignalType)this.CbxSignalType.SelectedValue; ChangeSourceL(); CbxSourceL.Visible = _RS232DecodePrsnt.ChType == ProtocolRS232.SignalType.Difference; LblSourceL.Visible = CbxSourceL.Visible; }; this.CbxSignalType.SelectedValue = _RS232DecodePrsnt.ChType; } private void InitSource() { this.CbxSource.DataSource = ChannelIdExt.GetAnalogs().GetEnumList(); this.CbxSource.DisplayMember = "Key"; this.CbxSource.ValueMember = "Value"; this.CbxSource.SelectedValueChanged += (_, _) => { _RS232DecodePrsnt.ChannelId = (ChannelId)this.CbxSource.SelectedValue; SourceChanged(); }; this.CbxSource.SelectedValue = _RS232DecodePrsnt.ChannelId; } #endregion 控件初始化 private void SourceChanged() { //通道发生改变后重新获取需要同步的Prsnt GetDestinPrsnt(); ChangeSourceL(); } /// /// 当属性为时, /// 中值不能相同 /// private void ChangeSourceL() { if (_RS232DecodePrsnt.ChType == ProtocolRS232.SignalType.Difference) { if (_RS232DecodePrsnt.ChannelL == _RS232DecodePrsnt.ChannelId) { if (_RS232DecodePrsnt.ChannelL == ChannelIdExt.GetAnalogs().Last()) _RS232DecodePrsnt.ChannelL = ChannelIdExt.GetAnalogs().Last() - 1; else if (_RS232DecodePrsnt.ChannelL == ChannelIdExt.GetAnalogs().First()) _RS232DecodePrsnt.ChannelL = ChannelIdExt.GetAnalogs().First() + 1; else { _RS232DecodePrsnt.ChannelL++; } } if (this.CbxSourceL.DataSource is IList list) { foreach (var val in list) { (val as dynamic).Visible = (val as dynamic).Value != _RS232DecodePrsnt.ChannelId; } } } } /// /// 同步两个Prsnt /// /// 属性名称 private void DecodeSynchronization(String propertyName) { if (DestinDecodePrsnt == null) return; ///由于使用了进行判断具体同步的目标 ///因此参数不进行同步 if (propertyName == nameof(_RS232DecodePrsnt.ChannelId)) return; if (String.IsNullOrEmpty(propertyName)) { typeof(RS232DecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x=>x.CanWrite).ToList().ForEach(x => { x.SetValue(DestinDecodePrsnt, x.GetValue(_RS232DecodePrsnt)); }); } else { PropertyInfo propertyInfo = typeof(RS232DecodePrsnt).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic); if (propertyInfo != null) propertyInfo.SetValue(DestinDecodePrsnt, propertyInfo.GetValue(_RS232DecodePrsnt)); } } private void SetBitRate() { 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; HexNumberKeyboard numberKeyboard = new HexNumberKeyboard(); numberKeyboard.Controls.Cast().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.StyleManager.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 = (Int64)1E9; numberKeyboard.MinValue = 0; numberKeyboard.ValueType = HexNumberKeyboard.HexValueType.Dec; numberKeyboard.Value = _RS232DecodePrsnt.BitRate; numberKeyboard.OkClick += (sender, args) => { _RS232DecodePrsnt.BitRate = (UInt32)args.Data; this.BtnRealBitRate.Text = _RS232DecodePrsnt.BitRate.ToString(); _FloatForm.Close(); }; numberKeyboard.CancelClick += (_, _) => _FloatForm.Close(); _FloatForm.Controls.Add(numberKeyboard); _FloatForm.FormClosing += (_, _) => { if (this.FindForm() is FloatForm form) form.CanClose = lastflag; }; EventBus.EventBroker.Instance.GetEvent().Publish(this, new Uni_Trend.MSO7000X.Common.Structs.FormEventArgs() { Current = _FloatForm, Type = Uni_Trend.MSO7000X.Common.Structs.FormType.InfoForm, }); } private void SetThreshold() { Boolean lastflag = true; if (this.FindForm() is FloatForm form) { lastflag = form.CanClose; form.CanClose = false; } if (_FloatForm != null && !_FloatForm.IsDisposed) _FloatForm?.Close(); _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().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.StyleManager.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 = _RS232DecodePrsnt.Threshold; numberKeyboard.Unit = "V"; numberKeyboard.MinValue = -10; numberKeyboard.MaxValue = 10; numberKeyboard.DecimalNumber = 2; numberKeyboard.OkClickEvent += (sender, args) => { this._RS232DecodePrsnt.Threshold = args.Data; this.BtnThreshold.Text = SIHelper.ValueChangeToSI(_RS232DecodePrsnt.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; }; EventBus.EventBroker.Instance.GetEvent().Publish(this, new Uni_Trend.MSO7000X.Common.Structs.FormEventArgs() { Current = _FloatForm, Type = Uni_Trend.MSO7000X.Common.Structs.FormType.InfoForm, }); } private void SetRealBitRateVisibility(Boolean visibility = false) { this.BtnRealBitRate.Visible = visibility; this.LblRealBitRate.Visible = visibility; } public void UpdateView(String propertyName) { DecodeSynchronization(propertyName); if (String.IsNullOrEmpty(propertyName)) { UpdateView(); return; } this.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Where(x => { UpdatePropertyAttribute attribute = x.GetCustomAttribute(); return attribute != null && attribute.PropertyName == propertyName; }).FirstOrDefault()?.Invoke(this, null); } private void UpdateView() { this.GetType().GetMethods().Where(x => x.GetCustomAttribute() != null).ToList().ForEach(x => x?.Invoke(this, null)); this.Refresh(); } //private class ComboBoxData //{ // public String Key { get; set; } // public T Value { get; set; } // public Boolean Visible { get; set; } // public ComboBoxData(String key, T value, Boolean visible = true) // { // Key = key; // Value = value; // Visible = visible; // } //} //[AttributeUsage(AttributeTargets.Method)] //private class UpdatePropertyAttribute : Attribute //{ // public UpdatePropertyAttribute(String propertyName) => this.PropertyName = propertyName; // public String PropertyName { get; set; } //} #region UpdateFunction [UpdateProperty(nameof(RS232DecodePrsnt.ChannelId))] private void UpdataChannelId() { this.CbxSource.SelectedValue = _RS232DecodePrsnt.ChannelId; } [UpdateProperty(nameof(RS232DecodePrsnt.ChannelL))] private void UpdateChannelL() { this.CbxSourceL.SelectedValue = _RS232DecodePrsnt.ChannelL; } [UpdateProperty(nameof(RS232DecodePrsnt.BitRate))] private void UpdateBitRate() { Int32 index = Enum.GetValues().Select(x => x switch { ProtocolRS232.BPSList.BPSList_115200=>115200, ProtocolRS232.BPSList.BPSList_19200=>19200, ProtocolRS232.BPSList.BPSList_2400=>2400, ProtocolRS232.BPSList.BPSList_38400=>38400, ProtocolRS232.BPSList.BPSList_4800=>4800, ProtocolRS232.BPSList.BPSList_57600=>57600, ProtocolRS232.BPSList.BPSList_9600=>9600, _ => 0, }).ToList().FindIndex(x => x == _RS232DecodePrsnt.BitRate); if (index >= 0) { CbxBitRate.SelectedValue = Enum.GetValues()[index]; SetRealBitRateVisibility(false); } else { CbxBitRate.SelectedValue = ProtocolRS232.BPSList.BPSList_Custom; SetRealBitRateVisibility(true); } BtnRealBitRate.Text = _RS232DecodePrsnt.BitRate.ToString(); } [UpdateProperty(nameof(RS232DecodePrsnt.ByteOrder))] private void UpdateByteOrder() { this.CbxByteOrder.ChoosedButtonIndex = Enum.GetValues().ToList().FindIndex(x => x == _RS232DecodePrsnt.ByteOrder); } [UpdateProperty(nameof(RS232DecodePrsnt.DataBits))] private void UpdateDataBits() { this.CbxDataBits.ChoosedButtonIndex = Enum.GetValues().ToList().FindIndex(x => x == _RS232DecodePrsnt.DataBits); } [UpdateProperty(nameof(RS232DecodePrsnt.Parity))] private void UpdateParity() { this.CbxParity.SelectedValue = _RS232DecodePrsnt.Parity; } [UpdateProperty(nameof(RS232DecodePrsnt.StopBits))] private void UpdateStopBits() { this.CbxStopBits.ChoosedButtonIndex = Enum.GetValues().ToList().FindIndex(x => x == _RS232DecodePrsnt.StopBits); } [UpdateProperty(nameof(RS232DecodePrsnt.ChType))] private void UpdateChType() { this.CbxSignalType.SelectedValue = _RS232DecodePrsnt.ChType; } [UpdateProperty(nameof(RS232DecodePrsnt.Threshold))] private void UpdateThreshold() { this.BtnThreshold.Text = SIHelper.ValueChangeToSI(_RS232DecodePrsnt.Threshold, 2, "V"); } [UpdateProperty(nameof(RS232DecodePrsnt.Polarity))] private void UpdatePolarity() { this.RbgPolarity.ChoosedButtonIndex = Enum.GetValues().ToList().FindIndex(x => x == _RS232DecodePrsnt.Polarity); } #endregion UpdateFunction } [AttributeUsage(AttributeTargets.Field)] partial class RowColAttribute : Attribute { public RowColAttribute(Int32 row = 0, Int32 col = 0) { Row = row; Colnum = col; } public Int32 Row { get; set; } public Int32 Colnum { get; set; } public override Boolean Equals(Object obj) { if (obj == null) return false; if (!(obj is RowColAttribute attribute)) return false; return attribute.Colnum == Colnum && attribute.Row == Row; } public override Int32 GetHashCode() { Int32 hashCode = Row.GetHashCode(); if (Row.GetHashCode() != Colnum.GetHashCode()) hashCode ^= Colnum.GetHashCode(); return hashCode; } } }