Dingus
Dingus
CC#
Created by Dingus on 5/17/2024 in #help
WPF possible threading issue?
I am creating a application to update a bunch of usb devices at the same time. I have figured out the usb communication with devices just fine but somehow someway I get a threading issue. I am just getting into threading so I just need someone with personal experience on how they would handle this situation. I have a System.Management object to watch for usb events. When usb connect/disconnect event fires off then check all devices connected by specific usb protocol and look at device paths for PID/VID (specific to manufacturer).
public MainWindow()
{
InitializeComponent();
StartDeviceDetection();
}

public void StartDeviceDetection()//start device detection through USB
{
ManagementEventWatcher watcher = new ManagementEventWatcher();
WqlEventQuery query = new WqlEventQuery("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_USBControllerDevice'"); //Querey for specific device standard
watcher.EventArrived += USBEvents; //subscribe to events it should querey for
watcher.Query = query; //Set Query
watcher.Start();//start watch
}

private void USBEvents(object sender, EventArrivedEventArgs e)//detects when device is removed from computer
{
ManagementBaseObject instance = (ManagementBaseObject)e.NewEvent["TargetInstance"];
eventType = e.NewEvent.ClassPath.ClassName;
//scanning for device arrival
if (eventType.Equals("__InstanceCreationEvent"))
{
aDeviceArrived();
}
}

private void aDeviceArrive(){
List<string> allPaths = {list of all paths with specific}
foreach(string path in allPaths){
if(path.Contains(VID) && path.Contains(PID)){
Device device = new Device();
device.path = path;
gridWithDevices.add(device);
}
}
public MainWindow()
{
InitializeComponent();
StartDeviceDetection();
}

public void StartDeviceDetection()//start device detection through USB
{
ManagementEventWatcher watcher = new ManagementEventWatcher();
WqlEventQuery query = new WqlEventQuery("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_USBControllerDevice'"); //Querey for specific device standard
watcher.EventArrived += USBEvents; //subscribe to events it should querey for
watcher.Query = query; //Set Query
watcher.Start();//start watch
}

private void USBEvents(object sender, EventArrivedEventArgs e)//detects when device is removed from computer
{
ManagementBaseObject instance = (ManagementBaseObject)e.NewEvent["TargetInstance"];
eventType = e.NewEvent.ClassPath.ClassName;
//scanning for device arrival
if (eventType.Equals("__InstanceCreationEvent"))
{
aDeviceArrived();
}
}

private void aDeviceArrive(){
List<string> allPaths = {list of all paths with specific}
foreach(string path in allPaths){
if(path.Contains(VID) && path.Contains(PID)){
Device device = new Device();
device.path = path;
gridWithDevices.add(device);
}
}
10 replies
CC#
Created by Dingus on 2/13/2023 in #help
❔ Converting from string into hex literal
I am looking for anyone who can help convert a hex string into a int hex literal. I am creating a usb program using usblibdotnet to find and connect to a device on my computer. In order to connect to the device I need the device PID and VID defined as hexedecimal intergers but how I know to retrieve them from device manager is as a string. If I try to use a simple conversion such as "Convert.ToInt32" or even "Int.Parse" it will convert the number into its literal numerical value rather than keeping it as its hexadecimal value. ex. string = "0x05E0" needs to be int = 0x05E0. Most methods I have found for converting end up making it int = 1504 which isn't wrong but it needs to stay as its literal hexadecimal value in order to work with the library and find the device.
35 replies
CC#
Created by Dingus on 8/12/2022 in #help
USB and Csharp
Hi Im new here and was wondering if there is anyone out there who can help me. I have about 2 years experience in C# but am looking to work with devices through standard USB communication. I have looked through the libusbdotnet library and tried looking through its documentation to understand it more but find the documentation to be not much help as there are almost no explanations for why or how things are set up. If anyone has any ideas or suggestions on how to approach USB communication whether it be in C# or another language I would really appreciate it!
19 replies