| 发表于:2007-03-06 16:26:0616楼 得分:0 |
博克,你好,按你写的类,我调试了一下,现在基本上搞定了。 但现在还有2个问题: 我先将我调试后的dll代码贴出: 以下是dll主文件: library project1; uses sysutils, classes, dialogs, unit1 in 'unit1.pas '; type //定义函数类型 p_formfun = function (log : string) : integer; var comclass : tmydllclass; function opencomm(scomm:pchar):boolean;stdcall; begin if comclass = nil then begin comclass := tmydllclass.create; end; if comclass.isopen then comclass.closecomm(scomm); result := comclass.opencomm(scomm); end; function closecomm(scomm:pchar):boolean;stdcall; begin result := true; if comclass <> nil then begin comclass.closecomm(scomm); end; end; function hextostr( //十六进制字符串处理成字符串 mhex: string //十六进制字符串 ): string;//返回处理后的字符串 var i: integer; begin result := ' '; mhex := stringreplace(mhex, #32, ' ', [rfreplaceall]); for i := 1 to length(mhex) div 2 do result := result + chr(strtointdef( '$ ' + copy(mhex, i * 2 - 1, 2), 0)); end; { hextostr } function strtohex( //字符串处理成十六进制字符串 mstr: string; //字符串 //mspace: boolean = false //是否用空格分开 mspace: boolean = true //是否用空格分开 ): string; //返回处理后的十六进制字符串 const cspacestr: array[boolean] of string = ( ' ', #32); var i: integer; begin result := ' '; for i := 1 to length(mstr) do result := format( '%s%s%.2x ', [result, cspacestr[mspace], ord(mstr[i])]); if mspace then delete(result, 1, 1); end; { strtohex } function sendrestoredata(scomm:string;sedtold:string;sedtnew1:string):boolean;stdcall; var srestore_init11:string; //复位卡的第一步:初始化指令。 begin result := false; srestore_init11:=hextostr( '6899999999999968050161cd16 '); if (comclass <> nil) and (comclass.isopen) then begin result := (comclass.sendbuffer(pchar(srestore_init11), length(srestore_init11)) > 0); jstimes:=11; myedtold:=sedtold; myedtnew1:=sedtnew1; end; end; function fun_test ( pfun:p_formfun) : integer ; stdcall ; begin pfun(strtohex(s_recv)); result := 0; end ; exports opencomm,closecomm,sendrestoredata,fun_test; begin end. 以下是dll中的单元: unit unit1; interface uses sysutils,classes,spcomm,dialogs; type tmydllclass = class private mycomm : tcomm; commopen : boolean; procedure recvdata(sender : tobject;buffer : pointer; bufferlength : word); public constructor create; destructor destroy; override; function opencomm(comname : pchar) : boolean; function sendbuffer(buffer : pchar; bufferlength : integer) : integer; function closecomm(comname : pchar) : boolean; function isopen : boolean; end; var s_recv:string; jstimes:integer; myedtold,myedtnew1:string; implementation constructor tmydllclass.create; begin mycomm := tcomm.create(nil); mycomm.onreceivedata := recvdata; end; destructor tmydllclass.destroy; begin if commopen then mycomm.stopcomm; end; function tmydllclass.sendbuffer(buffer : pchar; bufferlength : integer) : integer; begin result := 0; if commopen then begin try mycomm.writecommdata(buffer, bufferlength); result := bufferlength; except result := 0; end; end; end; function tmydllclass.opencomm(comname : pchar) : boolean; begin try mycomm.commname := comname; mycomm.baudrate:=2400; mycomm.startcomm; commopen := true; except commopen := false; end; result := commopen; end; function tmydllclass.closecomm(comname : pchar) : boolean; begin result := false; if commopen then begin mycomm.commname := comname; mycomm.stopcomm; commopen := false; result := true; end end; function hextostr( //十六进制字符串处理成字符串 mhex: string //十六进制字符串 ): string; //返回处理后的字符串 var i: integer; begin result := ' '; mhex := stringreplace(mhex, #32, ' ', [rfreplaceall]); for i := 1 to length(mhex) div 2 do result := result + chr(strtointdef( '$ ' + copy(mhex, i * 2 - 1, 2), 0)); end; { hextostr } function strtohex( //字符串处理成十六进制字符串 mstr: string; //字符串 //mspace: boolean = false //是否用空格分开 mspace: boolean = true //是否用空格分开 ): string; //返回处理后的十六进制字符串 const cspacestr: array[boolean] of string = ( ' ', #32); var i: integer; begin result := ' '; for i := 1 to length(mstr) do result := format( '%s%s%.2x ', [result, cspacestr[mspace], ord(mstr[i])]); if mspace then delete(result, 1, 1); end; { strtohex } procedure tmydllclass.recvdata(sender : tobject;buffer : pointer; bufferlength : word); var srestore_writeinit12,srestore_writebackinit13,srestore14:string; soldps,sd1,sd2,sd3,sd4,snewps,sn1,sn2,sn3,sn4: string; srestore:string; begin //----计算出原密码,新密码的字符串------ sd1:=copy(myedtold,7,2); sd2:=copy(myedtold,5,2); sd3:=copy(myedtold,3,2); sd4:=copy(myedtold,1,2); soldps:=inttohex((strtoint( '$ '+sd1)+51),2)+inttohex((strtoint( '$ '+sd2)+51),2)+inttohex((strtoint( '$ '+sd3)+51),2)+inttohex((strtoint( '$ '+sd4)+51),2); sn1:=copy(myedtnew1,7,2); sn2:=copy(myedtnew1,5,2); sn3:=copy(myedtnew1,3,2); sn4:=copy(myedtnew1,1,2); snewps:=inttohex((strtoint( '$ '+sn1)+51),2)+inttohex((strtoint( '$ '+sn2)+51),2)+inttohex((strtoint( '$ '+sn3)+51),2)+inttohex((strtoint( '$ '+sn4)+51),2); { //接收rs232的数据并显示memo1上。 setlength(s,bufferlength); move(buffer^,pchar(s)^,bufferlength); memo1.lines.add(strtohex(s)); memo1.invalidate; } setlength(s_recv,bufferlength); move(buffer^,pchar(s_recv)^,bufferlength); //**********点击了复位卡按纽。********************************** //如果第一步 "初始化 "成功,则进入第二步:写数据初始化。 if (strtohex(s_recv)= '68 ff ff ff ff ff ff 68 c5 01 62 f2 16 ') and (jstimes=11) then begin srestore_writeinit12:=hextostr( '68999999999999680301016b16 '); mycomm.writecommdata(pchar(srestore_writeinit12),length(srestore_writeinit12)); jstimes:=12; end; //如果第二步 "写数据初始化 "成功,则进入第三步:写返写区初始化。 if (strtohex(s_recv)= '68 ff ff ff ff ff ff 68 83 00 4d 16 ') and (jstimes=12) then begin srestore_writebackinit13:=hextostr( '68999999999999680301026c16 '); mycomm.writecommdata(pchar(srestore_writebackinit13),length(srestore_writebackinit13)); jstimes:=13; end; //如果第三步 "写返写区初始化 "成功,则进入最后一步第四步:复位卡。 if (strtohex(s_recv)= '68 ff ff ff ff ff ff 68 83 00 4d 16 ') and (jstimes=13) then begin srestore:= '6899999999999968041534339b4341cccccccccccc '+soldps+snewps+ 'f349ad16 '; srestore14:=hextostr(srestore); //srestore14:=hextostr( '6899999999999968041534339b4341cccccccccccc3334353633343536f349ad16 '); mycomm.writecommdata(pchar(srestore14),length(srestore14)); jstimes:=14; end; //如果第四步:复位卡成功,则提示复位卡制作成功。 if (strtohex(s_recv)= '68 ff ff ff ff ff ff 68 83 00 4d 16 ') and (jstimes=14) then begin showmessage( '复位卡的制作成功! '); jstimes:=15; end; //复位卡的异常处理,第一步发送指令后的返回结果不正确。 if (strtohex(s_recv)= '68 ff ff ff ff ff ff 68 c5 01 32 c2 16 ') and (jstimes=11)then messagedlg( '复位卡的操作失败,请将卡拿开,再放上去! ',mterror,[mbyes],0) ; end; function tmydllclass.isopen : boolean; begin result := commopen; end; end. | | |
|