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
例子:
m_hComm := CreateFile( //创建文件(串口),取得操作句柄
PChar(sCommPort), //文件名称
//'\\.\COM10',
GENERIC_READ or GENERIC_WRITE, //操作方式(读或写)
dwShareMode, //共享方式
nil, //SD
OPEN_EXISTING, //文件创建方法
FILE_ATTRIBUTE_NORMAL, //文件属性
0); //handle to template file
if( m_hComm = INVALID_HANDLE_VALUE ) then //串口打开失败
begin
;
end;
本文介绍如何使用Windows API函数CreateFile来打开并配置串口。文章详细解释了CreateFile函数各参数的意义及用法,并提供了一个具体的示例代码,帮助读者理解如何通过该函数实现对串口的读写操作。
515

被折叠的 条评论
为什么被折叠?



