在一个线程中我用
hPort[PortNo] = CreateFile(lpszPortName,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
打开串口,并且有个这个判断:
if (hPort[PortNo] == INVALID_HANDLE_VALUE) 
{
// Could not open the port.
neterrormsg1("不能打开端口 COM", PortNo + 1);//majun update
return FALSE;
}
(以上是放在函数SerialPortInitialize中)
void neterrormsg(LPSTR cp)
{
    char buf[128];
    wsprintf(buf,"%s(%d)",(LPSTR)cp,WSAGetLastError());
    AfxMessageBox(buf);
}
当我在某个地方改变了一个“波特率”的设置,然后重新调用函数SerialPortInitialize这时提示"不能打开端口 COM 1`"请问这是什么原因造成的,我怎么可以重新成功打开串口????

解决方案 »

  1.   

    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 dwCreationDistribution, // how to create
      DWORD dwFlagsAndAttributes, // file attributes
      HANDLE hTemplateFile // handle to file with attributes to copy
      );
      lpFileName: 指明串口制备,例:COM1,COM2
      dwDesiredAccess: 指明串口存取方式,例:GENERIC_READ|GENERIC_WRITE
      dwShareMode: 指明串口共享方式
      lpSecurityAttributes: 指明串口的安全属性结构,NULL为缺省安全属性
      dwCreateionDistribution: 必须为OPEN_EXISTIN
      dwFlagAndAttributes: 对串口唯一有意义的是FILE_FLAG_OVERLAPPED
      hTemplateFile: 必须为NULL