0

I am trying to open a serial port so that I can receive Uart data from a device with the standard CreateFile() code below;

HANDLE hComm;                          // Handle to the Serial port
char  ComPortName[] = "COM4";  // Name of the Serial port(May Change) to be opened,

/*---------------------------------- Opening the Serial Port -------------------------------------------*/

hComm = CreateFile(ComPortName,                  // Name of the Port to be Opened
    GENERIC_READ | GENERIC_WRITE, // Read/Write Access
    0,                            // No Sharing, ports cant be shared
    NULL,                         // No Security
    OPEN_EXISTING,                // Open existing port only
    0,                            // Non Overlapped I/O
    NULL);                        // Null for Comm Devices

if (hComm == INVALID_HANDLE_VALUE)
    printf("\n    Error! - Port %s can't be opened\n", ComPortName);
else
    printf("\n    Port %s Opened\n ", ComPortName);

However every time it returns an invalid handle. I am a bit of a newby I'm afraid, but I have had a good look into to this and I can't see any reason for this?! I have tried "COM4:" as suggested in another post, and "\\\\.\\COM4" (although I am aware this should only be valid for com port 10 and over), but still no joy! The port is definitely there as I can connect and receive data fine using both Teraterm and RealTerm, and it isn't a case that it is already in use either.

I'm using windows 10 on a Mac with Boot Camp and wondering if this could be an issue? I read in one thread the there is an problem if windows has not being properly activated but it was a bit vague.

Any advice would be much appreciated, many thanks in advance.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Call [`GetLastError()`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms679360(v=vs.85).aspx) to find out why it failed. – 001 Nov 20 '17 at 20:45
  • thanks, I'm actually just looking in to that at the moment. I'm taking the advice from https://msdn.microsoft.com/en-us/library/windows/desktop/ms680582(v=vs.85).aspx. Although it is not quite working yet. My C++ isn't great unfortunately so I'm kind of having to learn as I go....will update asap. – George Howell Nov 20 '17 at 20:51
  • Also, the "\\.\" prefix is valid for any DOS device. DOS namespace path processing transforms "COM1" - "COM9" in any existing directory to "\\.\COM1" - "\\.\COM9", and then to the native object paths "\??\COM1" - "\??\COM9". For the native "\??" object directory (i.e. NTAPI, not WinAPI), the Object Manager checks the local and global DOS device directories of the caller's logon session. The global DOS device directory is normally "\GLOBAL??". The "COM4" DOS device, if present, is a link from "\GLOBAL??\COM4" to a native device path such as "\Device\Serial3". – Eryk Sun Nov 20 '17 at 21:31
  • cheers, still a bit stuck on how I use the GetLastError() function but I'll get there. – George Howell Nov 20 '17 at 22:46
  • ERROR_INVALID_NAME 123 (0x7B) The filename, directory name, or volume label syntax is incorrect. – George Howell Nov 20 '17 at 23:24
  • Okay, so I think I've got the GetLastError() function right now and it returns error code 123, which is as above. I'm a completely stumped as to whats going on now so I'll have to do a bit more research. – George Howell Nov 20 '17 at 23:27
  • Probably you've defined `UNICODE`, so `CreateFile` is actually `CreateFileW`, which requires `ComPortName` to be a [W]ide-character string (i.e. `wchar_t`). Using Unicode is a good thing, so don't take the easy way out by changing it to call `CreateFileA`. – Eryk Sun Nov 21 '17 at 00:23

0 Answers0