views:

593

answers:

4

I recently tried to get access to a serial communication using a Bluetooth usb dongle. I used the C code below, and keep getting error 5, which is “Access denied”. I am the administrator for the system (which seemed to be the common solution to this problem on the forums) and no other application is accessing the same port I am using (also another common solution). I’m running on a Windows Vista Home Basic 32bit system. I was wondering if anyone had a solution for this My C code is:

HANDLE hComm;

hComm = CreateFile( _T("\\.\COM3"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

if (hComm == INVALID_HANDLE_VALUE)
 printf("Error number: %ld\n", GetLastError());
else
 printf("success\n");
+3  A: 

I don't know if this is your problem or not, but I suspect you need to escape the backslashes in the path, like so: "\\\\.\\COM3"

Benji York
+1, I believe this is the answer. This is an insidious bug that will haunt C programmers on Windows until Windows is no more.
RBerteig
Actually this is only necessary for COM10 and higher. For lower COMs, COM3 will work as well
Eli Bendersky
A: 

In my experience the backslashes are not needed

hComm = CreateFile( _T("COM3"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
Tom Leys
Backslashes are not needed for COM1 through COM9; they ARE needed for COM10 and above.
Adam Rosenfield
+1  A: 

That does look like you have to escape your backslashes again. You can also verify that the COM port you're targeting exists on your system by using an object viewer, such as WinObj (http://technet.microsoft.com/en-us/sysinternals/bb896657.aspx), although I don't know if WinObj runs on Vista.

jjb
A: 

Thanks for the tips but it turns out the bluetooth passkey was not set properly and therefore it was denying access to the serial port.