CreateFile 串口打不开,如下代码.....
hCom=CreateFile("COM15", // 文件名
GENERIC_READ | GENERIC_WRITE, // 允许读和写
0, // 独占方式
NULL,
OPEN_EXISTING, //打开而不是创建
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, // 重叠方式
NULL
);
if(hCom==INVALID_HANDLE_VALUE)
{
AfxMessageBox("打开COM失败!");
int err=GetLastError();
CString st;
st.Format( "Error in open COM port: %d\n", err );
MessageBox(st);
return;
}
hCom=CreateFile("COM15", // 文件名
GENERIC_READ | GENERIC_WRITE, // 允许读和写
0, // 独占方式
NULL,
OPEN_EXISTING, //打开而不是创建
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, // 重叠方式
NULL
);
if(hCom==INVALID_HANDLE_VALUE)
{
AfxMessageBox("打开COM失败!");
int err=GetLastError();
CString st;
st.Format( "Error in open COM port: %d\n", err );
MessageBox(st);
return;
}
用UNC,仔细看MSDN中CreateFile的说明
偶使用的是VC++ 6.0 要使用哪个版本的MSDN?
//关于 “Win32 Device Namespaces”
\\.\PHYSICALDRIVE2 Obtains a handle to the third physical drive on the user's computer.
Tape Drives
Windows NT/2000/XP: You can open tape drives using a file name of the form \\.\TAPEx where x is a number indicating which drive to open, starting with tape drive 0. To open tape drive 0 in C, use the file name "\\\\.\\TAPE0". For more information on manipulating tape drives for backup or other applications, see Tape Backup.