private void Initialize(string dataType, string componentType, string portName) { UpdateDataTypeCommand = new ReactiveCommand(); UpdatePortNameCommand = new ReactiveCommand(); UpdateCompoentTypeCommand = new ReactiveCommand(); DataTypes = UpdateDataTypeCommand .SelectMany(_ => Observable.Start(() => _recordDescriptionRepository.GetDataTypes(componentType, portName)) .Catch((Exception ex) => Messenger.Raise(new InformationMessage("データベースアクセスに失敗しました。", "エラー", "ShowError")))) .Do(_ => DataTypes.ClearOnScheduler()) .SelectMany(_ => _) .ToReactiveCollection(); PortNames = UpdatePortNameCommand .SelectMany(_ => Observable.Start(() => _recordDescriptionRepository.GetPortNames(dataType, componentType)) .Catch((Exception ex) => Messenger.Raise(new InformationMessage("データベースアクセスに失敗しました。", "エラー", "ShowError")))) .Do(_ => PortNames.ClearOnScheduler()) .SelectMany(_ => _) .ToReactiveCollection(); ComponentTypes = UpdateCompoentTypeCommand .SelectMany(_ => Observable.Start(() => _recordDescriptionRepository.GetComponentTypes(dataType, portName)) .Catch((Exception ex) => Messenger.Raise(new InformationMessage("データベースアクセスに失敗しました。", "エラー", "ShowError")))) .Do(_ => ComponentTypes.ClearOnScheduler()) .SelectMany(_ => _) .ToReactiveCollection(); DataType = dataType; PortName = portName; ComponentType = componentType; UpdateDataTypeCommand.Execute(null); UpdatePortNameCommand.Execute(null); UpdateCompoentTypeCommand.Execute(null); }
public int Initialized() { string[] PortNames; string[] TempPortNames = SerialPort.GetPortNames(); if (RePortName != null && TempPortNames.Contains(RePortName)) { PortNames = new string[TempPortNames.Length + 1]; TempPortNames.CopyTo(PortNames, 1); PortNames[0] = RePortName; PortNames = PortNames.Distinct().ToArray(); } else { PortNames = TempPortNames; } //这种写法不允许有多个串口; for (int i = 0; i < PortNames.Count(); i++) { if (OpenPort(PortNames[i]) == 0) { RePortName = PortNames[i]; Read(); return(0); } } return(-1); }
private void RefreshPortNames() { // get two lists, and if not the same, replace one var physicalNames = SerialPort.GetPortNames(); var localNames = PortNames.ToArray(); // local copy to prevent locking/size changing issues var matches = physicalNames.Length == localNames.Length; if (matches) { // same length, check contents foreach (var name in physicalNames) { matches &= localNames.Contains(name); } foreach (var name in localNames) { matches &= physicalNames.Contains(name); } } if (!matches) { var msg = Messager; if (msg != null) { msg.AddMessage("COM Ports changed"); } // replace internals var curSelected = SelectedPortName; // copy in new ones PortNames.Clear(); foreach (var name in physicalNames) { PortNames.Add(name); } var oldNameOk = !String.IsNullOrEmpty(curSelected) && PortNames.Contains(curSelected); if (oldNameOk) { SelectedPortName = curSelected; } else if (PortNames.Any()) { SelectedPortName = PortNames[0]; } else { SelectedPortName = ""; } // todo - disconnect when pulled? handle crashes? } }
public ComPortViewModels() { if (!PortNames.Any()) { throw new ArgumentException("Нет ни одного COM порта"); } _serialPort = new SerialPort { Parity = Parity.None, DataBits = 8, StopBits = StopBits.One }; PortName = PortNames.Last(); PortSpeed = 9600; _serialPort.DataReceived += SerialPortOnDataReceived; }
private ConfigurationModel CreateInstance(Configuration configuration) { if (configuration == null) { configuration = new Configuration() { Active = true, Mode = (int)Mode.RTU, DataBits = 7, Parity = (int)Parity.Odd, BaudRate = 6900, Timeout = (int)Timeout.S30, PortName = PortNames.FirstOrDefault(), StopBits = (int)StopBits.None, } } ; return(this._mapper.Map <ConfigurationModel>(configuration)); }
public Form1() { InitializeComponent(); // Check the valid Com ports string[] PortNames; PortNames = SerialPort.GetPortNames(); int UpperBound = PortNames.GetUpperBound(0); for (int i = UpperBound; i >= 0; i--) { Combo_COMPort.Items.Add(PortNames[i]); } // İnitial values for comboboxes Combo_COMPort.SelectedIndex = 0; Combo_BaudRate.SelectedIndex = 0; Combo_DataBits.SelectedIndex = 0; Combo_StopBits.SelectedIndex = 0; Combo_Parity.SelectedIndex = 0; }