0

I've converted the following from "RFIDAPI.dll" C function to Delphi:

bool SAAT_YTagSelect( void *pHandle, 
                      unsigned char nOpEnable, 
                      unsigned char nMatchType, 
                      unsigned char *MatchData, 
                      unsigned char nLenth )

to

function SAAT_YTagSelect( pHandle: Pointer; 
                          nOpEnable, 
                          nMatchType, 
                          MatchData, 
                          nLenth: PAnsichar): Boolean; stdcall;

I am trying to call the function and I am getting an Access Violation. Apparently I am not assigning the correct value to the nOpEnable 1Byte variable.

Variable nOpEnable Buzzer or LED enable(1byte):

1: enable
0: disable
7   6   5   4   3   led buzzer
N/A N/A N/A N/A N/A 1   1
procedure TForm5.Button4Click(Sender: TObject);
 var
  hp: Pointer;
  b: array[0..7] of AnsiChar;
begin
   b[0] := '1';
   b[1] := '1';
   b[2] := '0';
   b[3] := '0';
   b[4] := '0';
   b[5] := '0';
   b[6] := '0';
   b[7] := '0';
    if SAAT_YTagSelect(hp, b, '0x01', '84500080', '8') then
      StatusBar1.Panels[1].Text := 'Tag Selected';
end;
5
  • I believe you are asking for the c function: bool SAAT_YTagSelect(void *pHandle,unsignedchar nOpEnable,unsigned char nMatchType,unsigned char *MatchData,unsigned char nLenth) Commented Nov 10, 2014 at 1:38
  • 1
    Did you read this yet: rvelthuis.de/articles/articles-convert.html Commented Nov 10, 2014 at 7:26
  • 1
    FWIW, char * is a pointer to an 8-bit character in C, so it can be translated as PAnsiChar. The P in that name stands for pointer. So you would normally translate char to AnsiChar, not to PAnsiChar (big difference!). But char can also by a byte, in C, especially if it is used as unsigned char. So the correct translation of unsigned char is, most of the time, Byte. Commented Nov 10, 2014 at 21:17
  • 1
    And you should really read the article David referred to. I am slowly beginning to see David's point about you not wanting to learn. In the olden days, they used to call such a person a help vampire. Commented Nov 10, 2014 at 21:18
  • I did read the article, and yes trying to learn, eve though not a c# developer. For now just want to learn the basics. Commented Nov 13, 2014 at 22:37

2 Answers 2

2

The unsigned char parameters are 1-byte integral types, not strings... so they correspond to Delphi's Byte rather than PAnsiChar. The numbers for each buzzer/LED are the bit positions to set in that byte, not a character position in a string. So, the prototype should probably be:

function SAAT_YTagSelect(pHandle: Pointer; nOpEnable, nMatchType: Byte; MatchData: PByte; nLenth: Byte): Boolean; stdcall;

and the call should be something like:

procedure TForm5.Button4Click(Sender: TObject);
var
    hp: Pointer;
    b: Byte;
    data: PAnsiChar;
begin
    // set hp appropriately first
    b := 1 or 2; // Bitwise OR the values of each set bit
    data := '84500080';
    if SAAT_YTagSelect(hp, b, 1, PByte(data), 8) then
        StatusBar1.Panels[1].Text := 'Tag Selected';
end;

Also you haven't pointed hp at anything, which is likely a problem depending on what it's for.

Sign up to request clarification or add additional context in comments.

11 Comments

Is it possible that the pointer is being assigned when the following function is called: function SAAT_Open(pHandle: Pointer): Boolean; stdcall; ?
Thank you for your time! I am still getting the same error!
@user734781 Not unless that pHandle points to the other pHandle... so probably not.
Looks like the pHandle pointer is assigned through SAAT_TCPInit() or SAAT_COMInit()... after that, you pass it to each of the other functions.
unsigned char* is a byte array rather than a string. So that parameter should be PByte.
|
1

The correct translation of the function signature is:

function SAAT_YTagSelect(pHandle: Pointer; nOpEnable, nMatchType: Byte; MatchData: PByte; nLenth: Byte): Boolean; stdcall;

And the usage would look something like this:

var
  hp: Pointer;
  b: Byte;
  Data: PAnsiChar;
begin
   SAAT_TCPInit(hp, '192.168.0.238', 7086);
   SAAT_Open(hp);
   ...
   b := 1 or 2;
   data := '84500080';
   if SAAT_YTagSelect(hp, b, 1, PByte(data), 8) then
     StatusBar1.Panels[1].Text := 'Tag Selected';
   ...
   SAAT_Close(hp);
end;

I can't find any documentation for the SAAT_YTagSelect() function, so it is difficult to know for sure exactly what the MatchData parameter is expecting. Considering that you have a habit of using strings for numeric parameters, it might even be something more like this:

var
  hp: Pointer;
  b: Byte;
  Data: array[0..7] of Byte;
begin
   SAAT_TCPInit(hp, '192.168.0.238', 7086);
   SAAT_Open(hp);
   ...
   b := 1 or 2;
   data[0] := 8;
   data[1] := 4;
   data[2] := 5;
   data[3] := 0;
   data[4] := 0;
   data[5] := 0;
   data[6] := 8;
   data[7] := 0;
   if SAAT_YTagSelect(hp, b, $01, @data[0], 8) then
     StatusBar1.Panels[1].Text := 'Tag Selected';
   ...
   SAAT_Close(hp);
end;

4 Comments

Thank you Remy! It the call is not producing any errors, but its not activating the tag, I am wondering if the variable b which is the nOpEnable has to receive values to it's bit (a Byte has 8 bits right) There are two options buzzer and light that can be activated simultaneous, at the same time you can just activate the light not the buzzer. What would be the correct way to assign this values? Buzzer or LED enable(1byte): 1: enable 0:disable
Like I said, I cannot find the documentation for this particular function, so I don't know how the parameters should be set. A bit set makes sense, but whether it uses the low bits ($1 or $2, bitmask $03) or the high bits ($80 or $7F, bitmask $C0) is anyone's guess.
I could send you the documentation if you like!
@user734781: People try to help you, but it is bit too much to ask them to spend the best part of the day helping you. Try to find out a little bit yourself. ISTM you are trying to run before you can walk. Try something simpler first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.