改过了--新的,看这个上面的不要了,还有从卡里面读回来的数据要如何操作,请告知。
我已经把源文件放在了论坛里面了,你可以去下载。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, SPComm, Buttons;
type
TForm1 = class(TForm)
Panel1: TPanel;
GroupBox1: TGroupBox;
Panel2: TPanel;
CN1: TComboBox;
BR1: TComboBox;
BS1: TComboBox;
PA1: TComboBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
SB1: TComboBox;
Bevel1: TBevel;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
SFCB1: TCheckBox;
Panel4: TPanel;
Memo1: TMemo;
Label11: TLabel;
Comm1: TComm;
Panel5: TPanel;
BitBtn7: TBitBtn;
BitBtn8: TBitBtn;
RXP1: TPanel;
TXP1: TPanel;
Label13: TLabel;
ListBox1: TListBox;
Label6: TLabel;
Panel3: TPanel;
BitBtn4: TBitBtn;
BitBtn5: TBitBtn;
Timer1: TTimer;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
BitBtn6: TBitBtn;
Label12: TLabel;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure BitBtn3Click(Sender: TObject);
procedure BitBtn8Click(Sender: TObject);
procedure BitBtn7Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure Comm1ReceiveError(Sender: TObject; EventMask: Cardinal);
procedure BitBtn4Click(Sender: TObject);
procedure BitBtn6Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure BitBtn5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Len: Integer; //消息指令流的长度
RBuf,SBuf: array [1..100] of byte; //发送和接受缓冲区大小为100个字节
RX,TX:LongWord; //发送和接受的字节数
F: TextFile; //接口数据的日志文件
Send_Msg:String; //发送的指令队列
implementation
{$R *.dfm}
function SendData(Len:Integer):Bool;
var
S1,S2,TP: String;
CommFLG:Boolean;
i: Integer;
begin
S1:='';
S2:='';
CommFLG:=True; //发送是否成功标记
Form1.TXP1.Color:=CLRED;
Application.ProcessMessages;
for i:=1 to Len do
begin
if not Form1.Comm1.WriteCommData(@SBuf,1) then
begin
CommFLG:=false; //发送数据不成功
break;
end;
S1:=S1+Char(SBuf); //ASC显示
S2:=S2+inttohex(SBuf,2)+''; //16进制显示
Sleep(1); //发送数据时每字节数据间的延时
end;
Form1.Memo1.Lines.Add(DateTimetoStr(Now)+' COM SEND DEC ---> '+S1);
Form1.Memo1.Lines.Add(DateTimetoStr(Now)+' COM SEND HEX ---> '+S2);
Form1.Memo1.Lines.Add('... ');
if Form1.SFCB1.Checked=True then
begin
Write(F,DateTimeToStr(NOW)+' COM SEND DEC ---> '+S1+#10); //写入文件
Write(F,DateTimeToStr(NOW)+' COM SEND HEX ---> '+S2+#10); //写入文件
Write(F,'...'+#10);
end;
TX:=TX+Int64(Len);
Form1.TXP1.Caption:=' TX: '+IntToStr(TX);
Form1.TXP1.Color:=CLWhite;
Application.ProcessMessages;
if not CommFLG then
SendData:=False
else
SendData:=True; //函数返回是否发送成功的消息
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
Panel5.Font.Color:=CLLIME;
Panel5.Caption:='端口状态:开启';
Comm1.StartComm
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
Comm1.StopComm;
Panel5.Font.Color:=CLRED;
Panel5.Caption:='端口状态:关闭';
if SFCB1.Checked=True then
begin
Flush(F);
CloseFile(F);
end;
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
S1,S2,TP: String;
i: Integer;
TCMD:Bool;
begin
S1:='';
S2:='';
TP:='';
TCMD:=False;
//接收RS232的数据并显示Memo1上。
Move(Buffer^,RBuf,BufferLength);
RX:=RX+BufferLength;
RXP1.Caption:=' RX: '+IntToStr(RX);
RXP1.Color:=CLLIME;
Application.ProcessMessages;
For i:=1 to BufferLength do
begin
Sleep(1); //接收数据时每字节数据间的延时
S1:=S1+Char(RBuf); //ASC显示
S2:=S2+inttohex(RBuf,2)+''; //16进制显示
end;
Memo1.Lines.Add(DateTimeToStr(Now)+' COM RECV DEC <--- '+S1);
Memo1.Lines.Add(DateTimeToStr(Now)+' COM RECV HEX <--- '+S2);
Memo1.Lines.Add('... ');
if SFCB1.Checked=True then
begin
Write(F,DateTimeToStr(NOW)+' COM RECV DEC <--- '+S1+#10); //写入文件
Write(F,DateTimeToStr(NOW)+' COM RECV HEX <--- '+S2+#10); //写入文件
Write(F,'...'+#10);
end;
if BufferLength=2 then
begin
For i:=1 to BufferLength do
begin
TP:=TP+Char(RBuf);
end;
if TP='A0' then
begin
TCMD:=True;
SBuf:=RBuf;
Len:=BufferLength;
if SendData(Len)=False then MessageDlg('无法回复读卡器进入待机状态 !' ,mterror,[mbyes],0); //调用发送函数
Panel3.Caption:='读卡器进入待机状态';
end;
if TP='B0' then
begin
TCMD:=True;
SBuf:=RBuf;
Len:=BufferLength;
if SendData(Len)=False then MessageDlg('无法回复读卡器数据准备就绪 !' ,mterror,[mbyes],0); //调用发送函数
Panel3.Caption:='卡正确插入,数据准备就绪。';
BitBtn4.Enabled:=True;
BitBtn5.Enabled:=True;
BitBtn6.Enabled:=True;
end;
if TP='C0' then
begin
TCMD:=True;
Panel3.Caption:='卡插反或错误卡';
BitBtn4.Enabled:=False;
BitBtn5.Enabled:=False;
BitBtn6.Enabled:=False;
end;
if TP='CC' then
begin
TCMD:=True;
Panel3.Caption:='卡注册成功';
end;
if TP='D0' then
begin
TCMD:=True;
Panel3.Caption:='卡已经拔出';
BitBtn4.Enabled:=False;
BitBtn5.Enabled:=False;
BitBtn6.Enabled:=False;
end;
if TP='F0' then
begin
TCMD:=True;
Panel3.Caption:='注册卡要求已经被接受';
Send_Msg:='';
Send_Msg:=Edit1.Text+Edit2.Text+Edit3.Text+Edit4.Text; //写卡请求已经被读卡器认证,可以写入数据。
Len:=Length(Send_Msg);
Move(Pchar(Send_Msg)^,SBuf,Len);
if SendData(Len)=False then MessageDlg('无法发送注册卡指令消息,指令如下:'+Send_Msg,mterror,[mbyes],0); //调用发送函数
end;
end;
// AA XX(128个数据) BB
// (引导符) ( 结束位)
// 共 132个数据
if BufferLength=132 then
begin
//要怎么处理,你的问题里面没有要求,不过数据已经保存在了流水记录里面了。
end;
if ((BufferLength<>2) and (BufferLength<>132)) or (TCMD=False) then
begin
Memo1.Lines.Add('');
Memo1.Lines.Add(' [ --- COM RECV Error --- ]');
Memo1.Lines.Add('...');
if SFCB1.Checked=True then
begin
Write(F,'...'+#10);
Write(F,DateTimeToStr(NOW)+' --- COM RECV Error --- '+#10); //写入文件
Write(F,'...'+#10);
end;
end;
Application.ProcessMessages;
RXP1.Color:=CLWhite;
Application.ProcessMessages;
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
begin
Comm1.StopComm;
Panel5.Font.Color:=CLRED;
Panel5.Caption:='端口状态:关闭';
Application.ProcessMessages;
Sleep(1000);
Panel5.Font.Color:=CLYELLOW;
Panel5.Caption:='端口状态:正在进行设置';
Application.ProcessMessages;
Comm1.CommName:=CN1.Text;
Comm1.BaudRate:=StrToInt(BR1.Text);
Case StrToInt(SB1.Text) of
1: Comm1.StopBits:=_1;
2: Comm1.StopBits:=_2;
end;
Case StrToInt(BS1.Text) of
8: Comm1.ByteSize:=_8;
7: Comm1.ByteSize:=_7;
6: Comm1.ByteSize:=_6;
5: Comm1.ByteSize:=_5;
end;
if PA1.Text='NONE' then Comm1.Parity:=NONE;
if PA1.Text='ODD' then Comm1.Parity:=ODD;
if PA1.Text='EVWN' then Comm1.Parity:=EVEN;
if PA1.Text='MARK' then Comm1.Parity:=MARK;
if PA1.Text='SPACE' then Comm1.Parity:=SPACE;
if Comm1.Parity<>NONE then Comm1.ParityCheck:=True;
Sleep(2000); //启动通讯端口时延时
Application.ProcessMessages;
Comm1.StartComm;
Panel5.Font.Color:=CLLIME;
Panel5.Caption:='端口状态:开启';
end;
procedure TForm1.BitBtn8Click(Sender: TObject);
begin
Memo1.Clear;
end;
procedure TForm1.BitBtn7Click(Sender: TObject);
begin
RXP1.Caption:=' RX: 0';
TXP1.Caption:=' TX: 0';
RX:=0;
TX:=0;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
//Create File 建立Log文件 ***************
if FileExists('C:/ComPort.LOG')<>True then
ListBox1.Items.SaveToFile('C:/ComPort.LOG');
//***************************************
AssignFile(F, 'C:/ComPort.Log');
Append(F);
RX:=0;
TX:=0;
SFCB1.Checked:=True;
end;
procedure TForm1.Comm1ReceiveError(Sender: TObject; EventMask: Cardinal);
begin
Memo1.Lines.Add('...');
Memo1.Lines.Add('ERROR INFORMATION:');
Memo1.Lines.Add('COM PORT ERROR.');
Memo1.Lines.Add('...');
if SFCB1.Checked=True then
begin
Write(F,'...'+#10);
Write(F,'ERROR INFORMATION:'+#10);
Write(F,DateTimeToStr(NOW)+'COM PORT ERROR.'); //写入文件
Write(F,'...'+#10);
end;
end;
procedure TForm1.BitBtn4Click(Sender: TObject);
begin
Send_Msg:='';
Send_Msg:='F0';
Len:=Length(Send_Msg);
Move(Pchar(Send_Msg)^,SBuf,Len);
if SendData(Len)=False then MessageDlg('无法发送注册卡指令!' ,mterror,[mbyes],0); //调用发送函数
end;
procedure TForm1.BitBtn6Click(Sender: TObject);
begin
Send_Msg:='';
Send_Msg:='B0';
Len:=Length(Send_Msg);
Move(Pchar(Send_Msg)^,SBuf,Len);
if SendData(Len)=False then MessageDlg('无法发送读卡指令!' ,mterror,[mbyes],0); //调用发送函数
end;
procedure TForm1.BitBtn5Click(Sender: TObject);
begin
Send_Msg:='';
Send_Msg:='D0';
Len:=Length(Send_Msg);
Move(Pchar(Send_Msg)^,SBuf,Len);
if SendData(Len)=False then MessageDlg('无法发送清卡指令!' ,mterror,[mbyes],0); //调用发送函数
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var X,Y : integer;
begin
X:=Comm1.GetModemState;
if X=0 then Label12.Caption:='连接状态:脱机' else Label12.Caption:='连接状态:联机';
end;
end.