|Real Software Forums
|Windows Tip: From USB to Serial Port
|Page 1 of 1|
|Author:||Jason_Adams [ Wed Feb 08, 2012 4:06 pm ]|
|Post subject:||Windows Tip: From USB to Serial Port|
If you're like me, you deal with USB serial devices; that is, a USB device that shows up as a Serial Device on some COM port. What's annoying, is that Serial Ports only give their port information COM1..2..etc. This has to do with what physical port you plugged your device into. So how do you detect which port your device is on? Well, with the information so far you can't. You either have to have the user select the COM port from a list, or assume the device is always plugged into the same port. Gah! No!
Now for this tutorial I'm going to assume you've spent the $40 and bought the MBS USB Plugin. Why? Because it saves time and energy. If you haven't done this, then roll up your sleeves and dig up the Declare functions from the Windows API. Completely doable, but I'd rather spend the money, personally. From there, the principal is the same, so this tutorial is equally relevant. (If you do achieve this with declares, be a friend and post the code on how you did it here.)
Again, this is a Windows Only solution. If someone wants to expand on how to do this for OSX, that'd be great.
1. So let's look at what we've got from the WinUSBDeviceMBS object that's helpful: Product Name/ID, Vendor Name/ID, serial number. There's nothing here that even hints as to what COM port is being used. Bummer. We're going to have to dig. To the registry!
2. Deep in the tedious registry is a magical directory called "CurrentControlSet". This can be found at the path: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\". This is a directory of all the services and controls your machine is actively using. New Control Sets are made, which you'll see as the adjacent directories, but the Current Control Set is what interests us because it's a pointer to the active set. If you dig a bit deeper in here, you'll find the /Enum/FTDIBUS directory. If you're not familiar, FTDI is the chip which manufacturers put in their USB devices to make them immediately serial communicable. Brilliant.
3. Here's our find: The FTDIBUS directory contains every USB device with serial capabilities. It's formatted: VID_XXXXX+PID_XXXXX+SERIALKEYA" — Vendor ID, Product ID, and the Serial Key with a strange "A" tossed on the end. Now, if you're fortunate, the manufacturer has a unique PID for their products... or they're stupid and made them all the same, as Omega Engineering did for the products I use. No problem, we're going to use the serial anyway. With this information, we're going to tie the Serial of the product with the vital information found in the "/0000/Device Parameters" directory: the "PortName" key, the value of which is the name of the COM port.
4. Alright, so let's get coding:
Dim Registry As New RegistryItem ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\FTDIBUS")
5. Yeah.. Let's break that down a bit. First, I get all of the USB devices in an array, then I dig into the registry, as described, and break apart the directory names in the FTDIBUS directory to get my serial numbers. I then check for USB devices that have the vendor name I'm looking for, serial key, and matching product name I'm interested in. I then store the COMX name for the corresponding device. Finally, I go through all the System Serial Ports, find the port with the matching name, and we're good to go.
Some additional error checking should be placed within this code, but I wanted to keep it simple for the sake of explanation and readability. I also used constants in my code for product and vendor names. But all in all, that's really how it's done. I hope this useful to someone, somewhere, someday.
|Page 1 of 1||All times are UTC - 5 hours|
|Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group