谁有用delphi实现的用完成端口方式读取串口的源程序,我这调试一天不行(100)

  • 主题发起人 主题发起人 lishubing_2000
  • 开始时间 开始时间
L

lishubing_2000

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit_IOCP_Com;interfaceuses windows,Dialogs,Logs;type PPerHandleIOData = ^TPerHandleIOData; TPerHandleIOData = record Overlapped: TOverlapped; CommState : ComStat; hcom:Thandle; end; function init_iocp_com():boolean; implementationfunction GetWindowsErrorMessage(ErrCode: Integer): string;var Buf: array[0..1023] of char; Len: Integer;begin Len := FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nil, ErrCode, 0, Buf, 1024, nil); SetString(Result, Buf, Len);end;{工作线程}function ServerWorkerThreadProc(Param: Pointer): Cardinal;var CPort: THandle; Key, Bytes: Cardinal; Overlapped: POverlapped; PIOData: PPerHandleIOData; FIOData: PPerHandleIOData; cbNum,Cbread:Dword;begin CPort := THandle(Param); while True do if GetQueuedCompletionStatus(CPort, Bytes, Key,Overlapped, INFINITE) then begin if (Bytes=0) then begin Log.Write('读取数据失败!退出'); break; end else begin PIOData := PPerHandleIOData(Overlapped); Log.Write('读到数据:'); ///发送读取数据请求 FIOData := PPerHandleIOData(GlobalAlloc(GPTR,SizeOf(PPerHandleIOData))); FIOData.hcom:=PIOData.hcom; FIOData.Overlapped.Internal:=0; FIOData.Overlapped.InternalHigh:=0; FIOData.Overlapped.Offset:=0; FIOData.Overlapped.OffsetHigh:=0; FIOData.Overlapped.hEvent:=Createevent(nil,true,False,nil); cbNum := 8; Cbread := 0; if not ReadFile(FIOData.hcom,FIOData.buffer,cbNum,Cbread,@FIOData) then begin Log.Write('再次发送读取请求错误'); end; end; end else begin // 查询完成状态失败 Log.Write('查询完成状态失败!继续查询'); Continue; end; ShowMessage('Worker Thread Terminated'); Result := 0;end;{启动完成端口模式}function init_iocp_com():boolean;var systeminfo:SYSTEM_INFO; threadId:DWORD; threadHandle:THandle; CompletionPort: THandle;//完成端口 hcom1,hcom2:Thandle; Lpdcb:TDCB; perhanddata1: PPerHandleIOData; perhanddata2: PPerHandleIOData; i:integer; FIOData: PPerHandleIOData; cbNum,Cbread:Dword;begin CompletionPort := CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, 0, 0); if CompletionPort <= 0 then begin Log.Write('创建完成端口模式错误'); Result := false; exit; end; GetSystemInfo(systeminfo); for i:= 0 to systeminfo.dwNumberOfProcessors*2-1 do begin threadHandle := BeginThread(nil, 0, @ServerWorkerThreadProc, Pointer(CompletionPort), 0, threadId); if threadHandle=0 then begin Log.Write('创建工作线程错误'); break; end; CloseHandle(threadHandle); end; hcom1:=createFile(pchar('com2'), //串口名,可为com1-com4 generic_read or Generic_write,//访问模式 0, //共享模式,必须为0 nil, //安全属性指针 open_existing, ///找开方式必须为open_existing FILE_FLAG_OVERLAPPED or FILE_ATTRIBUTE_NORMAL,//文件属性,本文设为交迭标志 0); //临时文件句柄,必须为0 if hcom1<>invalid_Handle_Value then begin SetupComm(hcom1,4096,1024); //设置缓冲区长度 getCommState(hcom1,lpdcb); //设置串口 lpdcb.baudrate:=9600; lpdcb.stopbits:=0; lpdcb.bytesize:=8; lpdcb.parity:=0; setCommState(hcom1,lpdcb); //SetCommMask(Hcom1,ev_Rxchar); //设置串口事件屏蔽 if(CreateIoCompletionPort(hcom1,CompletionPort,DWORD(110),0))=0 then begin Log.Write('加入完成端口队列错误'); end else begin Log.Write('加入完成端口队列com1'); end; end; ///发送读取数据请求 FIOData := PPerHandleIOData(GlobalAlloc(GPTR,SizeOf(PPerHandleIOData))); FIOData.hcom:=hcom1; FIOData.Key:=1001; FIOData.Overlapped.Internal:=0; FIOData.Overlapped.InternalHigh:=0; FIOData.Overlapped.Offset:=0; FIOData.Overlapped.OffsetHigh:=0; cbNum := 8; Cbread := 0; if not ReadFile(FIOData.hcom,FIOData.buffer,cbNum,Cbread,POverlapped(FIOData)) then begin Log.Write('发送读取请求错误:'+GetWindowsErrorMessage(GetLastError)); Result := false; exit; end; Result:=true;end;end.没有源程序的话帮忙调试一下上面的程序,高分送上
 
问题已解决,送分了
 
解决就好,不要分,给后面来的吧
 
你接收数据部分在那里
 
解决了就贴上来吧,后面的人好查.
 
这是完成端口模式,高性能的IO模式,大家可以查资料学学下面这一行错了改成这个就行了PPerHandleIOData(GlobalAlloc(GPTR,SizeOf(TPerHandleIOData )));
 

Similar threads

后退
顶部