Query A Device Attached to USB - Windows - c#

Well, I've looked all over the internet, and it appears that what I want to do is near impossible without writing my own driver.
I have a device attached to a USB port, and - unfortunately for this situation - it uses a USB to UART driver to emulate a serial port which streams the device output.
However, in order to validate a license for the software, I want to ensure that I know which device is attached to the USB port - not just the make and model, but (hopefully) the actual serial number. If not the serial number, at least some unique identifier.
But I'm not sure where or how to get that.
The UART Bridge has a PNPDeviceID, but that doesn't seem to change, regardless of which device is plugged into the port.
I'm working in C#, which, from what I've read, is also a limitation.
So my question is this: How can I get a unique identifier from a USB-attached device while using a UART Bridge driver in C#?
Thanks in advance.

You might try this library. There's some sample code in this MSDN question as well.

The PnpDeviceID actually is what I was looking for. It's just that the EEPROM has to be updated, which is demonstrated in one of the sample applications from Silicon Labs. The PnpDeviceID consists of the Vendor ID, Product ID, and Serial Number, all of which are customizable via the method demonstrated. The Serial Number on my particular set of devices defaults to 0001, which makes it appear that the PnpDeviceID doesn't change.
Reference here.

Related

How to determine if a GPS device is a built-in receiver

We have an C#-application for Windows which makes use of data from a USB-connected GPS receiver. In the last year or so, many users have started to complain about bad quality of the position- and velocity data.
After some investigation, we realized that many new laptops have a built-in GPS receiver, which seems to be of quite low quality, and does not have the antenna in an optimal position.
Our application automatically checks the COMx ports for a GPS receiver, and selects the first one it finds. We would like to keep it this way, but in addition we would like it to never select a built-in device.
Is there any way of finding out this information in a quite generic way? We don't know of which brands the users' laptops will be, and we don't know for sure which brands the external receivers will be.
Built-in GPS receivers are usually parts of composite WLAN/WWAN devices. Here’s how the device tree looks like when you select View / Devices by connection:
You see?
The parent of the GPS COM port is a USB composite device, that has many children, and one of those children is a network adapter. You can query that hierarchy programmatically using setup API: Get parent device And you can query device interface classes to distinguish the network adapter, check for KSCATEGORY_NETWORK and GUID_DEVINTERFACE_NET
However, this method isn’t very reliable. There’re external USB devices out there that are also GPS + WWAN. So it’s better to show the list of all GPS devices, and allow users to select the right one. And use the heuristic I’ve described above to choose the initially-selected default device.

What can be used as a unique USB device identifier aside from serial number?

