private void BtnRefreshDevices_Click(object sender, RoutedEventArgs e) { ComPortViewModel.RefreshComPortList(); if (ComPortViewModel.ComPorts.Count > 0) { SelectedComPort = ComPortViewModel.ComPorts.LastOrDefault(); } }
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { // Here's where the help ends. Do what you need here. // Get additional info from http://www.pinvoke.net/ // USB event message is msg == 0x0219 (WM_DEVICECHANGE). // USB event plugin is wParam == 0x8000 (DBT_DEVICEARRIVAL). // USB event unplug is wParam == 0x8004 (DBT_DEVICEREMOVECOMPLETE). // Your device info is in lParam. Filter that. // You need to convert wParam/lParam to Int32 using Marshal. if (msg == 0x0219 && (wParam.ToInt64() == 0x8000) || (wParam.ToInt64() == 0x8004)) { Debug.WriteLine("Device list changed, refreshing COM port list"); ComPortViewModel.RefreshComPortList(); } return(IntPtr.Zero); }