Example #1
0
        public SerialPortWatcher(UpdateSerialPostListEventHandler updateSerialPostEvent)
        {
            UpdateSerialPostEvent += updateSerialPostEvent;
            _taskScheduler         = TaskScheduler.FromCurrentSynchronizationContext();
            ComPorts = new ObservableCollection <string>(SerialPort.GetPortNames().OrderBy(s => s));

            WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent");

            _watcher = new ManagementEventWatcher(query);
            _watcher.EventArrived += (sender, eventArgs) => CheckForNewPorts(eventArgs);
            _watcher.Start();
        }
Example #2
0
        private void initProfile()
        {
            if (!hasProfile())
            {
                _commProfile           = new CommProfile();
                _commProfile.Baudrates = new ObservableCollection <string> {
                    "110", "300", "1200", "2400", "4800", "9600", "14400", "19200", "38400", "57600", "115200", "128000", "256000"
                };
                _commProfile.VerifyBit = new ObservableCollection <string> {
                    "无", "奇校验", "偶校验", "标记", "空格"
                };
                _commProfile.DataBit = new ObservableCollection <string> {
                    "8位", "7位", "6位", "5位"
                };
                _commProfile.StopBit = new ObservableCollection <string> {
                    "无", "1位", "1.5位", "2位"
                };
                _commProfile.Encoding = new ObservableCollection <string> {
                    "DEFAULT", "UTF-8", "UNICODE", "GB2312", "ASCII"
                };
                _commProfile.BaudrateSelected  = 5;
                _commProfile.VerifyBitSelected = 0;
                _commProfile.DataBitSelected   = 0;
                _commProfile.StopBitSelected   = 1;
                _commProfile.EncodingSelected  = 1;
                _commProfile.IsReceivedToHex   = false;
                _commProfile.IsSendToHex       = false;
                _commProfile.IsAddNewLine      = true;
                _commProfile.TimerInterval     = 1000;
                _commProfile.NewLineType       = 1;
                _commProfile.Commands          = initCommands();
                var json = JsonConvert.SerializeObject(_commProfile);
                writeProfile(json);
            }
            else
            {
                _commProfile = JsonConvert.DeserializeObject <CommProfile>(readProfile());
            }

            dataContext.DataContext = _commProfile;


            UpdateSerialPortListEvent = (obj, args) =>
            {
                var changePortName = args.SerialPortName;

                if (btnOpenSerialPort.Content.Equals("关闭串口"))
                {
                    if (_serialPort.PortName.Equals(changePortName))
                    {
                        ClosePort();
                    }
                }

                if (_serialPortWatcher.ComPorts.Count == 1)
                {
                    comboBoxComm.SelectedIndex = 0;
                }
            };
            _serialPortWatcher = new SerialPortWatcher(UpdateSerialPortListEvent);
        }