Get Number of Serail Port in Computer
-
Hi, How can I know the number of serial ports on my computer using code? Thanks,
-
Hi, How can I know the number of serial ports on my computer using code? Thanks,
Is Another serial port enumerator[^] helpful? ;)
-
Is Another serial port enumerator[^] helpful? ;)
Thanks Hamid
-
Thanks Hamid
You're welcome. :)
-
Hi, How can I know the number of serial ports on my computer using code? Thanks,
You may try this ! #define MAX_AVAIL_COM_PORTS 250 int i = 0; char chTemp[3]; CString strCOMPort; HANDLE hndCurrCOMPortObj; for (i = 0; i < MAX_AVAIL_COM_PORTS; i ++) { itoa (i + 1, chTemp, 10); strCOMPort = "COM"; strCOMPort += chTemp; // Create handle for selected COM port and check its Avilability hndCurrCOMPortObj = CreateFile (strCOMPort,GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); if (INVALID_HANDLE_VALUE == hndCurrCOMPortObj) { strCOMPort += strCOMPort + " port not available !!"; AfxMessageBox (strCOMPort); } else { strCOMPort += " port Available !!"; AfxMessageBox (strCOMPort); CloseHandle (hndCurrCOMPortObj); } }
-
You may try this ! #define MAX_AVAIL_COM_PORTS 250 int i = 0; char chTemp[3]; CString strCOMPort; HANDLE hndCurrCOMPortObj; for (i = 0; i < MAX_AVAIL_COM_PORTS; i ++) { itoa (i + 1, chTemp, 10); strCOMPort = "COM"; strCOMPort += chTemp; // Create handle for selected COM port and check its Avilability hndCurrCOMPortObj = CreateFile (strCOMPort,GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); if (INVALID_HANDLE_VALUE == hndCurrCOMPortObj) { strCOMPort += strCOMPort + " port not available !!"; AfxMessageBox (strCOMPort); } else { strCOMPort += " port Available !!"; AfxMessageBox (strCOMPort); CloseHandle (hndCurrCOMPortObj); } }
It won't work for ports having number
>= 10
. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
It won't work for ports having number
>= 10
. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Thanks ! i didn't knew this !! but can u plz tell me the reason why this doesn't work ? and can you give a solution to it, bcoz i need the requirement must be fullfilled in a small code rather that importing those classes !!
kapardhi wrote:
i didn't knew this !!
I suspected so. :rolleyes:
kapardhi wrote:
but can u plz tell me the reason why this doesn't work ?
Because you must use
UNC
[^] names for serial ports with number>= 10
, e.g. port12
UNC
is\\.\COM12
(you may use alwaysUNC
names). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]