How do I determine which process is using a serial port? - vb.net

The company I work for makes hardware that communicates to the computer though a serial port. Third party companies write software that communicates with our hardware.
There are times when I need to diagnose our hardware. However, a third party software app connects to the serial port when Windows starts up, blocking any other connection. I don't know the name of this application/service and it's not always the same one.
Is there any way to either:
Find the name/pid of the app/service that is currently using a given serial port or
Steal the serial port connection from another app.
vb.net preferably, but I'll take a language agnostic answer as well.

You can use the process explorer tool also from SysInternals to search for open handles. In this case you would want to search for 'Serial' since it uses device names that may not map to com port numbers. (e.g. COM1 is \Device\Serial0 on my system).
If you want to take control of the serial port from another app I think you would need co-operation of the driver.

As Rob Walker said, you can find who's using a serial port using Process Explorer. Most of the time, typing Ctrl+F and searching for "serial" will show you who has a serial port open, but I just ran into a situation where my "COM3" serial port's handle appeared as "\Device\VCP0". It may be strange because it was running under VirtualBox with a USB-to-serial connector.
If searching for "serial" and "device\vcp" don't get you any results, you may be able to figure out how serial port handles are named by opening one with a known program. In Process Explorer, display the lower pane with each process's open handles by typing Ctrl+L. Click on the process that you used to open the serial port and look through the lower pane to see which handles look like they might be a serial port. You can open and close the port while you're looking, and the file handle should appear and disappear, as well as being highlighted in green or red. Of course, this is only possible if you have more than one serial port or the serial port you're trying to diagnose isn't always locked by some mystery process.

Sysinternals has a slew of utilities I find very useful and educational for tracking down what processes are doing to the system.
They have a utility that does exactly what you need called Portmon, and give some information on how it works near the bottom of the page. That info and a few well-asked questions will probably give you everything you need to implement it yourself if the utility isn't enough.
-Adam

Related

Why are my 2 USB connected devices, using separate FTDI Virtual COM Ports (COM3 and COM2), communicating over the same COM port?

I have a program written in Visual Basic (.NET Framework 3.0) that communicates with a power supply via a D2XX driver (over COM3) and a pump via a VCP driver (over COM2). Currently, if both USB devices are plugged in when I start the program, it tries to communicate with the pump, (COM2) when trying to communicate with the power supply (COM3). I found a temporary solution to this issue by only have the power supply connected when starting the program. Then I initialize communication with the power supply, and afterwards plug in the pump; this allows the program to communicate with both separately for the rest of the time running the program. But this process is tedious and I am hoping there is an alternative solution where I can keep both units connected via USB and the computer communicates separately. What is confusing is that this communication issue was not always the case, a week ago it worked without having to disconnect the pump (all drivers are updated, and on the device manager they are listed as their respective COM ports as well) ; I am struggling to find some correlation between what is the underlying issue.
I tried to delete other hidden additional COM ports. I made sure all drivers were updated. When the pump is communicating via the same port as the power supply, and then I try to communicate with the pump, I receive this error:
System.UnauthorizedAccessException
HResult=0x80070005
Message=Access to the port 'COM2' is denied.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
But again, If I start the program with only the power supply plugged in and initialize communication with the power supply, and THEN plug in the pump, this issue is evaded. I hope someone can give some sort of direction at all troubleshooting this issue because I am completely stumped. Thank you!
Most likely your program implicitly relied on the correct enumeration order. If you use devices with an FTDI product with internal flash or with dedicated eeprom, you may be lucky: Many can configured via FT_PROG to be visible only for D2XX and not as VCP. This way you can avoid the problem. Alternatively, you can try to modify your program so that it accesses D2XX first. Make sure to filter out the correct device using device informations. E.g. the serial number or the VID:PID if the device manufacturer has made the effort to aquire its own IDs. Check 3.4 FT_GetDeviceInfoList of D2XX Programmer's Guide for details.

Is there a POSIX way of determining if a file (serial port actually) is open-able without opening it?

We make a device that can appear as a USB serial port on a variety of POSIX-compliant systems. I'm supporting an API that allows callers to retrieve a list of all the currently available (i.e. not in use, have correct privileges, etc.) instances of our device available to them. The caller can also get a list where the in-use devices are also indicated.
The simple way to do this of course is just attempt to open/close each candidate serial port and take note of which ones are open-able. I would prefer not to do this as it is possible that multiple applications using this API (which would be a library / dylib in their app) could create race conditions where they both try to query the same serial port simultaneously which would cause one app to erroneously think the device was in use.
So what I need is a way of knowing the port is open-able without actually opening it. I have seen others use lock-file schemes where there is a special file created to indicate the port is in use - the assumption being that if the file is not present the port is available. My problem is that there may be other users of the port other than my library that do not abide by such as scheme, so I cannot rely on it.
Is there perhaps some low-level POSIX functionality that lets me query a file's status in this regard without actually attempting to open it?

