Pages: [1]   Go Down
Author Topic: Serial interfacing with Win32 console app  (Read 1186 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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;
      }
Logged

Florida, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 146
meow!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
           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
            );
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That seems to be working nicely.  Not sure why my way was working for the other board, though..  

Cheers.
Logged

Pages: [1]   Go Up
Jump to: