473,396 Members | 1,871 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,396 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 29540
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.