I'm looking for a fast way to detect if a serial port is present (also if busy from mouse or from other peripherals). Is there any API for this ? Many thanks.
Posted on 2001-03-13 14:21:00 by angelo
Angelo, There are a couple of ways of looking for the devices, I'll show you two here. The simplest way is to try and open them, and check the result, use CreateFile with the device name as "COMx" (x = number of the COM port e.g. "COM1") use GENERIC_READ+GENERIC_WRITE, sharing is not allowed, so use NULL for that, and you have to use OPEN_EXISTING. If you don't get INVALID_HANDLE_VALUE returned then, you've got a COM port you can use. If you're only looking to see if they exist, then remember to use CloseHandle to release them again. This will get you an open COM port, but you can't see the ones which are in use (with mice and the like) because they will already be open. To the second Method:- There's a handly little routine called QueryDosDevice, if you give it a first parameter of NULL, it returns a list of all the devices (you get all kinds of stuff with NT) If you give it the name of a device you are looking for (e.g. "COM1") it will return it as a string into the buffer supplied :-

invoke QueryDosDevice,NULL,szDevices,4096
Will give you a list of all devices on the system into szDevices, it's format is a list of null terminated strings, with a double null to finish (# = NULL) :-

The list I get is much longer than this but you get the idea. The only bad side to QueryDosDevice is it's NT and 98 only. You could also rifle the registry for the ports too. umbongo
Posted on 2001-03-14 03:49:00 by umbongo
umbongo, many thanks for your very precious help about serial ports cheking. I've saved it. Now i'm working as you indicate using CreateFileHandle to open the ports. Then, receivinf bad hanlde value as return, i'm calling GetLastError and using error code 5 (access denied) as busy port (for example used by mouse) and error 2 (file not found) as port not present. I don't know if it is always true but till now it is working correctly. I'm trying also QueryDosDevice. Very nice. Many thanks again angelo
Posted on 2001-03-14 13:28:00 by angelo