Solved

Trouble opening connection to serial port through "CreateFile"

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

14 Experts available now in Live!

Get 1:1 Help Now