CreateFile() Serial Communication Issue [duplicate]
Asked Answered
N

1

5

I am trying to do some serial communication through my usb port (named COM15), and I am getting an error. This is the code where the error is occurring:

HANDLE myPortHandle = CreateFile("COM15",
                                  GENERIC_READ | GENERIC_WRITE,
                                  0,
                                  NULL,
                                  OPEN_EXISTING,
                                  0,
                                  NULL);

if (myPortHandle == INVALID_HANDLE_VALUE)
{
    DWORD lastError = GetLastError();
    cout<<"ERROR HERE! = "<<lastError<<endl;
}

Every time I compile the program, the handle to the opened serial port == INVALID_HANDLE_VALUE. I read in the CreateFile() msdn documentation to use GetLastError() in order to get "extended error information". Now... when I run the code, GetLastError() returns a value of: 2

In the msdn documentation of GetLastError(), it says:

"The Return Value section of the documentation for each function that sets the last-error code notes the conditions under which the function sets the last-error code."

I tried looking for the meaning of the '2' in the Return Value section of the CreateFile() msdn documentation and couldn't find the meaning anywhere.

QUESTION:

1) Why is this happening: myPortHandle == INVALID_HANDLE_VALUE?

2) Also, if anyone could direct me to where I need to look in the msdn documentation to find the meaning of the '2' returned by GetLastError(), that would be awesome!

Nought answered 12/1, 2015 at 19:51 Comment(3)
Search the web for working code, you are not inventing anything new.Catercornered
@UlrichEckhardt I did actually. I just was unlucky enough to find code that all used COM1 through COM9. Issue has been solved thou.Nought
This is a dupe a hundred times over, sadly neither Google nor SO search can match \\.\COM as an exact phraseKissee
R
13

Windows error codes are documented here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx

Your error code is ERROR_FILE_NOT_FOUND. Which means that COM15 cannot be found. According to this article you need to use the name: "\\\\.\\COM15".

CreateFile() can be used to get a handle to a serial port. The "Win32 Programmer's Reference" entry for "CreateFile()" mentions that the share mode must be 0, the create parameter must be OPEN_EXISTING, and the template must be NULL.

CreateFile() is successful when you use "COM1" through "COM9" for the name of the file; however, the message INVALID_HANDLE_VALUE is returned if you use "COM10" or greater.

If the name of the port is \\.\COM10, the correct way to specify the serial port in a call to CreateFile() is as follows:

CreateFile(
  "\\\\.\\COM10",     // address of name of the communications device
  fdwAccess,          // access (read-write) mode
  0,                  // share mode
  NULL,               // address of security descriptor
  OPEN_EXISTING,      // how to create
  0,                  // file attributes
  NULL                // handle of file with attributes to copy
);

NOTES: This syntax also works for ports COM1 through COM9. Certain boards will let you choose the port names yourself. This syntax works for those names as well.

Or alternatively from the documentation to CreateFile itself:

The CreateFile function can create a handle to a communications resource, such as the serial port COM1. For communications resources, the dwCreationDisposition parameter must be OPEN_EXISTING, the dwShareMode parameter must be zero (exclusive access), and the hTemplateFile parameter must be NULL. Read, write, or read/write access can be specified, and the handle can be opened for overlapped I/O.

To specify a COM port number greater than 9, use the following syntax: \\.\COM10. This syntax works for all port numbers and hardware that allows COM port numbers to be specified.

Reamer answered 12/1, 2015 at 19:58 Comment(7)
Your awesome. If anyone says otherwise then they are lying to you.Nought
Writing a duplicate answer is not awesome :( Doesn't change that David is awesome.Kissee
@Ben None of my searches led to SO questions. Sorry.Reamer
I am also having the same problem. The only difference in my case is instead of a real serial port I am using "socat" (cygwin, also if I cat to this file, could see the file throws legit data) command to create a file. Any help is appreciated :)Coccidiosis
@SH' If it is the same problem, then the solution is hereReamer
I forgot to say, my code is in C. I tried this solution except fdwAccess using GENERIC_READ|GENERIC_WRITE. But it throws error code 2 :(Coccidiosis
@SH' You'll want to ask a new question. Make sure you provide a clean minimal reproducible example.Reamer

© 2022 - 2024 — McMap. All rights reserved.