|
I have a small question
How can I use the returned array (UintArray) to populate the combobox ??
I am trying but as the Addstring function takes String as argument , its not working ..
when I try to explicity cast it with CString it works, but the values returned are just garbage ...
can any one help me achieving this please !!!
thanks
|
|
|
|
|
Hello!
This code has a problem with other devices starting with "Com". For instance, I have a device named "CompositeBattery" (laptop computer), which gets inserted into the ports list as "COM0".
Apart from that, nice code. Just what I was looking for.
|
|
|
|
|
Sorry, I guess I should have checked your home page
Everybody, go here for the latest version: http://www.naughter.com/enumser.html
|
|
|
|
|
Hy...Anybody help me in getting code for creating a virtual serial port..
Thanks
|
|
|
|
|
Hi,
Have you found any information on how to create a virtual comm port?
Thank you,
Jean-Luc
|
|
|
|
|
Hi,
I have found another method that I tested successfully on Windows 95 (should work on other windows platforms). It uses the EnumPorts function.
(I took my inspiration from another author, Timothy J. Blue, http://multiverse.com/~mr.blue/enum_ports.html)
int i;
// pointer to port info struct
PPORT_INFO_2 pInfo = NULL;
// how many bytes needed to hold all port info
DWORD dwNeeded = 0;
// how many bytes filled after info is gotten
DWORD dwReturned = 0;
// Find out how much space is needed for pInfo
EnumPorts(NULL, 2, (LPBYTE)pInfo, 0, &dwNeeded, &dwReturned);
// allocate space for the port info
pInfo = (PPORT_INFO_2)malloc(dwNeeded);
if (pInfo == NULL) {
// handle error...
}
else {
EnumPorts (NULL, 2, (LPBYTE)pInfo, dwNeeded, &dwNeeded, &dwReturned);
// loop through all the ports returned looking
// for the type we want
for (i = 0; i < (int)dwReturned; i++) {
// We found a port!
// If it is a serial port, its name is COMx
if (strncmp(pInfo[i].pPortName, "COM", 3) == 0)
fprintf(stderr, "%s\t%s\n",
pInfo[i].pPortName,
pInfo[i].pDescription);
}
// free space allocated for port info
free(pInfo);
pInfo = NULL;
}
Friendly yours, Chris
|
|
|
|
|
In NT/W2K etc. I look at the entries in HKLM\HARDWARE\DEVICEMAP\SERIALCOMM and all serial ports will have an entry here unless it is in use by the kernel debugger. These entries get created automatically when the driver creates the symbolic link name for the port.
Cheers,
Phil
|
|
|
|
|
I'm using WinNT4. I get two COM0 when I use your library. After debugging, I find that there are devices named comxxxx where xxxx is anything (eg comDet). In this case, the _ttoi will return 0 because it failed to translate 'det' to a number.
In other words, even if the first three characters are com, it may not be a com port. You have to verify that the trailing characters are all numbers.
|
|
|
|
|
Hello, you emailed me that you have updated the code. However, when I try to download from this site, it seems to be unchanged. Could you please notify me when the codes are available for download at this site?
On another matter, I think you can optimize the codes a bit
these codes
// Go to next NULL character
while(szDevices[i] != _T('\0'))
i++;
// Bump pointer to the next string
i++;
could be replaced by these codes
// jump past the string
i += nLen;
++i;
Also, the loop could be changed from
for (;;)
{
...
// The list is double-NULL terminated, so if the character is
// now NULL, we're at the end
if (szDevices[i] == _T('\0'))
break;
}
to this
while(szDevices[i] != _T('\0'))
{
...
}
BTW, thanks for providing such a useful function.
|
|
|
|
|
Okay. I just found the updated codes on your homepage. It would be nice if you could update the codeproject page as well. Anyway, using google, I found this page but I didn't notice your hompage at all.
Another note is that your IsNumeric function would detect "com" as numeric. Since you 'assume the best', you would detect an empty string as numeric which I think is slightly incorrect.
|
|
|
|
|
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!??
|
|
|
|