打开串口失败(分情况)。
//wsprintf( szPort, "COM%d", nPort ); // OLD
wsprintf( szPort, "\\\\.\\COM%d", nPort ); // 2009.2.24
// m_hIDComDev = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );
m_hIDComDev = CreateFile( szPort,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,0, NULL); if( m_hIDComDev == INVALID_HANDLE_VALUE)
{
DWORD dwErr = GetLastError();
return( FALSE );
}
在用普通的串口时,打开时正常的。
但在使用 USB转串口 时,打开时总是失败,返回错误码
5, Access is denied. ERROR_ACCESS_DENIED 大伙知道是什么问题么.
着急啊
//wsprintf( szPort, "COM%d", nPort ); // OLD
wsprintf( szPort, "\\\\.\\COM%d", nPort ); // 2009.2.24
// m_hIDComDev = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );
m_hIDComDev = CreateFile( szPort,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,0, NULL); if( m_hIDComDev == INVALID_HANDLE_VALUE)
{
DWORD dwErr = GetLastError();
return( FALSE );
}
在用普通的串口时,打开时正常的。
但在使用 USB转串口 时,打开时总是失败,返回错误码
5, Access is denied. ERROR_ACCESS_DENIED 大伙知道是什么问题么.
着急啊
但在使用 USB转串口 时,打开时总是失败,返回错误码
5, Access is denied. ERROR_ACCESS_DENIED USB转串口要打驱动才行吧。
wsprintf( szPort, "COM%d", nPort );
else
wsprintf( szPort, "\\\\.\\COM%d", nPort );
CreateFile
Communications Resources
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.
wsprintf( szPort, "COM%d", nPort );
else
wsprintf( szPort, "\\\\.\\COM%d", nPort );