Arduino Forum

Forum 2005-2010 (read only) => Software => Interfacing => Topic started by: grapeape on Jan 08, 2007, 12:37 am

Title: Serial interfacing with Win32 console app
Post by: grapeape on Jan 08, 2007, 12:37 am
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",  
                                   GENERIC_WRITE,
                                   0,
                                   0,
                                   OPEN_EXISTING,
                                   FILE_ATTRIBUTE_NORMAL,
                                   0);
     if (hSerial == INVALID_HANDLE_VALUE)
     {
           if (GetLastError() == ERROR_FILE_NOT_FOUND)
           {
                 printf("port does not exist");
                 return 0;
           }
           printf("some other error");
           return 0;
     }
Title: Re: Serial interfacing with Win32 console app
Post by: CosineKitty on Jan 08, 2007, 03:03 am
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 (
               comport_name,
               GENERIC_READ | GENERIC_WRITE,
               0,                  // no sharing
               NULL,               // default security attributes
               OPEN_EXISTING,
               FILE_ATTRIBUTE_NORMAL,
               NULL
           );
Title: Re: Serial interfacing with Win32 console app
Post by: grapeape on Jan 08, 2007, 09:47 am
That seems to be working nicely.  Not sure why my way was working for the other board, though..  

Cheers.