串口号大于10的情况 - QT在windows下的串口编程

转载自:http://blog.chinaunix.net/uid-20044771-id-3189309.html

 

使用QT在windows下编写一个串口工具,经常碰到串口号大于10的情况,如"COM12"、"COM20"这样的,打开串口(createfile)时会出错;
在网上找到解决方法,即追加一段前缀("
\\\\.\\")

LPCWSTR lpPort = new wchar_t[com.size()+1]; //com为QString类型的串口
wstring wsPort(L"");
memset( (void*)lpPort,0, sizeof(wchar_t)*(com.size()+1));
com.toWCharArray((wchar_t*)lpPort);
wsPort.append(lpPort);
if(wsPort.size()>4)
{
wsPort.insert(0,L"\\\\.\\");
}
delete lpPort;

handle = CreateFile( wsPort.c_str(),
                        GENERIC_READ | GENERIC_WRITE,
                        0, NULL,
                        OPEN_EXISTING,
                        0,
                        NULL);


 

你可能感兴趣的:(串口号大于10的情况 - QT在windows下的串口编程)