Solved

Trouble opening connection to serial port through "CreateFile"

Posted on 2004-09-07
4
796 Views
Last Modified: 2010-04-24
Hey Everybody,

I am trying to get access to my serial port through the CreateFile Method like this

HANDLE portHandle = CreateFile( "COM2",  GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0);

but it keeps giving me an invalid handle. I suspect it is the name of the port. I want to access comm port 2 (like if I were in hyperterminal it would be com2) but have no clue if I am using the right name here. Any help would be greatly appreciated.
0
Comment
Question by:komeisa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 12002626
This looks correct.  If your result is portHandle == INVALID_HANDLE_VALUE, what error is reported by GetLastError()?  Are you sure COM2 is configured on your machine?
0
 

Author Comment

by:komeisa
ID: 12008004
I figured out the problem. My application is in managed C++. And I guess it doesn't like the CreateFile Routine. Any clue on how I might get this to work in a managed C++ environment
0
 
LVL 19

Accepted Solution

by:
drichards earned 100 total points
ID: 12009339
I use CreateFile in managed C++ and it works just fine.  You can't see the handle value in the debugger, but the call succeeds and communications work.  In the debugger the handle value goes from <undefined value> to {void} when CreateFile is called.
0
 

Author Comment

by:komeisa
ID: 12011517
Yeah,

I finally got it to work, it wasn't because of the managed c++. I think it was an error in my code.  I reailized that I was connecting okay to the port but when I would try to write something out onto the port it would come back with an error. It turns out I wasn't passing the buffer with the data to transmit correctly. Thanks for you help, I really appreciate it.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c++ class logger 6 108
Need help with a query 7 55
How to 4 54
Visual Studio 2013 and Eclipse Neon 6 73
The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question