关于Windows API的一个串口通信测试程序的问题(50分)

  • 主题发起人 主题发起人 ccdd_liu
  • 开始时间 开始时间
C

ccdd_liu

Unregistered / Unconfirmed
GUEST, unregistred user!
我用《Delphi串口通信技术与工程实践》(人民邮电出版社 赵兰涛 苏彦华 编著)附带光盘中的程序——Windows API串口编程实例进行串口调试,字符能够被发送出去,但却接收不到东西(用其他程序进行调试都可以接受到的)。请教高手!如须原码,请告诉我邮箱。谢谢!
 
代码贴出来,大家帮你分析
 
有点长哦~~
 
unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls, ComCtrls;<br><br>const<br> &nbsp;WM_COMMNOTIFY = WM_USER + 100; // 通讯消息<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;StatusBar1: TStatusBar;<br> &nbsp; &nbsp;Memo1: TMemo;<br> &nbsp; &nbsp;Memo2: TMemo;<br> &nbsp; &nbsp;Label1: TLabel;<br> &nbsp; &nbsp;Label2: TLabel;<br> &nbsp; &nbsp;GroupBox1: TGroupBox;<br> &nbsp; &nbsp;Label3: TLabel;<br> &nbsp; &nbsp;Label4: TLabel;<br> &nbsp; &nbsp;Label5: TLabel;<br> &nbsp; &nbsp;Label6: TLabel;<br> &nbsp; &nbsp;ComboBox4: TComboBox;<br> &nbsp; &nbsp;ComboBox3: TComboBox;<br> &nbsp; &nbsp;ComboBox2: TComboBox;<br> &nbsp; &nbsp;ComboBox1: TComboBox;<br> &nbsp; &nbsp;Label7: TLabel;<br> &nbsp; &nbsp;ComboBox5: TComboBox;<br> &nbsp; &nbsp;btnOpenCom: TButton;<br> &nbsp; &nbsp;btnSendData: TButton;<br> &nbsp; &nbsp;btnReceiveData: TButton;<br> &nbsp; &nbsp;btnCloseCom: TButton;<br> &nbsp; &nbsp;procedure btnOpenComClick(Sender: TObject);<br> &nbsp; &nbsp;procedure FormCreate(Sender: TObject);<br> &nbsp; &nbsp;procedure btnCloseComClick(Sender: TObject);<br> &nbsp; &nbsp;procedure btnSendDataClick(Sender: TObject);<br> &nbsp; &nbsp;procedure btnReceiveDataClick(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp; &nbsp;procedure WMCOMMNOTIFY(var Message :TMessage);message WM_COMMNOTIFY;<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>var<br> &nbsp;CommHandle:THandle;<br> &nbsp;PostEvent:THandle;<br> &nbsp;ReadOs : Toverlapped;<br> &nbsp;Connected:Boolean;<br> &nbsp;Receive :Boolean;<br> &nbsp;ReceiveData : Dword;<br><br>procedure AddToMemo(Str:PChar;Len:Dword); // 接收的数据送入显示区Memo2<br>begin<br> &nbsp;//接收厚的字符串为NULL终止<br> &nbsp;str[Len]:=#0;<br> &nbsp;Form1.Memo2.Text:=Form1.Memo2.Text+StrPas(str);<br>end;<br><br><br>procedure CommWatch(Ptr:Pointer);stdcall; // 通讯监视线程<br>var<br> &nbsp;dwEvtMask,dwTranser : Dword;<br> &nbsp;PostMsgFlag: Boolean;<br> &nbsp;overlapped : Toverlapped;<br><br>begin<br> &nbsp;Receive :=True;<br> &nbsp;FillChar(overlapped,SizeOf(overlapped),0);<br> &nbsp;overlapped.hEvent :=CreateEvent(nil,True,False,nil); // 创建重叠读事件对象<br> &nbsp;if overlapped.hEvent=null then<br> &nbsp;begin<br> &nbsp; &nbsp;MessageBox(0,'overlapped.Event Create Error !','Notice',MB_OK);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br><br> &nbsp;//进入串口监视状态,直到全局变量Receive置为False停止<br> &nbsp;while(Receive) do<br> &nbsp;begin<br> &nbsp; &nbsp;dwEvtMask:=0;<br> &nbsp; &nbsp;// 等待串口事件发生<br> &nbsp; &nbsp;if not WaitCommEvent(CommHandle,dwEvtMask,@overlapped) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;if ERROR_IO_PENDING=GetLastError then<br> &nbsp; &nbsp; &nbsp; &nbsp;GetOverLappedResult(CommHandle,overlapped,dwTranser,True)<br> &nbsp; &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp; &nbsp;//串口读事件发布消息 <br> &nbsp; &nbsp; &nbsp;if ((dwEvtMask and EV_RXCHAR)=EV_RXCHAR) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;// 等待允许传递WM_COMMNOTIFY通讯消息<br> &nbsp; &nbsp; &nbsp; &nbsp;WaitForSingleObject(Postevent,INFINITE);<br> &nbsp; &nbsp; &nbsp; &nbsp;// 处理WM_COMMNOTIFY消息时不再发送WM_COMMNOTIFY消息<br> &nbsp; &nbsp; &nbsp; &nbsp;ResetEvent(PostEvent);<br> &nbsp; &nbsp; &nbsp; &nbsp;// 传递WM_COMMNOTIFY通讯消息,告知主线程调用读串口的过程<br> &nbsp; &nbsp; &nbsp; &nbsp;PostMsgFlag:=PostMessage(Form1.Handle,WM_COMMNOTIFY,CommHandle,0);<br> &nbsp; &nbsp; &nbsp; &nbsp;if (not PostMsgFlag) then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MessageBox(0,'PostMessage Error !','Notice',MB_OK);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;CloseHandle(overlapped.hEvent); // 关闭重叠读事件对象<br>end;<br><br><br>procedure TForm1.WMCOMMNOTIFY(var Message :TMessage); // 消息处理函数<br>var<br> &nbsp;CommState : ComStat;<br> &nbsp;dwNumberOfBytesRead : Dword;<br> &nbsp;ErrorFlag : Dword;<br> &nbsp;InputBuffer : Array [0..1024] of Char;<br><br>begin<br> &nbsp;if not ClearCommError(CommHandle,ErrorFlag,@CommState) then<br> &nbsp;begin<br> &nbsp; &nbsp;MessageBox(0,'ClearCommError !','Notice',MB_OK);<br> &nbsp; &nbsp;PurgeComm(CommHandle,Purge_Rxabort or Purge_Rxclear);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br><br> &nbsp;if CommState.cbInQue&gt;0 then<br> &nbsp;begin<br> &nbsp; &nbsp;fillchar(InputBuffer,CommState.cbInQue,#0);<br> &nbsp; &nbsp;// 接收通讯数据<br> &nbsp; &nbsp;if (not ReadFile( CommHandle,InputBuffer,CommState.cbInQue,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dwNumberOfBytesRead,@ReadOs )) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;ErrorFlag := GetLastError();<br> &nbsp; &nbsp; &nbsp;if (ErrorFlag &lt;&gt; 0) and (ErrorFlag &lt;&gt; ERROR_IO_PENDING) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;MessageBox(0,'ReadFile Error!','Notice',MB_OK);<br> &nbsp; &nbsp; &nbsp; &nbsp;Receive :=False;<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(ReadOs.hEvent);<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(PostEvent);<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(CommHandle);<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp;else begin<br> &nbsp; &nbsp; &nbsp; &nbsp;WaitForSingleObject(CommHandle,INFINITE); // 等待操作完成<br> &nbsp; &nbsp; &nbsp; &nbsp;GetOverlappedResult(CommHandle,ReadOs,dwNumberOfBytesRead,False);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;if dwNumberOfBytesRead&gt;0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;ReadOs.Offset :=ReadOs.Offset+dwNumberOfBytesRead;<br> &nbsp; &nbsp; &nbsp;ReceiveData := ReadOs.Offset;<br> &nbsp; &nbsp; &nbsp;// 处理接收的数据<br> &nbsp; &nbsp; &nbsp;AddToMemo(InputBuffer,dwNumberOfBytesRead);<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br> &nbsp;// 允许发送下一个WM_COMMNOTIFY消息<br> &nbsp;SetEvent(PostEvent);<br>end;<br><br><br>procedure TForm1.btnOpenComClick(Sender: TObject);<br>var<br> &nbsp; &nbsp;CommTimeOut : TCOMMTIMEOUTS;<br> &nbsp; &nbsp;DCB : TDCB;<br><br>begin<br> &nbsp; &nbsp;StatusBar1.SimpleText := '连接中...';<br><br> &nbsp; &nbsp;//发送消息的句柄<br> &nbsp; &nbsp;PostEvent:=CreateEvent(nil,True,True,nil);<br> &nbsp; &nbsp;if PostEvent=null then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;MessageBox(0,'CreateEvent Error!','Notice',MB_OK);<br> &nbsp; &nbsp; &nbsp; &nbsp;StatusBar1.SimpleText := '串口打开失败';<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;//Overlapped Read建立句柄<br> &nbsp; &nbsp;ReadOs.hEvent :=CreateEvent(nil,true,False,nil);<br> &nbsp; &nbsp;if ReadOs.hEvent=null then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;MessageBox(0,'CreateEvent Error!','Notice',MB_OK);<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(PostEvent);<br> &nbsp; &nbsp; &nbsp; &nbsp;StatusBar1.SimpleText := '串口打开失败';<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;//建立串口句柄<br> &nbsp; &nbsp;CommHandle := CreateFile(PChar(ComboBox1.Text),GENERIC_WRITE or GENERIC_READ,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,nil,OPEN_EXISTING,FILE_FLAG_OVERLAPPED or FILE_ATTRIBUTE_NORMAL,0);<br><br> &nbsp; &nbsp;if CommHandle = INVALID_HANDLE_VALUE then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(PostEvent);<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(ReadOs.hEvent);<br> &nbsp; &nbsp; &nbsp; &nbsp;MessageBox(0,'串口打开失败!','Notice',MB_OK);<br> &nbsp; &nbsp; &nbsp; &nbsp;StatusBar1.SimpleText := '串口打开失败';<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;StatusBar1.SimpleText := '已同端口 '+ ComboBox1.Text + ' 连接!';<br><br> &nbsp; &nbsp;//设置超时<br> &nbsp; &nbsp;CommTimeOut.ReadIntervalTimeout := MAXDWORD;<br> &nbsp; &nbsp;CommTimeOut.ReadTotalTimeoutMultiplier := 0;<br> &nbsp; &nbsp;CommTimeOut.ReadTotalTimeoutConstant := 0;<br> &nbsp; &nbsp;SetCommTimeouts(CommHandle, CommTimeOut);<br><br> &nbsp; &nbsp;//设置读写缓存<br> &nbsp; &nbsp;SetupComm(CommHandle,4096,1024);<br><br> &nbsp; &nbsp;//对串口进行指定配置<br> &nbsp; &nbsp;GetCommState(CommHandle,DCB);<br> &nbsp; &nbsp;DCB.BaudRate := StrToInt(ComboBox2.Text);<br> &nbsp; &nbsp;DCB.ByteSize := StrToInt(ComboBox3.Text);<br> &nbsp; &nbsp;DCB.Parity := ComboBox4.ItemIndex;;<br> &nbsp; &nbsp;DCB.StopBits := ComboBox5.ItemIndex;<br> &nbsp; &nbsp;Connected := SetCommState(CommHandle, DCB);<br><br> &nbsp; &nbsp;//关系串口的读事件<br> &nbsp; &nbsp;if (not SetCommMask(CommHandle,EV_RXCHAR)) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;MessageBox(0,'SetCommMask Error !','Notice',MB_OK);<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;if (Connected) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;btnOpenCom.Enabled :=False;<br> &nbsp; &nbsp;end<br> &nbsp; &nbsp;else begin<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(CommHandle);<br> &nbsp; &nbsp; &nbsp; &nbsp;StatusBar1.SimpleText := '设置串口失败';<br> &nbsp; &nbsp;end;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> &nbsp; &nbsp;Connected:=False;<br> &nbsp; &nbsp;ComboBox1.ItemIndex:=0;<br> &nbsp; &nbsp;ComboBox2.ItemIndex:=0;<br> &nbsp; &nbsp;ComboBox3.ItemIndex:=4;<br> &nbsp; &nbsp;ComboBox4.ItemIndex:=0;<br> &nbsp; &nbsp;ComboBox5.ItemIndex:=0; &nbsp; &nbsp; &nbsp;<br>end;<br><br>procedure TForm1.btnCloseComClick(Sender: TObject);<br>begin<br> &nbsp; &nbsp;if not Connected then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;StatusBar1.SimpleText := '未打开串口';<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;Receive :=False;<br> &nbsp; &nbsp;//取消事件监视,此时监视线程中的WaitCommEvent将返回<br> &nbsp; &nbsp;SetCommMask(CommHandle,0);<br> &nbsp; &nbsp;//等待监视线程结束<br> &nbsp; &nbsp;WaitForSingleObject(PostEvent,INFINITE);<br> &nbsp; &nbsp; //关闭事件句柄<br> &nbsp; &nbsp;CloseHandle(PostEvent);<br> &nbsp; &nbsp;//停止发送和接收数据,并清除发送和接收缓冲区<br> &nbsp; &nbsp;PurgeComm(CommHandle,PURGE_TXABORT or PURGE_RXABORT or PURGE_TXCLEAR or PURGE_RXCLEAR);<br> &nbsp; &nbsp; //关闭其他的句柄<br> &nbsp; &nbsp;CloseHandle(ReadOs.hEvent);<br> &nbsp; &nbsp;CloseHandle(CommHandle); &nbsp; &nbsp;<br> &nbsp; &nbsp;btnOpenCom.Enabled :=True;<br> &nbsp; &nbsp;Connected:=False;<br> &nbsp; &nbsp;StatusBar1.SimpleText := '串口已经关闭';<br>end;<br><br>procedure TForm1.btnSendDataClick(Sender: TObject);<br>var<br> &nbsp; &nbsp;Str:String;<br> &nbsp; &nbsp;i:Integer;<br> &nbsp; &nbsp;writeoverlapped:TOverlapped;<br> &nbsp; &nbsp;ByteToWrite,BytesWritten,AllBytesWritten:DWORD;<br> &nbsp; &nbsp;ErrorCode,ErrorFlag:DWORD;<br> &nbsp; &nbsp;CommStat:COMSTAT;<br><br>begin<br> &nbsp; &nbsp;if not Connected then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;StatusBar1.SimpleText := '未打开串口';<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;if (Memo1.GetTextLen=0) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;StatusBar1.SimpleText := '缓冲区为空';<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;AllBytesWritten:=0;<br> &nbsp; &nbsp;for i:=0 to memo1.Lines.Count-1 do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Str:=memo1.Lines;<br> &nbsp; &nbsp; &nbsp; &nbsp;ByteToWrite:=length(Str);<br> &nbsp; &nbsp; &nbsp; &nbsp;if &nbsp;ByteToWrite=0 then continue;<br> &nbsp; &nbsp; &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StatusBar1.SimpleText := '正在发送数据';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//初始化一步读写结构<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FillChar(writeoverlapped,Sizeof(writeoverlapped),0);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //避免贡献资源冲突<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;writeoverlapped.hEvent:=CreateEvent(nil,True,False,nil);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//发送数据<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if not WriteFile(Commhandle,Str[1],ByteToWrite,BytesWritten,@writeoverlapped) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ErrorCode:=GetLastError;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ErrorCode&lt;&gt;0 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ErrorCode=ERROR_IO_PENDING then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StatusBar1.SimpleText := '端口忙,正在等待...';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while not GetOverlappedResult(Commhandle,writeoverlapped,BytesWritten,True) do<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ErrorCode:=GetLastError;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ErrorCode=ERROR_IO_PENDING then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;continue<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ClearCommError(Commhandle,ErrorFlag,@CommStat);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage('发送数据出错');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(WriteOverlapped.hEvent);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(Commhandle);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;btnOpenCom.Enabled :=True;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AllBytesWritten:=AllBytesWritten+BytesWritten;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ClearCommError(Commhandle,ErrorFlag,@CommStat);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage('发送数据出错');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(WriteOverlapped.hEvent);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Receive :=False;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(Commhandle);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(PostEvent);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;btnOpenCom.Enabled :=True;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(writeoverlapped.hEvent);<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;StatusBar1.SimpleText:='已经发送了Byte个数:'+IntToStr(ALLBytesWritten);<br> &nbsp; &nbsp;btnReceiveData.OnClick(btnReceiveData);<br>end;<br><br>procedure TForm1.btnReceiveDataClick(Sender: TObject);<br>var<br> &nbsp; &nbsp;com_thread: Thandle;<br> &nbsp; &nbsp;ThreadID:DWORD;<br><br>begin<br> &nbsp; &nbsp;if not connected then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;StatusBar1.SimpleText := '未打开串口';<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;ReceiveData :=0;<br> &nbsp; &nbsp;Memo2.Clear;<br> &nbsp; &nbsp;FillChar(ReadOs,SizeOf(ReadOs),0);<br> &nbsp; &nbsp;ReadOs.Offset := 0;<br> &nbsp; &nbsp;ReadOs.OffsetHigh := 0;<br><br> &nbsp; &nbsp;// 建立通信监视线程<br> &nbsp; &nbsp;Com_Thread:=CreateThread(nil,0,@CommWatch,nil,0,ThreadID);<br> &nbsp; &nbsp;if (Com_Thread=0) then<br> &nbsp; &nbsp; &nbsp; &nbsp;MessageBox(Handle,'No CreateThread!',nil,mb_OK);<br> &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp;//设置DTR信号线<br> &nbsp; &nbsp;EscapeCommFunction(Commhandle,SETDTR);<br> &nbsp; &nbsp;StatusBar1.SimpleText := '正在接收数据...';<br>end;<br><br>end.
 
