Go Down

Topic: Serial interfacing with Win32 console app (Read 2054 times) previous topic - next topic


Hi All,

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?

     HANDLE hSerial;
     hSerial = CreateFile("COM15",  
     if (hSerial == INVALID_HANDLE_VALUE)
           if (GetLastError() == ERROR_FILE_NOT_FOUND)
                 printf("port does not exist");
                 return 0;
           printf("some other error");
           return 0;


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!).

Code: [Select]

           char comport_name [64];
           sprintf (comport_name, "\\\\.\\COM%d", comport);
           HANDLE comhandle = CreateFileA (
               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..  


Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131