473,414 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

CreateFile Windows COM1

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
11 29543
donbock
2,426 Expert 2GB
Try adding a colon to the filename: "COM1:".
Nov 3 '08 #2
no good. I have even tried (recommended by some) ""////.//COM1" :(
Nov 3 '08 #3
Banfa
9,065 Expert Mod 8TB
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
9,065 Expert Mod 8TB
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
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
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
9,065 Expert Mod 8TB
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
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
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
@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
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

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

Similar topics

1
by: Chuck Rittersdorf | last post by:
Hi There I am having a problem using the win32 API from VB6. I am trying to send a command string to a printer(zebra TLP 2742) on LPT1 using the folowing API functions CreateFile and...
3
by: Glen | last post by:
Hello. Can some one explain me why below example work LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; RECT rect;
3
by: Antoine | last post by:
Hello, I'm writing a program to send requests to my wlan pocket pc device (UIO1: driver) in C#. Here how I import CreateFile functions from coredll.dll with DllImport: public static extern...
1
by: Steve N. | last post by:
I'm having trouble getting a handle returned from the following code within a managed class: HANDLE m_hSerialComm = CreateFile("\\\\.\\COM1\0\0\0", GENERIC_READ|GENERIC_WRITE, 0, 0,...
0
by: JohnnyBoy | last post by:
I am playing with a Microsoft code example that uses the creatFile call to open a stream to a serial COM port. When I execute the example, the createFile call fails with an error code 5...
5
by: Lee | last post by:
I am getting this error trying to run a win forms .net app using the .net 2.0 serial port control. This occurs when the app tries to open the port. I knwo this is some type of security access...
29
by: mastermagrath | last post by:
Hi, Sorry for the possibly silly question. Am i right in saying that the C library i use on a windows machine really utilises the windows API for all its operations e.g. opening files, printing...
5
by: =?Utf-8?B?R2FyeQ==?= | last post by:
Been struggling with the code below for the last couple of days: m_hSerialHandle = CreateFile("COM1:", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, /*FILE_FLAG_OVERLAPPED*/0, NULL); ...
5
by: DotNetDanny | last post by:
Hello Machine: Windows Vista Business, standalone machine (no domain). Installed an old classic ASP webapplication in IIS7, running under a new app.pool with 'NETWORK SERVICE' account (using...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.