Link to home
Start Free TrialLog in
Avatar of komeisa
komeisa

asked on

Trouble opening connection to serial port through "CreateFile"

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.
Avatar of drichards
drichards

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?
Avatar of komeisa

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of drichards
drichards

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of komeisa

ASKER

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.