打开串口失败(分情况)。
//wsprintf( szPort, "COM%d", nPort ); // OLD
wsprintf( szPort, "\\\\.\\COM%d", nPort ); // 2009.2.24
// m_hIDComDev = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );
m_hIDComDev = CreateFile( szPort,
                      GENERIC_READ | GENERIC_WRITE, 
  0, 
  NULL, 
  OPEN_EXISTING,0, NULL); if( m_hIDComDev == INVALID_HANDLE_VALUE) 
{
DWORD dwErr = GetLastError();
return( FALSE );
}
在用普通的串口时,打开时正常的。
但在使用 USB转串口 时,打开时总是失败,返回错误码
5, Access is denied.  ERROR_ACCESS_DENIED  大伙知道是什么问题么.
着急啊

解决方案 »

  1.   

    别是串口被占用了吧,很多机器自带一个com1,然后因为一些原因(比如装个猫)有一个com2, 或com3,再新的串口可能是com4, com5........
      

  2.   

    在用普通的串口时,打开时正常的。
    但在使用 USB转串口 时,打开时总是失败,返回错误码
    5, Access is denied. ERROR_ACCESS_DENIED   USB转串口要打驱动才行吧。
      

  3.   

    用串口调试助手测试一下你那个usb转串口是否可用啊
      

  4.   

    if(nPort < 10)
     wsprintf( szPort, "COM%d", nPort ); 
    else
     wsprintf( szPort, "\\\\.\\COM%d", nPort ); 
    CreateFile
    Communications Resources
    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.
      

  5.   

    if(nPort < 10)
     wsprintf( szPort, "COM%d", nPort );  
    else
     wsprintf( szPort, "\\\\.\\COM%d", nPort );