Click here to Skip to main content
15,918,275 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my first code of serial communications with VC++

I try to call CreateFile like as follows:

m_hcomm = CreateFile(L"//./" + Portname,
GENERIC_READ | GENERIC_WRITE,
0,0, OPEN_EXISTING, 0,0);

made error m_hcomm = INVALID_HANDLE_VALUE.

Now
The pc has one serial port and connected with RS232 cable with another port of PC.
(Is this required condition when call CreateFile?)

The Last error code = 2.

As I am novice of serial comm. What do I take care of code or environment?

Thank in advance.

What I have tried:

More 2 days wasted for this problem.
Posted
Updated 18-Jun-17 21:28pm

My tip is, that the Portname string is somehow incorrect. (I would use it without slashes and dot) Look in the system settings that the port is available and what the correct name of the port is.

Read the Micorsoft documentation with some interesting code snippets.

Read this article about Creating a Serial communication on Win32
 
Share this answer
 
Error code 2 is ERROR_FILE_NOT_FOUND ("The system cannot find the file specified.").

You are trying to use the file name prefix to support COM ports >= 10. But these use back slashes ("\\.\") instead of forward slashes (//./). So try this:
m_hcomm = CreateFile(L"\\\\.\\" + Portname,
    GENERIC_READ | GENERIC_WRITE,
    0,0, OPEN_EXISTING, 0,0);

If you still get an error check if the port name is valid and the port exists.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900