HANDLE CreateFile(
LPCTSTR lpFileName, // pointer to name of the file
DWORD dwDesiredAccess, // access (read-write) mode
DWORD dwShareMode, // share mode
LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to security attributes
DWORD dwCreationDistribution, // how to create
DWORD dwFlagsAndAttributes, // file attributes
HANDLE hTemplateFile // handle to file with attributes to copy
);
lpFileName: 指明串口制备,例:COM1,COM2
dwDesiredAccess: 指明串口存取方式,例:GENERIC_READ|GENERIC_WRITE
dwShareMode: 指明串口共享方式
lpSecurityAttributes: 指明串口的安全属性结构,NULL为缺省安全属性
dwCreateionDistribution: 必须为OPEN_EXISTIN
dwFlagAndAttributes: 对串口唯一有意义的是FILE_FLAG_OVERLAPPED
hTemplateFile: 必须为NULL
LPCTSTR lpFileName, // pointer to name of the file
DWORD dwDesiredAccess, // access (read-write) mode
DWORD dwShareMode, // share mode
LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to security attributes
DWORD dwCreationDistribution, // how to create
DWORD dwFlagsAndAttributes, // file attributes
HANDLE hTemplateFile // handle to file with attributes to copy
);
lpFileName: 指明串口制备,例:COM1,COM2
dwDesiredAccess: 指明串口存取方式,例:GENERIC_READ|GENERIC_WRITE
dwShareMode: 指明串口共享方式
lpSecurityAttributes: 指明串口的安全属性结构,NULL为缺省安全属性
dwCreateionDistribution: 必须为OPEN_EXISTIN
dwFlagAndAttributes: 对串口唯一有意义的是FILE_FLAG_OVERLAPPED
hTemplateFile: 必须为NULL
解决方案 »
- 可谓自绘标题栏的终极难题
- 求救 象beyond compare 树形比较视图 是如何实现的
- 急救!急救!急救!怎样判断CString类型的数据是不是数字?已经没分了!
- 求教宏定义
- 惨!VC不能调试了
- DllRegisterServer in MyActiveX.dll failed,Return Code was :0x8007007e????
- 这个问题怎么都没有人回答?
- 兄弟们,做一份调查,请大家谈谈关于自已想开发什么样的软件,以及市场上会需要什么软件(内详)
- vc中如何获得当前窗口句柄
- vc++ 技术内幕 第四版 现在值不值的买???是不是已经有点过时?
- 怎样获得本机的子网掩码
- 当列表框中内容很多时,如何排序?(内容为文件信息)
If the function succeeds, the return value is an open handle to the specified file. If the specified file exists before the function call and dwCreationDisposition is CREATE_ALWAYS or OPEN_ALWAYS, a call to GetLastError returns ERROR_ALREADY_EXISTS (even though the function has succeeded). If the file does not exist before the call, GetLastError returns zero. If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.
BuildCommDCB
BuildCommDCBAndTimeouts
ClearCommBreak
ClearCommError
CommConfigDialog
EscapeCommFunction
GetCommConfig
GetCommMask
GetCommModemStatus
GetCommProperties
GetCommState
GetCommTimeouts
GetDefaultCommConfig
PurgeComm
SetCommBreak
SetCommConfig
SetCommMask
SetCommState
SetCommTimeouts
SetDefaultCommConfig
SetupComm
TransmitCommChar
WaitCommEvent
WriteFile
ReadFile等
OpenCom(LPCTSTR ComName)
{
DCB tmpdcb;
COMMTIMEOUTS timeout; hcom=CreateFile(ComName, //打开串口
GENERIC_READ|GENERIC_WRITE, //以读\写方式打开
0, //共享属性,必须设为0
NULL, //设为NULL使用缺省的安全属性
OPEN_EXISTING, //打开已经存在的串口
FILE_FLAG_OVERLAPPED, //对串口只能使用此设置
NULL); //对串口只能使用此设置
if(hcom==INVALID_HANDLE_VALUE)
{
ShowMessage("Open com failed!");
return false;
}
ComOpened=true; //设置TIMEOUT
GetCommTimeouts(hcom,&timeout);
timeout.ReadIntervalTimeout=50;
timeout.ReadTotalTimeoutConstant=500;
timeout.ReadTotalTimeoutMultiplier=50;
SetCommTimeouts(hcom,&timeout); GetCommState(hcom,&tmpdcb);
BuildCommDCB("24,N,8,1",&tmpdcb);
tmpdcb.fDtrControl=DTR_CONTROL_DISABLE;
SetCommState(hcom,&tmpdcb); SetupComm(hcom,1024,1024);
return true;
}
//-----------------------------------------------------------------
SendChar(unsigned char x[],DWORD n)
{
DWORD nAct;
WriteFile(hcom,x,n,&nAct,NULL);
return true;
}
//-----------------------------------------------------------------
ReceiveChar(unsigned char x[],DWORD n)
{
DWORD nRead;
ReadFile(hcom,x,n,&nRead,NULL);
return nRead;
}