辛苦大家了~!
 
你创建了接收线程,但线程里什么都没做,也没有接收数据的代码,当然是接收不到数据的,我的串口接收数据函数,你参考一下。<br>function TCustomCom.Read(pData: PChar; DataLen: DWord): Boolean;<br>var<br> &nbsp;Overlapped: TOverlapped;<br> &nbsp;FEvent: TSimpleEvent;<br> &nbsp;iReceive, dwErrorFlags: DWord;<br>begin<br> &nbsp;Result := True;<br> &nbsp;Lock;<br> &nbsp;FEvent := TSimpleEvent.Create;<br> &nbsp;try<br> &nbsp; &nbsp;iReceive := 0;<br> &nbsp; &nbsp;FillChar(Overlapped, Sizeof(Overlapped), 0);<br> &nbsp; &nbsp;Overlapped.hEvent := FEvent.Handle;<br> &nbsp; &nbsp;ClearCommError(Fm_handle, dwErrorFlags, nil); //清除错误标志<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp;if not ReadFile(Fm_handle, pData[0], DataLen, DWord(iReceive), @Overlapped) and<br> &nbsp; &nbsp; &nbsp; &nbsp;(GetLastError &lt;&gt; ERROR_IO_PENDING) then<br> &nbsp; &nbsp; &nbsp; &nbsp;abort;<br><br> &nbsp; &nbsp; &nbsp;if FEvent.WaitFor(Fm_ReadTimeout) = wrSignaled then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;GetOverlappedResult(Fm_handle, Overlapped, dWord(iReceive), False);<br> &nbsp; &nbsp; &nbsp; &nbsp;FEvent.ResetEvent;<br> &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp;else abort;<br> &nbsp; &nbsp;except<br> &nbsp; &nbsp; &nbsp;Result := False;<br> &nbsp; &nbsp;end;<br> &nbsp;finally<br> &nbsp; &nbsp;FEvent.Free;<br> &nbsp; &nbsp;Unlock;<br> &nbsp; &nbsp;Result := Result and (iReceive = DataLen);<br> &nbsp;end;<br>end;
 
