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) = 'p' 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
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;
end;
function TForm1.ResetComm: Boolean;
var
I: Integer;
begin
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;
end;