|
There is a better way to enumerate ports...
You should use the SetupAPI's to enumerate devices of class Ports this method lists all available ports including virtual ones.
// Christian
|
|
|
|
|
Hi
Would it work on a windows CE, and in a non MFC environment ?
Best Regs
Chris
|
|
|
|
|
hi,
I am also looking for a same thing as you but in MFC win32 also ok, can u pls give a idea or suggestion,
Thanks
|
|
|
|
|
do you know how can i know if on windows 2000 a modem is
installed ???
thank you
|
|
|
|
|
VC6, SP3
I tried modifying the code to use STL, the .h file becomes:
#include <vector>
using namespace std;
void EnumerateSerialPorts(vector<uint>& ports);
and replaced the ports.Add() to ports.push_back() in the .cpp file.
During compiling, encountered warning messages, am tearing my hair now. I have used STL in my other MFC project, everything is fine. Please help.
The warning messages are:
enumser.cpp
c:\program files\microsoft visual studio\vc98\include\xmemory(39) : warning C4100: '_P' : unreferenced formal parameter
c:\program files\microsoft visual studio\vc98\include\xmemory(41) : warning C4100: '_P' : unreferenced formal parameter
c:\program files\microsoft visual studio\vc98\include\vector(249) : warning C4663: C++ language change: to explicitly specialize class template 'vector' use the following syntax:
template<> class vector<bool,class std::allocator<unsigned="" int=""> > ...
c:\program files\microsoft visual studio\vc98\include\vector(156) : warning C4018: '<' : signed/unsigned mismatch
c:\program files\microsoft visual studio\vc98\include\vector(156) : while compiling class-template member function 'void __thiscall std::vector<unsigned int,class="" std::allocator<unsigned="" int=""> >::insert(unsigned int *,unsigned int,const unsig
ned int &)'
c:\program files\microsoft visual studio\vc98\include\vector(167) : warning C4018: '<' : signed/unsigned mismatch
c:\program files\microsoft visual studio\vc98\include\vector(156) : while compiling class-template member function 'void __thiscall std::vector<unsigned int,class="" std::allocator<unsigned="" int=""> >::insert(unsigned int *,unsigned int,const unsig
ned int &)'
c:\program files\microsoft visual studio\vc98\include\xmemory(37) : warning C4100: '_P' : unreferenced formal parameter
c:\program files\microsoft visual studio\vc98\include\xmemory(68) : see reference to function template instantiation 'void __cdecl std::_Destroy(unsigned int *)' being compiled
main.cpp
c:\program files\microsoft visual studio\vc98\include\xmemory(39) : warning C4100: '_P' : unreferenced formal parameter
c:\program files\microsoft visual studio\vc98\include\xmemory(41) : warning C4100: '_P' : unreferenced formal parameter
c:\program files\microsoft visual studio\vc98\include\vector(249) : warning C4663: C++ language change: to explicitly specialize class template 'vector' use the following syntax:
template<> class vector<bool,class std::allocator<unsigned="" int=""> > ...
c:\program files\microsoft visual studio\vc98\include\vector(156) : warning C4018: '<' : signed/unsigned mismatch
c:\program files\microsoft visual studio\vc98\include\vector(156) : while compiling class-template member function 'void __thiscall std::vector<unsigned int,class="" std::allocator<unsigned="" int=""> >::insert(unsigned int *,unsigned int,const unsig
ned int &)'
c:\program files\microsoft visual studio\vc98\include\vector(167) : warning C4018: '<' : signed/unsigned mismatch
c:\program files\microsoft visual studio\vc98\include\vector(156) : while compiling class-template member function 'void __thiscall std::vector<unsigned int,class="" std::allocator<unsigned="" int=""> >::insert(unsigned int *,unsigned int,const unsig
ned int &)'
c:\program files\microsoft visual studio\vc98\include\xmemory(37) : warning C4100: '_P' : unreferenced formal parameter
c:\program files\microsoft visual studio\vc98\include\xmemory(68) : see reference to function template instantiation 'void __cdecl std::_Destroy(unsigned int *)' being compiled
StdAfx.cpp
Linking...
Please help.
|
|
|
|
|
All my code is set to compile at level 4. I do not think STL can handle this yet. Set the warning level to 3 and the warnings should go away
|
|
|
|
|
Be sure to Rebuild everything after changing the Warning level.
|
|
|
|
|
It's a very nice class, however it doesn't seem to find the virtual serial ports created by my BocaBoard (www.bocaresearch.com).
Was this capability intented
|
|
|
|
|
It was intended to work with any Win32 ports. Would it be possible for you to debug into the code and check what error it gives. Hopefully by that, I will be able to suggest a fix.
|
|
|
|
|
I'm using the following code for enumeration. Tested succesfuly on Win98, NT and 2000. Hope it will work for you.
No CreateFile/CloseHandle is needed.
BOOL IsPortAvailable(int nPort)
{
TCHAR szPort[15];
COMMCONFIG cc;
DWORD dwCCSize;
_stprintf(szPort, _T("COM%d"), nPort);
// Check if this port is available
dwCCSize = sizeof(cc);
return GetDefaultCommConfig(szPort, &cc, &dwCCSize);
}
void EnumerateSerialPorts(CUIntArray& ports)
{
...
ports.RemoveAll();
for (int nPort = 1; nPort <= 255; nPort++)
if (IsPortAvailable( nPort ))
{
ports.Add( nPort );
}
}
|
|
|
|
|
I'm using the following code for enumeration. Tested succesfuly on Win98, NT and 2000. Hope it will work for you.
No CreateFile/CloseHandle is needed.
BOOL IsPortAvailable(int nPort)
{
TCHAR szPort[15];
COMMCONFIG cc;
DWORD dwCCSize;
_stprintf(szPort, _T("COM%d"), nPort);
// Check if this port is available
dwCCSize = sizeof(cc);
return GetDefaultCommConfig(szPort, &cc, &dwCCSize);
}
void EnumerateSerialPorts(CUIntArray& ports)
{
...
ports.RemoveAll();
for (int nPort = 1; nPort <= 255; nPort++)
if (IsPortAvailable( nPort ))
{
ports.Add( nPort );
}
}
|
|
|
|
|
Nice one. I'll update the source ASAP. One thing i might do is use \the format "\.\COMX" instead of "COMx" as Windows 9x cannot handle the latter format for ports with a number greater than 9.
|
|
|
|
|
I've updated the distribution on my web site to now use this method
|
|
|
|
|
I've tried using your method, but I wasn't successful in detecting the virtual serial port created by a PC card. Please help. Thanks in advance.
|
|
|
|
|
With the method of enumeration being used here, a virtual COM port will ONLY be work if the driver for that virtual port supports opening as a DOS device via CreateFile() or similar.
Additionally, the port may not follow the same naming convention and any system that performs enumeration on the string "\\.\ComX" will never find a COM port named "commap001" for instance.
It is likely that enumeration can be done using the WMI interface but this doesn't appear to exits pre ME (i.e. 98 is a no go) so I'm trying to find examples for SetupAPI (assuming this API will ALSO work for Win98).
Why oh why, does Microsoft not just provided us an interface, it surely CANNOT be THAT much to ask!??
|
|
|
|
|
FYI, The version on my web site now includes the following mechanisms to determine COM ports:
EnumerateSerialPorts1: Simple call to CreateFile
EnumerateSerialPorts2: QueryDosDevice, Only available on NT kernel
EnumerateSerialPorts3: GetDefaultCommConfig
EnumerateSerialPorts4: Uses SetupAPI calls. You will need to test this to see what older OS's this supports.
EnumerateSerialPorts5: Uses EnumPorts.
For a serial port the port has to appear as "\\.\ComX" for it to be a valid serial port. This will appear as a symlink back to the real device name such as "commap001".
I will look into using WMI for an update.
|
|
|
|
|
How to register the COM port driver if its not registered??? I'm not able to enumerate the drivers. it seems the driver is not installed.
|
|
|
|
|
Just to let you know that I have released an update to my web site (www.naughter.com) which shows how to use WMI to enumerate serial ports. This brings all the different ways of doing the enumeration to 6!!!. The question of providing an inteface has now once and for all been addressed by the introduction of WMI by MS, but it seems that the serial port class in WMI is only available on NT 4 SP4, 2000, XP and 2003.
Regards,
|
|
|
|