unit Unitwrite;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, SPComm, StdCtrls, Buttons;
type
TForm1 = class(TForm)
Label3: TLabel;
Edit1: TEdit;
BitBtn1: TBitBtn;
Label4: TLabel;
Edit5: TEdit;
BitBtn2: TBitBtn;
Button1: TButton;
Comm1: TComm;
ComboBox1: TComboBox;
Label1: TLabel;
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure FormDestroy(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure Edit5KeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
function ReadData: Boolean;
function WriteCard(sText: string): Boolean;
function ResetComm: Boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
Rubf: array[1..32] of byte;
implementation
{$R *.dfm}
function TForm1.WriteCard(sText: String): Boolean;
var
sBuffer: String;
I: Integer;
begin
sBuffer := sText;
Result := Comm1.WriteCommData(PChar(sBuffer), Length(sBuffer));
if Result then
begin
Result := False;
Rubf[1] := Byte($1B);
Rubf[2] := Byte($74);
Rubf[3] := Byte($1D);
Rubf[4] := Byte($1B);
Rubf[5] := Byte($5C);
//开始写指令
for I := 1 to 2 do
begin
if not comm1.WriteCommData(@Rubf,1) then
begin
Result := False;
break;
end;
//发送时字节间的延时
sleep(2);
end;
Comm1.WriteCommData(PChar(sBuffer), Length(sBuffer));
for I := 3 to 5 do
begin
if not comm1.WriteCommData(@Rubf,1) then
begin
Result := False;
break;
end;
sleep(2);
end;
end
else
begin
MessageBox(Handle,'发送写数据失败!!! ','',16);
end;
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
Buf: PChar;
Vstring: String;
I,Index: Integer;
begin
GetMem(Buf,BufferLength);
try
Move(Buffer^,Buf^,BufferLength);
for I := 1 to BufferLength do
Vstring := Vstring + Buf;
if Vstring = ('rq'+#0) then
begin
MessageBox(Handle,'读数据错误!!! ','',16);
Exit;
end;
//if Copy(Vstring,Length(Vstring)-1,1) = 'q' then
if Vstring = ('rq'+#0) then
begin
MessageBox(Handle,'写数据错误!!! ','',16);
Exit;
end;
Index := POS('s',Vstring);
Vstring := Copy(Vstring,Index+1,Length(Vstring)-1);
Index := POS('?',Vstring);
vstring := Copy(Vstring,1,Index-1);
Edit5.Text := Vstring;
{Rubf[1] := Byte($1B);
Rubf[2] := Byte($5D);
for I:= 1 to 32 do
begin
if not comm1.writecommdata(@Rubf,1) then
begin
break;
end;
//发送时字节间的延时
sleep(2);
end;}
finally
FreeMem(Buf);
end;
end;
function TForm1.ReadData: Boolean;
var
I: Integer;
begin
try
for I := 0 to 32 do
begin
Rubf := 0;
end;
Rubf[1] := Byte($1B);
Rubf[2] := Byte($5D);
Result := True;
for I := 1 to 32 do
begin
if not comm1.writecommdata(@Rubf,1) then
begin
Result := False;
Break;
end;
//发送时字节间的延时
Sleep(2);
end;
Result := True;
except
Result := False;
end;
end;
function TForm1.ResetComm: Boolean;
var
I: Integer;
begin
Exit;
try
for I := 0 to 32 do
begin
Rubf := 0;
end;
Rubf[1] := Byte($1B);
Rubf[2] := Byte($61);
Result := True;
for I := 1 to 32 do
begin
if not comm1.writecommdata(@Rubf,1) then
begin
Result := False;
Break;
end;
//发送时字节间的延时
Sleep(2);
end;
Result := True;
except
Result := False;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
if Comm1 <> nil then
begin
ResetComm;
Comm1.StopComm;
Comm1.Destroy;
end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if Trim(Edit1.Text) <> '' then
begin
WriteCard(Trim(Edit1.Text));
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Comm1.CommName := ComboBox1.Items[ComboBox1.ItemIndex];
Comm1.StartComm;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
ReadData;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ResetComm;
end;
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
Comm1.StopComm;
Comm1.CommName := ComboBox1.Items[ComboBox1.ItemIndex];
Comm1.StartComm;
end;
procedure TForm1.Edit5KeyPress(Sender: TObject; var Key: Char);
begin
if (Key = ';') or (Key = '?') then
Key := #0;
end;
end.