By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,916 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,916 IT Pros & Developers. It's quick & easy.

CreateFile Windows COM1

P: 73
Trying to open COM1 in XP using CreateFile and I keep getting INVALID_HANDLE_VALUE.

here is the code I am using:

//main
.............
.....
....
..
.
Expand|Select|Wrap|Line Numbers
  1. HANDLE serialhCom = CreateFile("COM1",            
  2.                        GENERIC_READ | GENERIC_WRITE,
  3.                        0,                    
  4.                        NULL,                 
  5.                        OPEN_EXISTING,        
  6.                        FILE_FLAG_OVERLAPPED, 
  7.                        NULL);                
  8.  
  9.  
  10.      if (serialhCom != INVALID_HANDLE_VALUE) 
  11.      {
  12.        SetupComm(serialhCom, inLen, outLen);
  13.        serialSetTimeouts(serialhCom, MAXDWORD, 0, 0, 0, 0);
  14.      }
  15.      else
  16.      {
  17.        printf ("Could not open COM1\n");
  18.      }
  19.  
  20.  
.
..
...
......
.............
I used GetLastError to get more details. and I received error # 123?

I have searched Google and looked at MSDN and could not info to help me solve the problem. There are dozens of articles explaining CreateFile and how one should use it, but no real substance. I have checked my device manager and my COM1 is active with no conflicts. I have checked my registry and have confirmed my COM1 in memory.

Please advise.

Thanks,
Sevak
Nov 3 '08 #1
Share this Question
Share on Google+
11 Replies


Expert 100+
P: 2,400
Try adding a colon to the filename: "COM1:".
Nov 3 '08 #2

P: 73
no good. I have even tried (recommended by some) ""////.//COM1" :(
Nov 3 '08 #3

Banfa
Expert Mod 5K+
P: 8,916
Error 123... you can look up error code in winerror.h where you would find

//
// MessageId: ERROR_INVALID_NAME
//
// MessageText:
//
// The filename, directory name, or volume label syntax
// is incorrect.
//
#define ERROR_INVALID_NAME 123L // dderror
You may have tried "////.//COM1" but did you try it with the correct slash "\\\\.\\COM1"?

Actually I have to say that "COM1" has always worked for me I assume you computer has a COM1?
Nov 3 '08 #4

Banfa
Expert Mod 5K+
P: 8,916
Oh and 1 final thought, COM1 isn't already in use by some other program is it? Only one process can have access to the COM port at a time (in fact I would question if you can have more than 1 handle open to the COM port).
Nov 3 '08 #5

P: 73
weird!

I tried the following:

1. char str[8];
//port was passed in through the function parameter
sprintf(str, "COM%d", port);

// "str" was used as the first parameter in CreateFile
2. "COM1"
3. "com1"
4. "COM1:"
5. "\\\\.\\COM1"

and finally....

6. "TEXT("COM1") which is the one that finally went through and did not give me an "INVALID_HANDLE_VALUE".

All of these methods (2,3,4,5) were found on google. The first method was working on Borland C and did not have any problems. I moved the same file which compiled on my other machine to the new computer that has Visual Studio 2008 and that's when the problem started.

I havent tried to see if my program spits anything out of the serial port yet, but at least I am passing through the section which was failing before.

I do have COM1 and no other program uses it. I have tried COM1 on windows Terminal software and it does communicate with my other computer.

-Sevak
Nov 3 '08 #6

P: 73
just for kicks:

I tried to open COM1 with 2 different handles and the error code for that is "5"

which is "ERROR_ACCESS_DENIED"
Nov 3 '08 #7

Banfa
Expert Mod 5K+
P: 8,916
and finally....

6. "TEXT("COM1")
Damn, sorry yes that would be required to turn it into a mu;tibyte string which is now the default for windows.
Nov 3 '08 #8

P: 73
I am not sure what that is? (multibyte)

Oh and by the way, it works. I am getting data out of COM1.

My question now is, how do I make this dynamic. Can I pass a var to TEXT()?
It doesn't look like I can because it takes in a "quote"

How can I do this?

-Sevak
Nov 4 '08 #9

P: 2
I have the same question:

Does anyone know how to generate the output of TEXT("COM1") in a dynamic way?

Thanks,
Ian.
Mar 18 '09 #10

P: 1
@ianinini

You shouldn't have to. You could use the simpler macro _T("COM1") to achive the same results. This way if the application is compiled using UNICODE then the string expands to unicode else it remains a regular ASCII string.

If you are compiling an ASCII application and wish to create a UNICODE (wide) string I use MultiByteToWideChar(), try this function:

Expand|Select|Wrap|Line Numbers
  1. // create a WIDE character string from an ASCII string
  2. // wlen is the size of the WIDE character buffer -- it must be at least 2*(strlen(src)+1)
  3. BOOL wstrcpy(LPWSTR wdest, LPCSTR src, int wlen)
  4. {
  5.     return (MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,src, strlen(src)+1, wdest, wlen) != 0);
  6. }
  7.  
  8. // I use it as follows
  9.  
  10. WCHAR wszString[100]; 
  11. wstrcpy(wszString, "ascii string here", 90);
  12.  
  13.  
May 12 '09 #11

P: 2
Thanks bmillerqw. I've actually managed to make something work using MultiByteToWideChar().

I don't think you can use TEXT() or_T() when for example "COM1" is generated at
std::string s("COM1");
TEXT(s.str());

However the MultiByteToWideChar() type methods work fine!

Thanks again!
May 12 '09 #12

Post your reply

Sign in to post your reply or Sign up for a free account.