I'm making a USB desktop locker, much like this one but I'm trying to make it work with other USB devices (mice, kb, anything really). Are there any other unique info I can use aside from a serial number? Are there ways to access these from .NET? (C# specifically)
(I had some usb devices without a serial number, mostly from China)
Use USB VID and PID. Not the best way, but atleast it is one. When you are able to use a unique ID, then use it too. You can use hashfunctions to combine them too.

What determines the order for sound devices in windows when using winmm.dll?

I am trying to use NAudio to create a multiple sound output application. We have 8 USB sound cards installed. NAudio lets me use all 8 but I can't figure out a pattern for determining which device index is which card.
The cards will be hooked up to different hardware so it is important to make sure you know which card you are using.
I have been trying to use WMI to poll for the information but I can't seem to locate any information that determines the order of the sound devices.
Update:
I forgot to include some information about this problem. The sound cards are all USB sound cards hooked up through a 12 port hub.
The order of devices is non deterministic for all versions of Windows. For Vista and above, the devices are typically ordered by the DSound GUID (more-or-less) so they're effectively random.
I'm assuming you are using WaveOut? You can call WaveOut.GetCapabilities(deviceNumber) to get hold of the name of the device, which might help you out.
This is what I have come up with so far and it works for us.
Using WMI you can get the DeviceID from Win32_SoundDevice. Then using that you can access the registery at HKLM\SYSTEM\CurrentControlSet\ENUM\'DeviceID' and get the string value named "Driver". This value contains the ClassGUID plus a number at the end.
Example: {4d36e96c-e325-11ce-bfc1-08002be10318}\0015
If you strip off that last number*(15)* for all of you sound devices and order them, that is the order that the devices are listed from NAudio with uses winmm.dll. There is also a location for these sound devices, either in the registery at the same key or from Win32_PNPEntity using the DeviceID.
In our case the location lets us determine which port of the USB hub that sound device is plugged into.
Have a look at this MSDN article. It uses DirectSound to enumerate the audio devices:
http://msdn.microsoft.com/en-us/library/bb318674(VS.85).aspx

How can I determine the speed of a USB port?

Is there an easy way to programmatically determine the speed (or version) of a USB port? I'm looking to control the speed of data sent to a usb port based its maximum bandwidth.
If you need a solution for Windows this should be a good start:
http://msdn.microsoft.com/en-us/library/ms793313.aspx
Basically you should try this:
Enumerate the USB devices and the symbolic names to their drivers
Open a handle to the USB device driver through its symbolic name via CreateFile
Perform a DeviceIoControl on the driver handle with the control code IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX. This will have you returned the structure USB_NODE_CONNECTION_INFORMATION_EX. There you have a member there called Speed of the type USB_DEVICE_SPEED.
(Close the driver handle)
This could also be interesting for you: CodeProject: Enumerate Properties of an Installed Device
To answer your question, I'm sure that there are ways of getting the information you need. I don't know the answer for windows, but Linux has files you can read within the sysfs directory structure.
Speed control is usually taken care of by the drivers and the hardware controlling the bus. Most modern USB controllers really have 2 controllers per port connected. 1 for the slower speed 12Mbps USB 1.0, and another for the higher pseed 480Mbps USB 2.0. There is a magic switch inside that connects it properly. The driver itself makes sure that everything is enumerated properly, controls the flow, etc. A higher level "user-space" application typically doesn't need to worry about these things.
Also, if you have a device that is capable of running at faster than 12Mbps, and you plug it into a 12Mbps port, it'll get dragged down to 12Mbps whether you like it or not. Is it that you want to know that is got dragged down?

Need a Switch component controlled via USB port

I need device switch component which an be controlled by code(.net,C# Or vb) ON/OFF state is enough..
I have code knowledge in C#, .net window application, I need to make a switch that can be controlled using code via USB port ... so that using that switch I will turn on/off electrical devices (fan,light) for a specific time interval.. For my MINI Project
Note: I need the brief note regarding components needed and how to assemble it.
If you know any link which resembles this type of project pls refer me.
thanks.
You'll probably want to look into using an FTDI chip for this purpose. They do have a .NET interface available as well as traditional C DLLs.
FTDI offers two types of drivers - one emulates a simple COM port (Virtual COM Port, which is probably enough for you), and the other is more of a direct USB control (D2XX), although it still shows up as a COM port when the USB device is connected.
On the other hand, if you really only need on/off support, you could probably just use the .net serial port drivers for the most basic communication. Of course, the serial port drivers don't let you tie a serial port pin high or low, so you will need some method of reading the data coming from the PC. Many microcontrollers have freely available UART libraries exactly for this purpose, but you will need to also obtain an RS232 level shifting IC for this because the serial port from the PC outputs +/- 6V IIRC (might be +/- 12V), while most microcontrollers run off of and accept signals at 0/5V or 0/3.3V.
You could try something like the Arduino or one of its clones like the Sanguino. It's probably a bit overkill for your project, but they come pre-assembled and have a large support base.
You would also (for any low voltage switching circuit) need some form of relay board to switch electrical appliances that run on mains power. See here for some ideas.
Are you sure you have to do this with the USB port?
You'd need a device that implements a whole USB interface. This isn't as simple as using a line on the COM port (RTS/DSR etc...) or using the parallel port.
A quick search found this...

Categories

Resources