Opening Serial (COM) Port Error - Windows 10

2.2k views Asked by At

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.

0

There are 0 answers