用STRING来接收.以下是使用MOXA的COMMLIB写的发送和接收文件的程序.
unit Unit1;
{
Api232.dll 和程序一起打包
}interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Timer1: TTimer;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}
function sio_ioctl(port, baud, mode: Longint): Longint; stdcall;external 'API232.DLL';
function sio_read(port: Longint; buf: String; len: Longint): Longint; stdcall;external 'API232.DLL';
function sio_write(port: Longint; buf:String; len: Longint): Longint; stdcall;external 'API232.DLL';
function sio_flush(port, func: Longint): Longint; stdcall;external 'API232.DLL';
function sio_iqueue(port: Longint): Longint; stdcall;external 'API232.DLL';
function sio_oqueue(port: Longint): Longint; stdcall;external 'API232.DLL';
function sio_lctrl(port, mode: Longint): Longint; stdcall;external 'API232.DLL';
function sio_close(port: Longint): Longint; stdcall;external 'API232.DLL';
function sio_open(port: Longint): Longint; stdcall;external 'API232.DLL';Var
Recive_flag,
Send_Flag,
BusyFlag : Boolean;
ResF,
SendF : File;
Buff : String;procedure TForm1.FormCreate(Sender: TObject);
Var
i1 : integer;
begin
SetLength(Buff,10240);//10k 缓冲,最大32K
Recive_flag:=False;
Send_Flag:=False;
BusyFlag:=False; I1:=sio_Open(0);
if i1<>0 then
Begin
showmessage('Com1 Open Err');
Exit;
End;
Sio_ioctl(0,14,3);//1:300 ... 12:9600 ,13:19200 ,14:38400bps... 115200
sio_Flush(0,2); I1:=sio_Open(1);
if i1<>0 then
Begin
showmessage('Com2 Open Err');
Exit;
End;
sio_ioctl(1,14,3);//12:9600 ,13:19200 ,14:38400bps... 115200
sio_Flush(1,2);
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
Var
i1:integer;
begin
sio_Close(0);
sio_Close(1);
{$i-}
CloseFile(ResF);
CloseFile(SendF);
{$i+}
i1:=ioResult;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
sio_Flush(0,2);//清缓冲
if SaveDialog1.Execute then exit;
Recive_Flag:=True;
AssignFile(ResF,SaveDialog1.FileName);
Rewrite(ResF,1);
end;procedure TForm1.Button3Click(Sender: TObject);
begin
Recive_Flag:=False;
CloseFile(ResF);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
sio_Flush(0,2);//清缓冲
if OpenDialog1.Execute then exit;
Send_Flag:=True;
AssignFile(SendF,OpenDialog1.FileName);
Rewrite(SendF,1);
end;procedure TForm1.Button4Click(Sender: TObject);
begin
Send_Flag:=False;
CloseFile(SendF);
end;procedure TForm1.Timer1Timer(Sender: TObject);
Var
i1,i2 : Integer;
P : Pointer;
begin
P:=@Buff[1];
if Recive_Flag then
Begin
if sio_iqueue(0)>0 then
Begin
i1:=sio_Read(0,Buff,sio_iqueue(0));
if (i1>0) then BlockWrite(ResF,P^,i1,i2);//如果有可能超过10k,强制只读10k
End;
End;
if BusyFlag then Exit;
if Send_Flag then
Begin
if Not Eof(SendF) then
Begin
BlockRead(SendF,P^,10240,i2);
sio_Write(1,Buff,i2);
End;
End;
end;end.
unit Unit1;
{
Api232.dll 和程序一起打包
}interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Timer1: TTimer;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}
function sio_ioctl(port, baud, mode: Longint): Longint; stdcall;external 'API232.DLL';
function sio_read(port: Longint; buf: String; len: Longint): Longint; stdcall;external 'API232.DLL';
function sio_write(port: Longint; buf:String; len: Longint): Longint; stdcall;external 'API232.DLL';
function sio_flush(port, func: Longint): Longint; stdcall;external 'API232.DLL';
function sio_iqueue(port: Longint): Longint; stdcall;external 'API232.DLL';
function sio_oqueue(port: Longint): Longint; stdcall;external 'API232.DLL';
function sio_lctrl(port, mode: Longint): Longint; stdcall;external 'API232.DLL';
function sio_close(port: Longint): Longint; stdcall;external 'API232.DLL';
function sio_open(port: Longint): Longint; stdcall;external 'API232.DLL';Var
Recive_flag,
Send_Flag,
BusyFlag : Boolean;
ResF,
SendF : File;
Buff : String;procedure TForm1.FormCreate(Sender: TObject);
Var
i1 : integer;
begin
SetLength(Buff,10240);//10k 缓冲,最大32K
Recive_flag:=False;
Send_Flag:=False;
BusyFlag:=False; I1:=sio_Open(0);
if i1<>0 then
Begin
showmessage('Com1 Open Err');
Exit;
End;
Sio_ioctl(0,14,3);//1:300 ... 12:9600 ,13:19200 ,14:38400bps... 115200
sio_Flush(0,2); I1:=sio_Open(1);
if i1<>0 then
Begin
showmessage('Com2 Open Err');
Exit;
End;
sio_ioctl(1,14,3);//12:9600 ,13:19200 ,14:38400bps... 115200
sio_Flush(1,2);
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
Var
i1:integer;
begin
sio_Close(0);
sio_Close(1);
{$i-}
CloseFile(ResF);
CloseFile(SendF);
{$i+}
i1:=ioResult;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
sio_Flush(0,2);//清缓冲
if SaveDialog1.Execute then exit;
Recive_Flag:=True;
AssignFile(ResF,SaveDialog1.FileName);
Rewrite(ResF,1);
end;procedure TForm1.Button3Click(Sender: TObject);
begin
Recive_Flag:=False;
CloseFile(ResF);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
sio_Flush(0,2);//清缓冲
if OpenDialog1.Execute then exit;
Send_Flag:=True;
AssignFile(SendF,OpenDialog1.FileName);
Rewrite(SendF,1);
end;procedure TForm1.Button4Click(Sender: TObject);
begin
Send_Flag:=False;
CloseFile(SendF);
end;procedure TForm1.Timer1Timer(Sender: TObject);
Var
i1,i2 : Integer;
P : Pointer;
begin
P:=@Buff[1];
if Recive_Flag then
Begin
if sio_iqueue(0)>0 then
Begin
i1:=sio_Read(0,Buff,sio_iqueue(0));
if (i1>0) then BlockWrite(ResF,P^,i1,i2);//如果有可能超过10k,强制只读10k
End;
End;
if BusyFlag then Exit;
if Send_Flag then
Begin
if Not Eof(SendF) then
Begin
BlockRead(SendF,P^,10240,i2);
sio_Write(1,Buff,i2);
End;
End;
end;end.
object Form1: TForm1
Left = 192
Top = 107
Width = 222
Height = 188
Caption = '没有使用线程'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnClose = FormClose
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 52
Top = 132
Width = 108
Height = 13
Caption = 'Com1接收 Com2发送'
end
object Button1: TButton
Left = 22
Top = 22
Width = 75
Height = 25
Caption = '接收启动'
TabOrder = 0
OnClick = Button1Click
end
object Button2: TButton
Left = 22
Top = 64
Width = 75
Height = 25
Caption = '发送启动'
TabOrder = 1
OnClick = Button2Click
end
object Button3: TButton
Left = 116
Top = 22
Width = 75
Height = 25
Caption = '接收停止'
TabOrder = 2
OnClick = Button3Click
end
object Button4: TButton
Left = 116
Top = 64
Width = 75
Height = 25
Caption = '发送停止'
TabOrder = 3
OnClick = Button4Click
end
object Timer1: TTimer
OnTimer = Timer1Timer
Left = 2
Top = 4
end
object OpenDialog1: TOpenDialog
Left = 102
Top = 64
end
object SaveDialog1: TSaveDialog
Left = 104
Top = 14
end
end
还有我想要Api232.dll,应该从那里找,我的mail:[email protected],谢谢hlbl11.如果你有就请帮忙一下。
我想要,谢谢。[email protected]
给我一个[email protected]
谢谢
procedure TMainForm.MSComm1Comm(Sender: TObject);
var
v: Variant;
iHBound: integer;
reccomdata: array[0..64] of byte;
begin
case MSComm1.CommEvent of
comEvReceive:
begin
v:=MsComm1.Input;
iHBound:=VarArrayHighBound(v,1);
for i:=0 to iHBound do reccomdata[i]:= v[i];
// MSComm1.RThreshold:=nextrecsizi ;
end;
end;
如果你一定要用字符方式的话。我只用过二进制方式,想来差不多,以下是二进制方式的代码,我临时写的,可能有错。
var
P:^byte;
i:integer;
str:string;
begin
p:=buffer;
for i:=0 to bufferlength-1 do
begin
str:=str+chr(p^);
inc(p);
end;
showmessge(str);
end;
可以给我来E-Mail:[email protected].大家一起进步。呵呵,好运!