Quick question to those of you who have gotten a USB Arduino board talking with a Windows console application. Can anyone spot what I'm doing wrong here? The code below works fine with my USB->Serial adapter based around the Prolific chip, but can't seem to find the port when used with my Arduino. The Arduino host software communicates fine with the board. This code, however, consistently returns ERROR_FILE_NOT_FOUND. Any thoughts?
hSerial = CreateFile("COM15",
if (hSerial == INVALID_HANDLE_VALUE)
if (GetLastError() == ERROR_FILE_NOT_FOUND)
printf("port does not exist");
printf("some other error");
Here is a snippet of code from one of my C++ programs for opening a com port as a handle.
You have to open it as "\\.\com1" for example. Of course in C++, a backslash in a string has to be doubled to avoid it being treated as an escape sequence (gee, thanks Microsoft, for not using "/" like Unix does!).
char comport_name ;
sprintf (comport_name, "\\\\.\\COM%d", comport);
HANDLE comhandle = CreateFileA (
GENERIC_READ | GENERIC_WRITE,
0, // no sharing
NULL, // default security attributes
That seems to be working nicely. Not sure why my way was working for the other board, though..