请教:如何实现边读文件,边发送至串口

yanni23 2011-05-18 10:21:45
下面是我写的代码,不知如何修改实现边读文件,边发送至串口,现在是把文件全部读取出来再发送至串口,谢谢
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls,StrUtils, ExtCtrls, SPComm;

type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Label1: TLabel;
ProgressBar1: TProgressBar;
Memo1: TMemo;
OpenDialog1: TOpenDialog;
Memo2: TMemo;
Button2: TButton;
Timer1: TTimer;
Comm1: TComm;
ComboBox1: TComboBox;
Label2: TLabel;
Button3: TButton;
ProgressBar2: TProgressBar;
//procedure FormShow(Sender:Tobject);
procedure FormClose(Sender:Tobject;var Action:TCloseAction);
procedure Button1Click(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
private
{ Private declarations }
public
{ Public declarations }
end;

var
j,fs:integer;
F:file;
max:integer;
LenF:longint;
Form1: TForm1;
//viewstring:string;//保存读取文件
sendstring:string;//存储发送文件
sbuf:array of byte;
rbuf:array[0..1024] of byte;
commflg:boolean;
function ReadFile0(FileName:String):boolean;

implementation

{$R *.dfm}
//procedure FormShow(Sender:Tobject);

//读取文件函数

function ReadFile0(FileName:String):boolean;
var
//F:File;
Src:Array[0..1024-1] of byte;
viewstring:string;
i,j,numread:Integer;
p:integer;
sp:pchar;
LenFH,LenFL:BYTE;
begin
result:=True;
if not FileExists(FileName) then
begin result:=false; exit; end;
AssignFile(F,FileName);
Reset(F,1);
p:=filesize(F);
max:=p;
LenF:=filesize(F);
setlength(sbuf,LenF);
if P < sizeof(Src) then
begin //Test
CloseFile(f);
showmessage('文件大小错误');
exit;
end
else numread:= sizeof(Src)-1;
if form1.ProgressBar1.Max>10 then
form1.ProgressBar1.Step:= form1.ProgressBar1.Max div 10
else
form1.ProgressBar1.Step:=form1.ProgressBar1.Max;
j:=3;
repeat
viewstring:='';
BlockRead(F,Src,sizeof(src),numread);
form1.ProgressBar1.Position:=0;
form1.ProgressBar1.Max:=numread;
if numread>0 then
begin
for p:=0 to numread-1 do
begin
viewstring:=viewstring+inttohex(Src[p],2)+' ';
if not Eof(F) then form1.ProgressBar1.position:=form1.ProgressBar1.position+1;
end;
form1.Memo1.lines.add(viewstring);
for i:=0 to numread-1 do
begin
sbuf[j]:=Src[i];
inc(j);
end;
Application.ProcessMessages;
end;
until ((NumRead = 0) and (form1.ProgressBar1.Position<=form1.ProgressBar1.Max));
CloseFile(F);
Application.ProcessMessages;
end;


procedure TForm1.ComboBox1Change(Sender: TObject);
begin
form1.Comm1.CommName:=form1.ComboBox1.Text;
end;
//关闭串口
procedure TForm1.FormClose(Sender:TObject;var Action:TCloseAction);
begin
form1.comm1.StopComm;//关闭窗体时,将comm1控件关闭
end;


procedure TForm1.Button1Click(Sender: TObject);
var LenFH,LenFL:longint;
viewstring1:string;
begin
form1.Comm1.CommName:=form1.ComboBox1.Text;
form1.Comm1.BaudRate:=38400;
form1.Comm1.Parity:=None;
form1.Comm1.ByteSize:=_8;
form1.Comm1.StopBits:=_1;
form1.comm1.StartComm;
//sleep(2);
memo1.Clear;
memo2.Clear;
if (OpenDialog1.Execute) and (Opendialog1.FileName<>'' )then
ReadFile0(Opendialog1.FileName);
if LenF>256 then
begin
LenFH:=LenF div 256;
LenFL:=LenF-256*LenFH;
end
else
begin
LenFH:=$00;
LenFL:=LenF;
end;
sbuf[0]:=$EB;//帧头
sbuf[1]:=LenFH;
sbuf[2]:=LenFL;
fs:=0;
commflg:=true;
if not form1.Comm1.WriteCommData(@sbuf[fs],1)then
begin commflg:=false; sleep(2); end;
if not commflg then MessageDlg('发送失败!',mterror,[mbyes],0)
else
begin
viewstring1:='';
viewstring1:=inttohex(sbuf[fs],2)+' ';
form1.Memo2.Lines.Add(viewstring1);
inc(fs);
end;
end;

procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
r:array[0..1024]of byte;
//viewstring2:string;
begin
move(buffer^,r,bufferlength);
rbuf[0]:=r[0];
if (fs=1) and (rbuf[0]=$EB) then
begin
for i:=1 to 5 do
begin
if not form1.Comm1.WriteCommData(@sbuf[fs],1)then
begin commflg:=false; sleep(2); end;
inc(fs);
sleep(2);
end;
end;
if (fs=6) and (rbuf[0]=$55) then
begin
if not form1.Comm1.WriteCommData(@sbuf[fs],1)then
begin commflg:=false; sleep(2); end;
inc(fs);
sleep(2);
end;
while fs<=LenF+2 do
begin
for fs:=7 to LenF+2 do
begin
if rbuf[0]=sbuf[fs-1] then
begin
if not form1.Comm1.WriteCommData(@sbuf[fs],1)then
begin commflg:=false; sleep(2); end;
sleep(2);
end;
end;
end;
if fs=LenF+2 then form1.Memo2.Lines.Add('下载成功');
form1.ProgressBar2.Max:=LenF+2;
form1.ProgressBar2.Step:=1;
form1.ProgressBar2.Position:=0;
if not eof(F) then form1.ProgressBar2.Position:=fs;
Application.ProcessMessages;
end;
end.
...全文
66 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
jinhx 2011-05-20
用 TFileStream 载入文件,载入时并不读取文件,postion移动时才从磁盘上读取实际数据
回复
dinoalex 2011-05-19
要么读完后,再从头循环提取和发送
回复
dinoalex 2011-05-19
线程,临界
回复
相关推荐
发帖
语言基础/算法/系统设计
创建于2007-08-02

1.6w+

社区成员

Delphi 语言基础/算法/系统设计
申请成为版主
帖子事件
创建了帖子
2011-05-18 10:21
社区公告
暂无公告