PORTS Forum

Ports and Interfaces => Serial Ports => Topic started by: Brownout on January 13, 2014, 07:35:41 pm

Title: Cannot open serial port in XP
Post by: Brownout 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.
















Title: Re: Cannot open serial port in XP
Post by: Jan Axelson on January 13, 2014, 08:37:28 pm
Error 2 is file not found:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382%28v=vs.85%29.aspx

Maybe these will help:

http://social.msdn.microsoft.com/Forums/en-US/7b1d925b-1b7a-4311-8b32-11c1c34d376b/difficult-to-access-the-serial-com-port

http://stackoverflow.com/questions/5345077/how-to-use-win32-api-to-talk-to-a-com-port-rs232
Title: Re: Cannot open serial port in XP
Post by: Brownout 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!
Title: Re: Cannot open serial port in XP
Post by: Jan Axelson on January 13, 2014, 09:33:50 pm
Good! Thanks for reporting back!