[DUG] RFID - EAccess violation
Jolyon Smith
jsmith at deltics.co.nz
Thu May 28 08:25:41 NZST 2015
The debugger should be breaking (as in break-point) on the line causing the
AV. Tell us where that is and we may be able to help.
I would also suggest that you greatly simplify the code you are using to
work with the buffers being passed to and received from the API. The more
steps you introduce to achieve something, the greater chance that one of
those steps will introduce an inadvertent error (and make it harder to
identify where that error is creeping in.
For example, if the API requires a pointer to a null terminated key value
of 6 $FF bytes then simply declare a constant and pass the address of that,
rather than building a string. Apart from anything else, depending on
which version of Delphi you are using, your technique of using an
intermediate "String" may yield a PANSIChar (char = byte-size) or PWIDEChar
(char = word-size) and one of these is almost certainly not what the API
you are using is expecting.
And note that even if this technique works in a version of Delphi where
String = ANSI, it will likely break when you upgrade and recompile in a
version where String = UTF16.
Using a constant you not only eliminate a whole chunk of unnecessary code
but can be entirely explicit in what you are trying to achieve, so for a
byte buffer:
const
KEY = array[0..6] of Byte = ($FF,$FF,$FF,$FF,$FF,$FF,$00);
receive:=API_PCDRead(0,0,$00,10,1, at KEY,bufferr);
This might even be the cause of your problem.
On 28 May 2015 at 00:14, Robo <robo555 at gmail.com> wrote:
> Are you able to debug which line of code the error is occuring on?
>
> On Wed, May 27, 2015 at 1:56 PM, Marshland Engineering <
> marshland at marshland.co.nz> wrote:
>
>> I'm a bit out my depth. Is there something I have missed ?
>>
>> EAccess violation at address 000000000
>>
>> I purchased an RFID reader with sample code. I stripped out what was not
>> necessary and ended up with this.
>>
>> Sorry for the long code
>>
>> **************************************************************
>>
>> unit Unit1;
>>
>> interface
>>
>> uses
>> Windows, Messages, SysUtils, Variants, Classes, Controls, Forms,
>> ComCtrls, StdCtrls, XPMenu, ExtCtrls;
>>
>> type
>> TForm1 = class(TForm)
>> showm: TRichEdit;
>> Button1: TButton;
>> procedure falsereason(s:string);
>> procedure bReadClick(Sender: TObject);
>> private
>> { Private declarations }
>> public
>> { Public declarations }
>> end;
>>
>> function
>>
>> API_PCDRead(comHandle:Thandle;DeviceAddress:integer;mode:byte;add_blk,num_blk:integer;
>> snr,buffer:pchar):integer;stdcall;external 'mi.dll';
>>
>> var
>> Form1: TForm1;
>> comhandle : Thandle ;
>> reason:string;
>> implementation
>>
>> {$R *.dfm}
>>
>> procedure TForm1.falsereason(s:string);
>> begin
>> if s='' then begin reason:=''; exit; end;
>> if s='00' then begin reason:=''; exit ; end;
>> etc
>> if s='96' then begin reason:='The Operation Do Not Success....';
>> exit;
>> end;
>> end;
>>
>> procedure TForm1.bReadClick(Sender: TObject);
>> var
>> buffer,bufferr:array [0..255] of char;
>> receive,i :integer;
>> keydata,cardnum,showmemo:string;
>> key : pchar;
>> begin
>> setlength(keydata,6);
>> for i:=1 to 6 do begin
>> keydata[i]:=Chr(StrToInt('$FF'));
>> end;
>> key:=StrPCopy(buffer,keydata);
>> receive:=API_PCDRead(0,0,$00,10,1,key,bufferr);
>> case receive of
>> 0:begin
>> for i:=0 to 3 do begin
>> cardnum:=cardnum+ inttohex(ord(buffer[i]),2);
>> end;
>> showm.Lines.Add('Number :'+cardnum);
>> end;
>> 1:begin
>> falsereason(inttohex(ord(bufferr[0]),2));
>> showm.Lines.Add(reason+#13#10);
>> end
>> else
>> showm.Lines.Add('error,no data receive...'+#13#10);
>> end;
>> end;
>>
>> end.
>> ****************************************************************
>> I added the above to my code and it complies but crashes when I do a
>> receive.
>> EAccess
>>
>>
>> unit uMain;
>>
>> interface
>>
>> uses
>> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
>> Forms,
>> Dialogs, Grids, DBGrids, Menus, ComCtrls, StdCtrls, Printers, DBCtrls,
>> Mask, ExtCtrls, IniFiles, DB, ShellAPI, XPMenu;
>>
>>
>> type
>> TfMain = class(TForm)
>> pcMembers: TPageControl;
>> Members: TTabSheet;
>> Events: TTabSheet;
>> DBGrid1: TDBGrid;
>> DBGrid4: TDBGrid;
>> DBGrid5: TDBGrid;
>> Label3: TLabel;
>> bFees: TButton;
>> Button1: TButton;
>> bEmail: TButton;
>> dbgMembers: TDBGrid;
>> Label5: TLabel;
>> Label7: TLabel;
>> dbgFeesMem: TDBGrid;
>> dbgSubs: TDBGrid;
>> Label4: TLabel;
>> Label2: TLabel;
>> puSubs: TPopupMenu;
>> puSubsAdd: TMenuItem;
>> puSubsDel: TMenuItem;
>> puFeesListPrinter: TMenuItem;
>> puFeesListScreen: TMenuItem;
>> upFeesCardsNotSent: TMenuItem;
>> PrintSet1: TPrinterSetupDialog;
>> puEventFees: TPopupMenu;
>> puFeesAdd: TMenuItem;
>> puFeesDelete: TMenuItem;
>> puFeesList: TMenuItem;
>> puMembers: TPopupMenu;
>> puAddMember: TMenuItem;
>> puDeleteMember: TMenuItem;
>> puBikes: TPopupMenu;
>> puAddBike: TMenuItem;
>> puDeleteBike: TMenuItem;
>> dbmNotes: TDBMemo;
>> Label6: TLabel;
>> dbgIncident: TDBGrid;
>> Label8: TLabel;
>> PrintDialog1: TPrintDialog;
>> puIncident: TPopupMenu;
>> AddIncident1: TMenuItem;
>> DeleteIncident1: TMenuItem;
>> dtpMainDate: TDateTimePicker;
>> DBEdit1: TDBEdit;
>> Label1: TLabel;
>> Label9: TLabel;
>> cbMainYear: TComboBox;
>> DBGrid2: TDBGrid;
>> bPostFees: TButton;
>> Button2: TButton;
>> eCardNumber: TEdit;
>> procedure dbgMembersDblClick(Sender: TObject);
>> procedure FormCreate(Sender: TObject);
>> procedure dbgSubsDblClick(Sender: TObject);
>> procedure puSubsAddClick(Sender: TObject);
>> procedure puSubsDelClick(Sender: TObject);
>> procedure upFeesCardsNotSentClick(Sender: TObject);
>> procedure puFeesListPrinterClick(Sender: TObject);
>> procedure FormShow(Sender: TObject);
>> procedure dbmNotesExit(Sender: TObject);
>> procedure puFeesAddClick(Sender: TObject);
>> procedure puFeesDeleteClick(Sender: TObject);
>> procedure dbgFeesDblClick(Sender: TObject);
>> procedure dbgMembersKeyPress(Sender: TObject; var Key: Char);
>> procedure puFeesListClick(Sender: TObject);
>> procedure Button1Click(Sender: TObject);
>> procedure puAddMemberClick(Sender: TObject);
>> procedure puDeleteMemberClick(Sender: TObject);
>> procedure puAddBikeClick(Sender: TObject);
>> procedure puDeleteBikeClick(Sender: TObject);
>> procedure DBGrid2DblClick(Sender: TObject);
>> procedure dbmNotesEnter(Sender: TObject);
>> procedure puFeesListScreenClick(Sender: TObject);
>> procedure bEmailClick(Sender: TObject);
>> procedure DBGrid3ColExit(Sender: TObject);
>> procedure dbgIncidentCellClick(Column: TColumn);
>> procedure AddIncident1Click(Sender: TObject);
>> procedure DeleteIncident1Click(Sender: TObject);
>> procedure MembersShow(Sender: TObject);
>> procedure Button2Click(Sender: TObject);
>> procedure falsereason(s:string);
>> private
>> { Private declarations }
>> public
>> sKey:String;
>> bFeesFirstTime:Boolean;
>> end;
>>
>> function
>>
>> API_PCDRead(comHandle:Thandle;DeviceAddress:integer;mode:byte;add_blk,num_blk:integer;
>> snr,buffer:pchar):integer;stdcall;external 'mi.dll';
>>
>> var
>> fMain:TfMain;
>> comhandle:Thandle ;
>> Reason:String;
>>
>> implementation
>>
>> uses uDM, uMembers, uSubs, uMemCards, uEventFees, uLogon, uBikes,
>> uMemoShow,
>> uEmail, uIncident;
>>
>> {$R *.dfm}
>>
>> procedure TfMain.falsereason(s:string);
>> begin
>> if s='' then begin reason:=''; exit; end;
>> if s='00' then begin reason:=''; exit ; end;
>> etc
>> if s='95' then begin reason:='The lock Operation Do Not Success....';
>> exit; end;
>> if s='96' then begin reason:='The Operation Do Not Success....';
>> exit;
>> end;
>> end;
>>
>>
>> ******** My other code,
>>
>> procedure TfMain.Button2Click(Sender: TObject);
>> var
>> buffer,bufferr:array [0..255] of char;
>> receive,i :integer;
>> keydata,cardnum,showmemo:string;
>> key : pchar;
>> begin
>> setlength(keydata,6);
>> for i:=1 to 6 do begin
>> keydata[i]:=Chr(StrToInt('$FF'));
>> end;
>> key:=StrPCopy(buffer,keydata);
>> receive:=API_PCDRead(0,0,$00,10,1,key,bufferr);
>> case receive of
>> 0:begin
>> for i:=0 to 3 do begin
>> cardnum:=cardnum+ inttohex(ord(buffer[i]),2);
>> end;
>> showm.Lines.Add('Number :'+cardnum);
>> end;
>> 1:begin
>> falsereason(inttohex(ord(bufferr[0]),2));
>> showm.Lines.Add(reason+#13#10);
>> end
>> else
>> showm.Lines.Add('error,no data receive...'+#13#10);
>> end;
>> end;
>>
>> end.
>>
>> _______________________________________________
>> NZ Borland Developers Group - Delphi mailing list
>> Post: delphi at listserver.123.net.nz
>> Admin: http://delphi.org.nz/mailman/listinfo/delphi
>> Unsubscribe: send an email to delphi-request at listserver.123.net.nz with
>> Subject: unsubscribe
>>
>
>
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at listserver.123.net.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at listserver.123.net.nz with
> Subject: unsubscribe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20150528/e5c15723/attachment-0001.html
More information about the Delphi
mailing list