SerialPort.GetPortNames() not returning correct result

I'm developing small demo application for Windows Mobile 6.1 to get the list of all serial ports and the Bluetooth devices configured on them. I'm facing some weird problem.
I can see total 10 COM ports when I go to Settings->Connections->Bluetooth->COM POrts. But when I use SerialPort.GetPortNames() to get all the ports, it shows 9 ports i.e. one port less. The last port i.e. COM09 is not returned in the result set.
Then I removed one of the paired devices configured on one of the COM ports (COM08) and SerialPort.GetPortNames() returned the result set WITHOUT the COM08 port.
I further tried to know which COM ports are still unused. When i tried to create "New Outgoing Port" manually on one of the 'unused' (as per my program) port, it gives a message saying "COM port cannot be created"!
Could someone tell me the reason and guide me further to resolve the issue?
The Bluetootch driver is probably modifying the registry key where the Drivers are and GetPortNames is probably looking through that key.
Take a look at both HKLM\Drivers\BuiltIn and HKLM\Drivers\Active with the remove registry editor and see what's happening. You can likely write your own parser that looks at these values for what you are after.

WiFi communication to embedded display

I'm trying to create an embedded outdoor display of bus arrival times at my university. I'd like the device to utilize my school's secured WiFi network to show arrival time updates determined from a server script I have running.
I was hoping to get some advice on the high-level operation of this thing -- would it be better for the display board to poll a hosted database via the WiFi network or should I have a script try to communicate with the board directly over 802.11? (Push or Pull?)
I was planning to use a Wifly or WIZnet ethernet board in combination with a wireless access hub. Mostly inspired by this project: http://www.circuitcellar.com/Wiznet/winners/001166.html Would anyone recommend something else over one of the WIZnet boards? I saw SPI/UART options and thought these boards could work with an AVR platform.
And out of curiosity -- if you were to 'cold start' this device (ie, request a bus arrival time by pushing the display's on button) you might expect it to take 10-20 seconds to get assigned an IP and successfully connect to the database, does that sound right?
I'd go pull. In fact, I'd have outdoor display make http or https requests of the server. That way the server could tell it how long to show a given set of data before polling for a new one using standard http page expiration.
I think pull would make it easier to have multiple displays, and to test your server as well. I've also got a gut feeling that this would make your display more secure. Someone would have to hack your server to hijack your display.
There's a very cool looking Arduino-targetted product called the WiShield. Seems super easy to use and he provides some source code. It uses SPI for host communication. If you're not interested in going the Arduino route, I'm sure the source code wouldn't be too hard to port to something like avr-gcc. Check it out, might save you some time and headaches for $55. Worth checking out anyway.

How to access a PCMCIA modem's serial number?

A Sprint cellular modem plugs into a laptop - often the PCMCIA slot. To connect, the user opens a program called the Connection Manager. This program has a menu option to display the serial number and phone number of the modem that's currently inserted.
Does that imply that the serial/phone number of the modem could be available to other programs running on the system? (Note: let's assume this is Windows XP.)
Specifically, could a company's VPN software be configured to pass along information about which modem is being used to connect?
Finally, is there existing VPN software that already does this, or would it have to be custom-programmed?
Sometimes you can get the modem's serial number using the AT command set. To see this in action, go to your control panel and open up Phone and Modem Options. Select the Modems tab, select the modem you're interested in, and choose Properties.
In the modem window, select the Diagnostics tab, and press the Query Modem button.
This opens the serial port and sends a series of AT commands to gather various settings and information. You can open the serial port in your program (or a terminal program), send the AT command, and get the same information back.
You may need to check your specific modem's AT command set to find where the serial number is stored, or use a serial port spy program to see how Sprint's program does it.
I'm not aware of any VPNs that use this information, and I can think of several ways to spoof it, since communications between the modem and the computer are not cryptographically secure.
-Adam
Open hyperterminal or make a serial port connection programatically and use Hayes AT language to talk to it. Most software also has it listed in the device properties and/or diagnostics.
AT+GSN
press enter