主题:求助,急!
这里是源代码
这是一个扫描枪的程序,功能就是让刷卡自动开柜门。
这个代码的功能就是卡与柜的初始化配对。卡里面本来有16位的序列号,当在电脑前刷卡的时候,电脑上会显示出卡号,让你自己设定柜号。
现在我想要的功能就是当下次在电脑前刷卡时会显示出已经保存好的柜号。
原理很简单,就是多加段代码,让刷卡的时候从一保存的数据库里查找出卡号对应的柜号,然后显示出来。由于我学的不是这门语言,所以不会语法,希望会的朋友帮个忙,谢谢~
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, MSCommLib_TLB,strutils, ExtCtrls, DB, ADODB,
Grids, DBGrids;
type
TForm6 = class(TForm)
ComboBox1: TComboBox;
Button2: TButton;
Edit1: TEdit;
Button4: TButton;
Memo1: TMemo;
MSComm1: TMSComm;
ADOQuery1: TADOQuery;
ADOConnection1: TADOConnection;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Button1: TButton;
Label1: TLabel;
Edit2: TEdit;
Label2: TLabel;
Label3: TLabel;
Button3: TButton;
Button5: TButton;
procedure Button2Click(Sender: TObject);
function gbshow:integer;
procedure Button4Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure MSComm1Comm(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form6: TForm6;
recdata,jieshoustr,shujustr,shujustr1:string;
// biaoji,shujuint:integer;
shuint: array [0..17] of integer;
implementation
uses unit1;
{$R *.dfm}
function hex(c:char):Integer ;
var
x:integer;
begin
if c=' ' then
x:=0
else if (Ord(c)>=ord('0')) and (Ord(c)<=ord('9')) then
x:=Ord(c)-Ord('0')
else if (Ord(c)>=ord('a')) and (Ord(c)<=ord('f')) then
x:=Ord(c)-Ord('a')+10
else if (Ord(c)>=ord('A')) and (Ord(c)<=ord('F')) then
x:=Ord(c)-Ord('A')+10
else
//输入错误
x:=-1;
Result:=x;
end;
function HexToInt(S:String): Integer;
var
tmpInt1,tmpInt2:Integer ;
begin
if Length(S)=1 then
begin
Result:=hex(S[1]);
end
else if Length(S)=2 then
begin
tmpInt1:=hex(S[1]);
tmpInt2:=hex(S[2]);
if (tmpInt1=-1) or (tmpInt2=-1) then
Result:=-1
else
Result:= tmpInt1*16+tmpInt2;
end
else
//输入错误,转换失败
Result:=-1;
end;
procedure TForm6.MSComm1Comm(Sender: TObject);
var
i,k,j,InputLen:Integer;
tmpInt:Integer;
tmpvar:Variant;
InputString,pstring,p:String;
begin
mscomm1.OutBufferCount:=0;
if MSComm1.CommEvent=2 then //接收事件 =2表示接收二进制形式
begin
InputLen:=MSComm1.InBufferCount; //读取缓冲区字节数
tmpvar:=MSComm1.Input; //接收二进制数据,转换为十六进制显示
InputString:=''; //接收串口发来的数据,属于变体类型
for i:= 0 to InputLen-1 do
begin
tmpInt:=tmpvar[i];
p:=p+inttostr(tmpint); //按字节读取
InputString:=inputstring+IntToHex(tmpInt,2));
end;
edit1.text:=edit1.text+p;
memo1.text:=memo1.text+inputstring;
end;
edit2.Text:=edit2.Text+InputString;
end;
procedure TForm6.Button2Click(Sender: TObject); //串口设置
begin //设置COM1
MSComm1.CommPort :=1; //接收模式为二进制方式
MSComm1.InputMode:=1;
try
MSComm1.PortOpen :=true; //打开串口
except
showmessage('串口打开失败');
exit;
end;
button1.Enabled:=true;
button2.Enabled:=false;
BUTTON4.Enabled:=true;
end;
procedure TForm6.Button4Click(Sender: TObject);
var s:string ;
begin
if (edit1.Text='') and (edit2.Text='') then begin
MessageBox(Handle, '柜号和卡号不能为空!', '提示', MB_OK+MB_ICONINFORMATION);
exit;
end ;
with adoquery1 do begin
close;
sql.Clear;
sql.Text:='insert into LockId values(:a,:b)';
parameters.ParamByName('a').Value:=trim(edit1.Text);//柜号
parameters.ParamByName('b').Value:=trim(edit2.Text);//卡号
try
ExecSQL;
// open;
except
MessageBox(Handle, '数据操作失败,请查明原因', '错误', MB_OK+MB_ICONERROR);
// Exit;
end;
gbshow;
showmessage('添加成功:'+#10+'柜号为:'+trim(edit1.Text)+#10+'卡号为:'+trim(edit2.Text));
end;
end;
procedure TForm6.FormShow(Sender: TObject);
begin
combobox1.ItemIndex:=0;
BUTTON1.Enabled:=False;
BUTTON4.Enabled:=False;
gbshow;
end;
procedure TForm6.Button5Click(Sender: TObject);
begin
close;
end;
function TForm6.gbshow:integer;
begin
with adoquery1 do begin
close;
sql.Clear;
sql.Text:='select * from LockId order by val(GuiId)' ;
open;
end;
result:=1;
end;
procedure TForm6.Button3Click(Sender: TObject);
begin
if messagebox(self.Handle,'确定要删除吗?','提示',mb_yesno+mb_iconquestion)=idyes then
adoquery1.Delete;
end;
procedure TForm6.Button1Click(Sender: TObject);
begin
try
mscomm1.PortOpen:=false;
except
showmessage('关闭串口失败');
exit;
end;
button1.Enabled:=false;
button2.Enabled:=true;
button4.Enabled:=false;
end;
procedure TForm6.FormCreate(Sender: TObject);
begin //extractfilepath(application.exename) ExtractFilePath(application.ExeName)
adoconnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+extractfilepath(application.exename)+'IDlock.mdb;Persist Security Info=False' ;
end;
end.
这是一个扫描枪的程序,功能就是让刷卡自动开柜门。
这个代码的功能就是卡与柜的初始化配对。卡里面本来有16位的序列号,当在电脑前刷卡的时候,电脑上会显示出卡号,让你自己设定柜号。
现在我想要的功能就是当下次在电脑前刷卡时会显示出已经保存好的柜号。
原理很简单,就是多加段代码,让刷卡的时候从一保存的数据库里查找出卡号对应的柜号,然后显示出来。由于我学的不是这门语言,所以不会语法,希望会的朋友帮个忙,谢谢~
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, MSCommLib_TLB,strutils, ExtCtrls, DB, ADODB,
Grids, DBGrids;
type
TForm6 = class(TForm)
ComboBox1: TComboBox;
Button2: TButton;
Edit1: TEdit;
Button4: TButton;
Memo1: TMemo;
MSComm1: TMSComm;
ADOQuery1: TADOQuery;
ADOConnection1: TADOConnection;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Button1: TButton;
Label1: TLabel;
Edit2: TEdit;
Label2: TLabel;
Label3: TLabel;
Button3: TButton;
Button5: TButton;
procedure Button2Click(Sender: TObject);
function gbshow:integer;
procedure Button4Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure MSComm1Comm(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form6: TForm6;
recdata,jieshoustr,shujustr,shujustr1:string;
// biaoji,shujuint:integer;
shuint: array [0..17] of integer;
implementation
uses unit1;
{$R *.dfm}
function hex(c:char):Integer ;
var
x:integer;
begin
if c=' ' then
x:=0
else if (Ord(c)>=ord('0')) and (Ord(c)<=ord('9')) then
x:=Ord(c)-Ord('0')
else if (Ord(c)>=ord('a')) and (Ord(c)<=ord('f')) then
x:=Ord(c)-Ord('a')+10
else if (Ord(c)>=ord('A')) and (Ord(c)<=ord('F')) then
x:=Ord(c)-Ord('A')+10
else
//输入错误
x:=-1;
Result:=x;
end;
function HexToInt(S:String): Integer;
var
tmpInt1,tmpInt2:Integer ;
begin
if Length(S)=1 then
begin
Result:=hex(S[1]);
end
else if Length(S)=2 then
begin
tmpInt1:=hex(S[1]);
tmpInt2:=hex(S[2]);
if (tmpInt1=-1) or (tmpInt2=-1) then
Result:=-1
else
Result:= tmpInt1*16+tmpInt2;
end
else
//输入错误,转换失败
Result:=-1;
end;
procedure TForm6.MSComm1Comm(Sender: TObject);
var
i,k,j,InputLen:Integer;
tmpInt:Integer;
tmpvar:Variant;
InputString,pstring,p:String;
begin
mscomm1.OutBufferCount:=0;
if MSComm1.CommEvent=2 then //接收事件 =2表示接收二进制形式
begin
InputLen:=MSComm1.InBufferCount; //读取缓冲区字节数
tmpvar:=MSComm1.Input; //接收二进制数据,转换为十六进制显示
InputString:=''; //接收串口发来的数据,属于变体类型
for i:= 0 to InputLen-1 do
begin
tmpInt:=tmpvar[i];
p:=p+inttostr(tmpint); //按字节读取
InputString:=inputstring+IntToHex(tmpInt,2));
end;
edit1.text:=edit1.text+p;
memo1.text:=memo1.text+inputstring;
end;
edit2.Text:=edit2.Text+InputString;
end;
procedure TForm6.Button2Click(Sender: TObject); //串口设置
begin //设置COM1
MSComm1.CommPort :=1; //接收模式为二进制方式
MSComm1.InputMode:=1;
try
MSComm1.PortOpen :=true; //打开串口
except
showmessage('串口打开失败');
exit;
end;
button1.Enabled:=true;
button2.Enabled:=false;
BUTTON4.Enabled:=true;
end;
procedure TForm6.Button4Click(Sender: TObject);
var s:string ;
begin
if (edit1.Text='') and (edit2.Text='') then begin
MessageBox(Handle, '柜号和卡号不能为空!', '提示', MB_OK+MB_ICONINFORMATION);
exit;
end ;
with adoquery1 do begin
close;
sql.Clear;
sql.Text:='insert into LockId values(:a,:b)';
parameters.ParamByName('a').Value:=trim(edit1.Text);//柜号
parameters.ParamByName('b').Value:=trim(edit2.Text);//卡号
try
ExecSQL;
// open;
except
MessageBox(Handle, '数据操作失败,请查明原因', '错误', MB_OK+MB_ICONERROR);
// Exit;
end;
gbshow;
showmessage('添加成功:'+#10+'柜号为:'+trim(edit1.Text)+#10+'卡号为:'+trim(edit2.Text));
end;
end;
procedure TForm6.FormShow(Sender: TObject);
begin
combobox1.ItemIndex:=0;
BUTTON1.Enabled:=False;
BUTTON4.Enabled:=False;
gbshow;
end;
procedure TForm6.Button5Click(Sender: TObject);
begin
close;
end;
function TForm6.gbshow:integer;
begin
with adoquery1 do begin
close;
sql.Clear;
sql.Text:='select * from LockId order by val(GuiId)' ;
open;
end;
result:=1;
end;
procedure TForm6.Button3Click(Sender: TObject);
begin
if messagebox(self.Handle,'确定要删除吗?','提示',mb_yesno+mb_iconquestion)=idyes then
adoquery1.Delete;
end;
procedure TForm6.Button1Click(Sender: TObject);
begin
try
mscomm1.PortOpen:=false;
except
showmessage('关闭串口失败');
exit;
end;
button1.Enabled:=false;
button2.Enabled:=true;
button4.Enabled:=false;
end;
procedure TForm6.FormCreate(Sender: TObject);
begin //extractfilepath(application.exename) ExtractFilePath(application.ExeName)
adoconnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+extractfilepath(application.exename)+'IDlock.mdb;Persist Security Info=False' ;
end;
end.