全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:801
推到 Plurk!
推到 Facebook!

紧急求救xcomm

尚未結案
pbggroup
一般會員


發表:1
回覆:1
積分:0
註冊:2004-07-23

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-07-23 10:10:30 IP:218.86.xxx.xxx 未訂閱
小弟正在做一个串口通讯的程序,用了xcomm组件的ondata事件来接受数据。 有一个问题就是:当单片机向串口发送数据很快时,整个程序就死了。大概100ms发送一次数据。请告诉我应该怎么办呢?是不是pc机响应太慢了的缘故啊?谢谢 我的一部分源码在下面: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, XComDrv, StdCtrls, ComCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; XComm1: TXComm; Timer1: TTimer; StatusBar1: TStatusBar; memo1: TMemo; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Timer1Timer(Sender: TObject); procedure decodedata(var data:array of char); procedure XComm1Data(Sender: TObject; const Received: Cardinal); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } public { Public declarations } end; var Form1: TForm1; end_code:array[0..3] of char; ok_code:array[0..3] of char; error_code:array[0..3] of char; error_count:integer; test:file; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin if xcomm1.Opened then exit; xcomm1.OpenDevice; end; procedure TForm1.Button2Click(Sender: TObject); begin if xcomm1.Opened then xcomm1.CloseDevice; end; procedure TForm1.FormCreate(Sender: TObject); begin ok_code:=#244 #0 #0 #0; //0xF4 0x00 0x00 0x00 end_code:=#244 #2 #0 #2; //0xf4 0x02 0x00 0x02 error_code:=#244 #255 #0 #255; //0xf4 0xff 0x00 0xff error_count:=0; assignfile(test,extractfilepath(application.ExeName) 'test.dat'); rewrite(test,1); end; procedure TForm1.Button3Click(Sender: TObject); begin xcomm1.SendData(ok_code,4); timer1.Enabled:=true; error_count:=0; end; procedure TForm1.Timer1Timer(Sender: TObject); begin error_count:=error_count 1; if error_count>=3 then begin Timer1.Enabled:=false; if xcomm1.Opened then xcomm1.CloseDevice; statusbar1.Panels[0].text:='通讯失败!请关闭后重试!'; exit; end; Xcomm1.PurgeOut; Xcomm1.Senddata(OK_code,4); statusbar1.Panels[0].text:='通讯失败,正在进行第' inttostr(error_count 1) '次重试!'; end; procedure TForm1.XComm1Data(Sender: TObject; const Received: Cardinal); var data:array[0..254] of char; begin statusbar1.Panels[0].Text:='接受到数据'; timer1.Enabled:=false; error_count:=0; xcomm1.ReadData(data,30); blockwrite(test,data,3); if (data[0]<>#244) then //问题就出现在这里,当单片机很快的发送0xF5开头的数据时,我的程序就死了 //好像根本就不执行这个判断 begin xcomm1.PurgeIn; xcomm1.CloseDevice; timer1.Enabled:=false; showmessage('not f4'); exit; end; decodedata(data);//我自己写的一个处理数据的过程 end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin if xcomm1.Opened then xcomm1.CloseDevice; closefile(test); end; end.
pbggroup
一般會員


發表:1
回覆:1
積分:0
註冊:2004-07-23

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-07-25 09:06:17 IP:218.86.xxx.xxx 未訂閱
高手们快来帮帮我啊,万分感谢啊!
系統時間:2024-05-03 2:35:13
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!