How to open comport more than 9 in VC++ code

56 Views Asked by At

I want to open comport which is higher than 9 in VC++ code. The following code can open comport which is higher than 9. But, the result I am getting in combo box is "\.\COM10". I don't want "\.\" before COM name in combo box. Please help me to resolve this issue. My code :

CString str;
int i;
for(i=1;i<30;i++)
{
    str.Format("\\\\.\\COM%d",i);
    ptrLC->comPort.CloseCommPort();

    if(ptrLC->comPort.OpenCommPort(str))
    {
        m_cCommPort.AddString(str);
        ptrLC->comPort.CloseCommPort();
    }
 }
1

There are 1 best solutions below

0
User0804 On

Thank you for your contribution...The above issue is resolved. I did changes in above code which helps me to open comport higher than 9. And also combo box Drop Down list does not contain \.\ before COM name.

Corrected code:

CString str, str1;
int i;
for(i=1;i<30;i++)
{
str.Format("COM%d",i);
ptrLC->comPort.CloseCommPort();

str1 = "\\\\.\\" + str;     // this is a I/O string format which helps to open comport higher than 9. 

if(ptrLC->comPort.OpenCommPort(str1))
{
    m_cCommPort.AddString(str);
    ptrLC->comPort.CloseCommPort();
}
}