Search code examples
c++visual-c++serial-portserial-communication

Debug Assertion Failed during opening COM port


I'm working on a program to take readings from a proximity sensor using Arduino UNO. While I can get the readings just fine using Arduino's build-in Serial Monitor, somehow I cannot open the same port from MS VC++.

Following is (one part of) the program:

int main(void)
{
    /*used for port"COM13"*/
    HANDLE hCom=INVALID_HANDLE_VALUE;

    char input[30];
    string ss,ss1,ss2,ss3,ss4;

    /*Open "COM13"*/
    hCom=CreateFile("COM13",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);

    if(hCom==INVALID_HANDLE_VALUE)
    {
        printf("can't open file");
    }

    /*Communication Setting*/
    DCB dcb;
    memset(&dcb,0,sizeof (DCB));
    dcb.DCBlength=sizeof (DCB);
    dcb.BaudRate=CBR_9600;
    dcb.ByteSize=8;
    dcb.Parity=NOPARITY;
    dcb.StopBits=ONESTOPBIT;
    SetCommState(hCom,&dcb);

    while(1)
      {
      //using the data string inputs, printout the readings, process it etc... 
      }
}

When I try to debug it, I'll get this error:

Please enlighten me with this error

and I got can't open file from debug windows showing there is problem during port opening.

Some additional infos:

  1. Why VC++? I'm also using OpenCV and some mathematical computation in the same program so it is easier for me to work in VC++
  2. I've also tested my UNO program with TeraTerm for data readings with no problem (=no problem with my UNO)
  3. I've tested above program with another microcontroller(non-Arduino) with no problem.

I'll upload my UNO program if needed.

Thanks in advance!


Solution

  • From the MSDN page on CreateFile:

    To specify a COM port number greater than 9, use the following syntax: "\.\COM10". This syntax works for all port numbers and hardware that allows COM port numbers to be specified.