我就是换成COM2,COM3都返回-1不知为什么,请指教!!! s 为一个SECURITY_ATTRIBUTES
结构

解决方案 »

  1.   

    哪个结构体有问题,那里是要传递一个指针的,你可以设空,下面是C的代码,你参考一下
    char *pcCommPort = "COM2";HANDLE hCom = CreateFile( pcCommPort,
                        GENERIC_READ | GENERIC_WRITE,
                        0,    // must be opened with exclusive-access
                        NULL, // no security attributes
                        OPEN_EXISTING, // must use OPEN_EXISTING
                        0,    // not overlapped I/O
                        NULL  // hTemplate must be NULL for comm devices
                        );
      

  2.   

    HANDLE CreateFile(
      LPCTSTR lpFileName,          // pointer to name of the file
      DWORD dwDesiredAccess,       // access (read-write) mode
      DWORD dwShareMode,           // share mode
      LPSECURITY_ATTRIBUTES lpSecurityAttributes,
                                   // pointer to security attributes
      DWORD dwCreationDisposition,  // how to create
      DWORD dwFlagsAndAttributes,  // file attributes
      HANDLE hTemplateFile         // handle to file with attributes to 
                                   // copy
    );
      

  3.   

    handle = CreateFile("COM1", GENERIC_READ Or GENERIC_WRITE, 0, byval 0, OPEN_EXISTING, 0, 0)
    这样试试,应该就不会错了