PDA

View Full Version : How Do I get the “friendly” name of serial port



sudheer168
22nd June 2009, 14:55
Hi ,
I am using QT4 in windows to develop an GUI application which is used to communicate with a Hardware Electronic unit through USB. I am having a UDB serial cable connected between system and Electronic unit .It shows in Device Manager as "MOXA USB Serial Port (COM2)".
In program I can able to get the active ports by checking whether it is open or not.But I want to be able to get the "friendly" .It means COM2 is not a friendly name. I want something that says "COM2 - USB connector" or something like that. It is nasty and hacky, but many times end users have no idea what COM port they need to open in my program unless there is a useful name - more useful than "COMn."
I want to know is it possible to do using QT or I have to use any windows Native API. If I have to use windows Native API what i have to do ,I mean which API I have to use and how it should include in my QT program,I means whether I have to include any windows libraries or any header files and which one I have to include.
SO please suggest me to solve this problem.

With regards,
Sudheer.

gsmiko
22nd June 2009, 20:48
Hi sudheer168!

I wonder how do you access the com port. Are you using some native win32 api functions or the QextSerialPort (http://qextserialport.sourceforge.net/)library? In QextSerialPort the ports can be enumerated and some useful information can be acquired, but I'm afraid it'll only return "COMx" for friendly name, I haven't tried it yet with non native serial ports, so I'm not sure. It's worth to try.
If that doesn't help you, and if platform dependency doesn't bothers you, consider using WMI (http://msdn.microsoft.com/en-us/library/aa394582(VS.85).aspx) (Windows Management Instrumentation). A huge amount of system information can be extracted from it, including hardware related stuff like com ports. This example (http://msdn.microsoft.com/en-us/library/aa390418(VS.85).aspx) should be enough to get started. The caption for the port (the exact same text what you can see in device manager), can be acquired using this query text "SELECT Caption FROM Win32_SerialPort".

sudheer168
23rd June 2009, 11:21
Hi thanks for y our kind reply.
Actually I am using QextSerialPort library that is why I am unable to get the friendly name of the com port. You suggest me to use WMI ,there is no problem to use.But one thing I want to know is how can I use the WMI in my Qt application.I mean whether I have to integrate any library or I have have to include any header file to use WMI.
So please suggest me to solve this problem.

With regards,
Sudheer.

gsmiko
23rd June 2009, 11:47
This example (http://msdn.microsoft.com/en-us/library/aa390418(VS.85).aspx) covers all the things you need to know. You have to include comdef.h, and Wbemidl.h, and you have to link with wbemuuid.lib. All of these can be found in the Windows SDK, no 3rd party stuff is required.

sudheer168
25th June 2009, 06:50
Hi gsmiko, thanks for your kind reply.
I gone through the example code which you have suggested me.I included the comdef.h, and Wbemidl.h in my QT application class and linked with wbemuuid.lib.But in if I used the API ,while compiling it is showing errors as ...


error C3861: 'CoInitializeEx': identifier not found, even with argument-dependent lookup
error C2065: 'COINIT_MULTITHREADED' : undeclared identifier


The errors I posted above are few that I occour.So how can I be out of these type of errors.
So please help me to solve this problem.

With Regards,
Sudheer.

kuzulis
25th June 2009, 07:36
2 sudheer168,


Try to use my library QSerialDevice. See in attach

in library see method : QStringList serialDevicesAvailable()

sudheer168
25th June 2009, 10:59
Hi kuzulis,
thanks for your support.I linked the QSerialDevice library to my applications .As you told I used QStringList serialDevicesAvailable() to acces serialport devices that are available . The List shows the COM details ie names as COM1,COM2 .But I want hte friendly name of the com port ,I mean if I insert any USB device in the USB port system ,the device manager shows the friendly name along with COMn.
So using this library how can get the friendly name of the com port ,if not possible using this library tell me the alternative to do this.
So please help me to solve this problem.


With regards,
Sudheer

gsmiko
25th June 2009, 11:31
Hi gsmiko, thanks for your kind reply.
I gone through the example code which you have suggested me.I included the comdef.h, and Wbemidl.h in my QT application class and linked with wbemuuid.lib.But in if I used the API ,while compiling it is showing errors as ...


error C3861: 'CoInitializeEx': identifier not found, even with argument-dependent lookup
error C2065: 'COINIT_MULTITHREADED' : undeclared identifier


The errors I posted above are few that I occour.So how can I be out of these type of errors.
So please help me to solve this problem.

With Regards,
Sudheer.

The first step of course would be to build the example from msdn as is, to make sure everything is configured correctly. I think you're missing some includepaths, please post the complete build log. By the way, which compiler are you using, VC or MinGW?

kuzulis
25th June 2009, 11:31
2 sudheer168,

In Windows XP, etc. This library uses a method serialDevicesAvailable.

This method reads the Windows registry on the way:
"HKEY_LOCAL_MACHINE \ \ Hardware \ \ Devicemap \ \ Serialcomm"

You connect the converter USB <-> RS232 and look in the registry using regedit.exe - or do not appear in the registry entry ...

If the entry appears, then I think the method serialDevicesAvailable - detects the new device. You need to check :-)

kuzulis
25th June 2009, 11:41
oops ... Once again, I read your post - and I think that in this case, no!

because method serialDevicesAvailable () can get names:
or COM1 ... COMn
or / Device/Serial0 ... /Device/Serialn

While it may be the friendly name is also stored somewhere in the registry .. But I do not know where :(

Perhaps you need to use WMI - but it requires extra header files and libraries!

Question: What do you friendly name? :)

sudheer168
25th June 2009, 14:11
Hi gsmiko, thanq for your reply.I am using MSVS .Net 2003.


2 kuzulis,
me to gone through it ,I got values as COMn and names as / Device/Serial0 ... /Device/Serialn. I want to know friendly name because ,I have to know to which com port my
USB <-> RS 232 was connected and by knowing the COMn dynamically I can able to communicate using QExtserialport library . And one more thing does this Qserialdevice is platform independent.
So suggest me to solve this problem.

With regards,
sudheer.

gsmiko
25th June 2009, 16:05
Hmm, very interesting.
The easiest solution would be to send you a sample project, but I'm using msvc2008, so thats out of the question. I would be nice to see the full build log, and the parts of the code where you tried to integrate the sample code from msdn.

Ferric
20th January 2010, 05:59
Hi sudheer168,

Just wondering, did you find a solution to finding the friendly names of the serial ports? I have a similar problem.

squidge
20th January 2010, 08:59
If you use something like:



hDevInfo = SetupDiGetClassDevs (&GUID_CLASS_COMPORT, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

for (ii = 0; bOk; ii++)
bOk = SetupDiEnumDeviceInterfaces (hDevInfo, NULL, &GUID_CLASS_COMPORT, ii, &ifcData);
if (bOK) {
bOk = SetupDiGetDeviceInterfaceDetail (hDevInfo, &ifcData, pDetData, dwDetDataSize, NULL, &devdata);


(obviously you'll need some variables with the above too, and some more error checking. It's just a rough idea)
then you can get the friendly name like so:



bSuccess = SetupDiGetDeviceRegistryProperty (hDevInfo, &devdata, SPDRP_FRIENDLYNAME, NULL,
(PBYTE)fname, sizeof (fname), NULL);


You can also gets lots of other stuff, see http://msdn.microsoft.com/en-us/library/ms792967.aspx

(Obviously this is Windows specific)

Ferric
25th January 2010, 02:27
The following answer is based on the "enumerator" example from the "qextserialport-1.2win-alpha" folder:



void Loader::getCOMPortName()
{

QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
qDebug() << ("List of ports:\n");
qDebug() << ports.size();
for (int i = 0; i < ports.size(); i++)
{
qDebug() << ("port name: %s\n", ports.at(i).portName.toLocal8Bit().constData());
qDebug() << ("friendly name: %s\n", ports.at(i).friendName.toLocal8Bit().constData());
qDebug() << ("physical name: %s\n", ports.at(i).physName.toLocal8Bit().constData());
qDebug() << ("enumerator name: %s\n", ports.at(i).enumName.toLocal8Bit().constData());
qDebug() << ("===================================\n\n");
}
//return EXIT_SUCCESS;

}



The attachment shows what I get as debug output from my system.