打开串口(COM)号大于9时报错

使用CreateFile打开串口(COM)号大于9时报错,GetLastError()的值为2。

解决方法:

  // 把串口名称由 "COMN" 改为 "\\.\COMN",N没有大小限制

  const int len = 20;

  char newCommName[len + 1] = { 0 };

  sprintf_s(newCommName, len, "\\\\.\\%s", chCommName);

  m_hComm = CreateFileA( newCommName, // 串口号;

              GENERIC_READ | GENERIC_WRITE,

              0,

              NULL,

              OPEN_EXISTING,

              0,

              NULL );

 

posted @ 2018-05-17 23:16  晴天224  阅读(690)  评论(0编辑  收藏  举报