0

I use the following C# code, to get an event, if a device is attached to a COM port.

WqlEventQuery query = new WqlEventQuery("__InstanceCreationEvent", new TimeSpan(0, 0, 3), @"TargetInstance ISA 'Win32_SerialPort'");
var watcher = new ManagementEventWatcher(query);
watcher.EventArrived += EventArrived;
watcher.Start();

This code works also on all PCs. But on many PCs with Windows 11, the event EventArrived is not raised if I attach a device to a COM port.

Any idea what I have to change?

Benjamin
  • 3
  • 2
  • It's not clear what kind of device you're using - specify _manufacturer_ and _model_. It's not clear how your device is attached. Is your device connected to a USB port? You may consider reviewing the technical documentation for Win 11. Anytime a new version of Windows is released, it contains security improvements over previous versions. So, one of the first things to try is to configure your app to run as an administrative user. – Tu deschizi eu inchid Apr 05 '23 at 15:38
  • Sorry for my bad explanation. I use the [Opticon 2001 Scanner](https://opticon.com/product/opn-2001/). The driver of the device is ok, I check this already with the Opticon Support. I can see the device also in the device manager of Windows. And I have also access to the device with the API of Opticon, so I can download the data and much more. I try to run the app as Administrator, no changes. – Benjamin Apr 06 '23 at 05:45
  • Hello, I try to check, if a USB device is attached: 'WqlEventQuery query = new WqlEventQuery("__InstanceCreationEvent", new TimeSpan(0, 0, 3), @"TargetInstance ISA 'Win32_USBHub'");' In this case, the event EventArrived is triggered. – Benjamin Apr 06 '23 at 06:26
  • Hello, maybe an additional information: Also if I try this code the COM port with my device is not available: `ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_SerialPort"); foreach (ManagementObject queryObj in searcher.Get()) { Console.WriteLine("-----------------------------------"); Console.WriteLine("Win32_SerialPort instance"); Console.WriteLine("-----------------------------------"); Console.WriteLine("Name: {0}", queryObj["Name"]); }` It must me a problem whit the device or his driver? – Benjamin Apr 06 '23 at 11:15
  • You can use _PowerShell_ to run the query as well. Since your device is a USB device, it may not show up in `Win32_SerialPort`, but probably will show up in `Win32_PnPEntity`. The following may be helpful: https://stackoverflow.com/a/74895268/10024425, https://stackoverflow.com/a/65971845/10024425, and https://stackoverflow.com/questions/620144/detecting-usb-drive-insertion-and-removal-using-windows-service-and-c-sharp – Tu deschizi eu inchid Apr 06 '23 at 14:26
  • Thank you @user09938 for your help. I use now a timer an check each second, if a valid device is connected, because the API of my devices (Opticon) can find the port. – Benjamin Apr 07 '23 at 07:35
  • None of the posts that I referenced use a timer - using a timer is probably unnecessary. – Tu deschizi eu inchid Apr 07 '23 at 15:12

0 Answers0