第一篇初步瞭解串口的大致運作,接下來我們看基本操作
先看串口操作的數據結構:
串口操作有幾個比較重要的Struct
1.Overlapped I/O 異步I/O模型
異步I/O和同步I/O不同,同步I/O時,程序被掛起,
Overlapped數據結構:
typedef struct _OVERLAPPED {
DWORD Internal;
DWORD InternalHigh;
DWORD Offset;
DWORD OffsetHigh;
HANDLE hEvent;
} OVERLAPPED, *LPOVERLAPPED;
DWORD Internal; 通常被保留,當GetOverlappedResult()
DWORD InternalHigh; 通常被保留,當GetOverlappedResult()
DWORD Offset; 指定文件的位置,從該位置傳送數據,
DWORD OffsetHigh; 指定開始傳送數據的字節偏移量的高位字,讀寫命名管道及通
信設備時調用進程忽略這個成員;
HANDLE hEvent; 標識事件,數據傳送完成時把它設為信號狀態,
WriteFile ConnectNamedPipe TransactNamedPipe函數前,
CreateEvent ResetEvent GetOverlappedResult
WaitForSingleObject CWinThread GetLastError
OVERLAPPED和數據緩衝區釋放問題:
在請求時,不能釋放,只有在I/O請求完成之後,才可以釋放。
內核對像(hand)實現:
例子:用overlapped模型讀一個磁盤文件內容。
1.把設備句柄看作同步對象,
2.完成I/O,設置信息狀態。為有信號。
3.
或者異步設備調用GetOverLappedResult函數。
view plaincopy to clipboardprint?
int main()
{
BOOL rc;
HANDLE hFile;
DWORD numread;
OVERLAPPED overlap;
char buf[READ_SIZE];
char szPath[MAX_PATH];
CheckOsVersion();
GetWindowsDirectory(szPath, sizeof(szPath));
strcat(szPath, "\\WINHLP32.EXE");
hFile = CreateFile( szPath,
GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL
);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("Could not open %s\n", szPath);
return -1;
}
memset(&overlap, 0, sizeof(overlap));
overlap.Offset = 1500;
rc = ReadFile(
hFile,
buf,
READ_SIZE,
&numread,
&overlap
);
printf("Issued read request\n");
if (rc)
{
printf("Request was returned immediately\n");
}
else
{
if (GetLastError() == ERROR_IO_PENDING)
{
printf("Request queued, waiting...\n");
WaitForSingleObject(hFile, INFINITE);
printf("Request completed.\n");
rc = GetOverlappedResult(
"串口通訊編程一日通2(Overlapped IO模型)"
- 串口通讯编程一日通2(Overlapped IO模型) - September - CSDN博客 (在「Google 網頁註解」中檢視)
沒有留言:
張貼留言