views:

42

answers:

2

Hi,

I am writing to open a port using this function:

HANDLE hFile = ::CreateFile(pszComName, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0,0);

// Check if we could open the device
if (hFile == INVALID_HANDLE_VALUE)
{
    DWORD hh=  ::GetLastError();

              error.Format(_T("test - [%d]"),hh); 

      AfxMessageBox(error,MB_ICONSTOP);
}

I cannot open the port and system error code I receive is 55: ERROR_DEV_NOT_EXIST 55 (0x37) from this list

what can i do to open the port? thanks

EDIT: I Enumerate Ports like this:

 for (UINT i=1; i<256; i++)
    {

      CString sPort;
      sPort.Format(_T("COM%d"), i);

      HANDLE hPort = ::CreateFile(sPort, GENERIC_READ | GENERIC_WRITE, 0, 0,OPEN_EXISTING,0, 0);
      if (hPort == INVALID_HANDLE_VALUE)
      {
        DWORD dwError = GetLastError();
      }
      else
      {
        AfxMessageBox(_T("1 open"));
        CloseHandle(hPort);
      }
    }

I also checked these formats:

sPort1.Format(_T("URT%d"), i);

sPort3.Format(_T("\.\COM%d"), i);

sPort4.Format(_T("\\.\COM%d"), i);

and sPort5.Format(_T("\COM%d"), i);

but I couldnt find any.

+3  A: 

The obvious thing to check is whether you have the name correct. For a COM port, it'll normally be something like \\.\com1, but in C or C++ you need to escape all the back-slashes, so it'll look like "\\\\.\\com1" if you're using a string literal.

Jerry Coffin
how do I know the correct name?
user1111111
@sam: one way is to enumerate the available ports with `EnumPorts`, put those in a list, and have the user select from the list so there's no way to enter an invalid name. Even though the docs for `EnumPorts` only talk about printer ports, serial ports are included.
Jerry Coffin
the enumeration didnt give me any results as showing the edit.
user1111111
@Jerry: Is there a version of `EnumPorts` for Windows Mobile? I could not find the docs using Google.
Shaihi
@Shaihi: Offhand, I'm not sure, but I doubt it. It's *intended* for printer ports, and mobile devices rarely have any physical ports for connecting printers.
Jerry Coffin
thanks Jerry, ur ideas helped me as well
user1111111
+2  A: 

COM ports names in Windows CE/Mobile are in the format of COMX: - the difference is the colon - (for example COM1:).

Your code should look like this: CreateFile(L"COM1:",...)

You can also check the port name through the registry. If you have an ActiveSync connection, use a remote registry editor and go to [HKLM\Drivers\Active] - one of the subkeys will hold the information of the port you want (assuming it is loading properly).

Shaihi
That's a colon, not a semicolon...
Will Dean
Fixed it - thanks.
Shaihi