Author Topic: Cannot open serial port in XP  (Read 3603 times)

Brownout

  • Member
  • ***
  • Posts: 14
Cannot open serial port in XP
« on: January 13, 2014, 07:35:41 pm »
First of all, the port opens and works fine with Hyperterm.  However, I'm trying to run a simple program to open and setup the port, but cannot make it work.  Here is a code snip:

Code: [Select]
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>

int _tmain(int argc, _TCHAR* argv[])
{
   DCB dcb;
   HANDLE hCom;
   BOOL fSuccess;
   hCom = CreateFile( (LPCTSTR)"COM1",
                    GENERIC_READ | GENERIC_WRITE,
                    0,    // must be opened with exclusive-access
                    NULL, // no security attributes
                    OPEN_EXISTING, // must use OPEN_EXISTING
                    0,    // not overlapped I/O
                    NULL  // hTemplate must be NULL for comm devices
                    );

   if (hCom == INVALID_HANDLE_VALUE)
   {
       // Handle the error.
       printf ("CreateFile failed with error %d.\n", GetLastError());
   while(!kbhit());
   Sleep(50);
       return (1);
   }


This always fails and returns error code 2.  I successfully use COM1 with Hyperterm, but I always close the terminal program before trying this code.


















Brownout

  • Member
  • ***
  • Posts: 14
Re: Cannot open serial port in XP
« Reply #2 on: January 13, 2014, 08:51:35 pm »
Jan, the suggestion worked:  Project>Properties>General>Character Set = Use MultiByte Character Set, though I don't understand why.  But it's working!  And I'll investigate why that solution works.

Thanks!

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 2597
    • Lakeview Research
Re: Cannot open serial port in XP
« Reply #3 on: January 13, 2014, 09:33:50 pm »
Good! Thanks for reporting back!