这种通信方式是同步还是异步(重叠)的呢?
 
ReadFile(Fm_handle, pData[0], DataLen, DWord(iReceive), @Overlapped) <br>是异步的
 
呵呵,好像是异步的哈:)<br>如果要改成同步的,是不是把所有函数中的lpOverlapped参数删除就可以了呢?
 
To:nicai_wgl<br>你说线程里什么都没做,也没有接收数据的代码,那程序中 procedure TForm1.WMCOMMNOTIFY(var Message :TMessage); // 消息处理函数 &nbsp;是做什么用的呢?
 
Sorry,没有仔细看代码,你是怎么调试的,<br>procedure TForm1.WMCOMMNOTIFY(var Message :TMessage); 设置断点试试。
 
到现在我也没弄明白怎么回事,大家帮帮我啊~~~~
 
转别人的<br><br>在Windows 95/NT中,WM_COMMNOTIFY消息已经取消,在串行口产生一个通信事件时,程序并不会收到通知消息。线程需要调用WaitCommEvent函数来监视发生在串行口中的各种事件,该函数的第二个参数返回一个事件屏蔽变量,用来指示事件的类型。线程可以用SetCommMask建立事件屏蔽以指定要监视的事件,表12.4列出了可以监视的事件。调用GetCommMask可以查询串行口当前的事件屏蔽。
 
帮顶! <br><br>http://www.source520.com <br><br>站长开发推广同盟 站长朋友的终极驿站 <br>同时拥有海量源码电子经典书籍下载 <br><br>http://www.source520.com/search/search.asp <br><br>&quot;编程.站长&quot;论坛搜索引擎-----为中国站长注入动力!
 
我也用《Delphi串口通信技术与工程实践》(人民邮电出版社 赵兰涛 苏彦华 编著)附带光盘中的程序——电话语音查询系统(11章),程序运行时出现&quot;tapi设备不支持语音扩展&quot;的错误提示框.光标停在apdtapidevice1.voiceenable=false上!哪位高手知道请赐教!谢谢了
 
后退
顶部