Solved

Trouble opening connection to serial port through "CreateFile"

Posted on 2004-09-07
4
793 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now