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:
and I got can't open file
from debug windows showing there is problem during port opening.
Some additional infos:
I'll upload my UNO program if needed.
Thanks in advance!
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.