M
mazheng
Unregistered / Unconfirmed
GUEST, unregistred user!
代码如下,为何spcomm总是发送错误
===========================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, ComCtrls, SPComm, jpeg, Unit2;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Edit1: TEdit;
Button1: TButton;
Panel1: TPanel;
Panel2: TPanel;
ProgressBar1: TProgressBar;
Label1: TLabel;
Button2: TButton;
Button3: TButton;
Edit2: TEdit;
Label2: TLabel;
SpComm: TComm;
OpenDialog: TOpenDialog;
Image1: TImage;
Label3: TLabel;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
FileHandle, // 文件名柄变量
FileLength, //源文件大小变量
num, //源文件被分割后的文件数目
iBytesRead : integer;
buf1 : integer; //文件缓冲区变量
{ Private declarations }
function wFileSize(AFileName:string):integer;
public
{ Public declarations }
end;
const
AFileSize=255;
var
Form1: TForm1;
implementation
//uses Unit2;
{$R *.dfm}
function TForm1.wFileSize(AFileName:string):integer;
var
F: file of Byte;
begin
try
AssignFile(F,AFileName);
Reset(F);
Result:=FileSize(F);
CloseFile(F); //
FileLength:=Result;
except
Result :=0;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog.Execute then
begin
edit1.Text:=OpenDialog.FileName;
Image1.Picture.LoadFromFile(OpenDialog.FileName);
edit2.Text:=inttostr(wFileSize(OpenDialog.FileName));
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Form2.ShowModal;
end;
procedure TForm1.Button3Click(Sender: TObject);
var Buffer:array of PChar;
i,temp:integer;
pszBuffer: PChar;
begin
buf1:= AFileSize;
num := Round(FileLength/AFileSize+1);
SetLength(Buffer,num+2);
//ShowMessage(inttostr(num));
temp:=buf1;
ProgressBar1.Max:=num;//设置进度条状态
ProgressBar1.Position:=0;
for i:=1 to num do
begin
pszBuffer := PChar(AllocMem(buf1));
try
if i=num then
begin
pszBuffer := PChar(AllocMem(FileLength-buf1*(num-1)));
temp:=FileLength-buf1*(num-1);
end;
FileHandle:=FileOpen(edit1.Text,fmOpenRead);
FileSeek(FileHandle,buf1*(i-1),0);
iBytesRead := FileRead(FileHandle, pszBuffer^, temp);
FileClose(FileHandle);
spcomm.ByteSize:=0;
if not spcomm.WriteCommData(pszBuffer,temp) then
begin
ShowMessage('发送错误,终止发送');
//ProgressBar1.Max:=1;
ProgressBar1.Position:=0;
break;
end;
ProgressBar1.Position:=i;
sleep(2);
finally
FreeMem(pszBuffer);
end;
end;
//ShowMessage('ok');
end;
end.
===========================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, ComCtrls, SPComm, jpeg, Unit2;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Edit1: TEdit;
Button1: TButton;
Panel1: TPanel;
Panel2: TPanel;
ProgressBar1: TProgressBar;
Label1: TLabel;
Button2: TButton;
Button3: TButton;
Edit2: TEdit;
Label2: TLabel;
SpComm: TComm;
OpenDialog: TOpenDialog;
Image1: TImage;
Label3: TLabel;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
FileHandle, // 文件名柄变量
FileLength, //源文件大小变量
num, //源文件被分割后的文件数目
iBytesRead : integer;
buf1 : integer; //文件缓冲区变量
{ Private declarations }
function wFileSize(AFileName:string):integer;
public
{ Public declarations }
end;
const
AFileSize=255;
var
Form1: TForm1;
implementation
//uses Unit2;
{$R *.dfm}
function TForm1.wFileSize(AFileName:string):integer;
var
F: file of Byte;
begin
try
AssignFile(F,AFileName);
Reset(F);
Result:=FileSize(F);
CloseFile(F); //
FileLength:=Result;
except
Result :=0;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog.Execute then
begin
edit1.Text:=OpenDialog.FileName;
Image1.Picture.LoadFromFile(OpenDialog.FileName);
edit2.Text:=inttostr(wFileSize(OpenDialog.FileName));
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Form2.ShowModal;
end;
procedure TForm1.Button3Click(Sender: TObject);
var Buffer:array of PChar;
i,temp:integer;
pszBuffer: PChar;
begin
buf1:= AFileSize;
num := Round(FileLength/AFileSize+1);
SetLength(Buffer,num+2);
//ShowMessage(inttostr(num));
temp:=buf1;
ProgressBar1.Max:=num;//设置进度条状态
ProgressBar1.Position:=0;
for i:=1 to num do
begin
pszBuffer := PChar(AllocMem(buf1));
try
if i=num then
begin
pszBuffer := PChar(AllocMem(FileLength-buf1*(num-1)));
temp:=FileLength-buf1*(num-1);
end;
FileHandle:=FileOpen(edit1.Text,fmOpenRead);
FileSeek(FileHandle,buf1*(i-1),0);
iBytesRead := FileRead(FileHandle, pszBuffer^, temp);
FileClose(FileHandle);
spcomm.ByteSize:=0;
if not spcomm.WriteCommData(pszBuffer,temp) then
begin
ShowMessage('发送错误,终止发送');
//ProgressBar1.Max:=1;
ProgressBar1.Position:=0;
break;
end;
ProgressBar1.Position:=i;
sleep(2);
finally
FreeMem(pszBuffer);
end;
end;
//ShowMessage('ok');
end;
end.