使用CreateFile返回串口句柄m_hComm = CreateFile(szPort, // communication port string (COMX)
GENERIC_READ | GENERIC_WRITE, // read/write types
0, // comm devices must be opened with exclusive access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED, // Async I/O
0); // template must be 0 for comm devices但是当szPort在COM1~COM9时,都没问题,当szPort>=COM10,就返回INVALID_HANDLE_VALUE 出错,这是怎么回事啊??
GENERIC_READ | GENERIC_WRITE, // read/write types
0, // comm devices must be opened with exclusive access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED, // Async I/O
0); // template must be 0 for comm devices但是当szPort在COM1~COM9时,都没问题,当szPort>=COM10,就返回INVALID_HANDLE_VALUE 出错,这是怎么回事啊??
应该写为
\\\\.\\com10下边有个列子bool SetupPort(unsigned char byPort)
{
String sCom;
DCB dcb;
COMMTIMEOUTS CommTimeOuts;
HWND m_hComHandle; sCom = "\\\\.\\COM " + IntToStr(byPort); m_hComHandle = ::CreateFile(sCom.c_str(),
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
NULL);
if(m_hComHandle == INVALID_HANDLE_VALUE)
{
CloseHandle(m_hComHandle);
return false;
} SetCommMask(m_hComHandle, EV_RXCHAR); if(SetupComm(m_hComHandle, 256, 256) == false)
{
CloseHandle(m_hComHandle);
return false;
} PurgeComm(m_hComHandle,
PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR); CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.ReadTotalTimeoutConstant = 0;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(m_hComHandle, &CommTimeOuts); dcb.DCBlength = sizeof(DCB);
if(GetCommState(m_hComHandle, &dcb) == false)
{
//do something
}
else
{
dcb.BaudRate = CBR_38400;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fBinary = true;
dcb.fParity = true;
dcb.fOutxCtsFlow = false;
dcb.fOutxDsrFlow = false;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fOutX = false;
dcb.fInX = false;
dcb.fAbortOnError = false; if(SetCommState(m_hComHandle, &dcb) == false)
{
CloseHandle(m_hComHandle);
return false;
}
} //END OF else return true;
} //END OF SetupPort()
这是什么意思呢?为什么到COM10之后就不能叫COM10了??
产生这种奇怪现象的原因是:微软预定义的标准设备中含有“COM1”-“COM9”。所以,“COM1”-“COM9”作为文件名传递给函数时操作系统会自动地将之解析为相应的设备。但对于COM10及以上的串口,“COM10”之类的文件名系统只视之为一般意义上的文件,而非串行设备。为了增加对COM10及以上串行端口的支持,微软规定,如果要访问这样的设备,应使用这样的文件名(以COM10为例):\\.COM10
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.For more information about communications, see Communications.
不要问为什么。MSDN上就是这么说的