Original Working VB_Code
Private Declare Function ConnectReader Lib "rfidhid.dll" () As Integer
Private Declare Function DisconnectReader Lib "rfidhid.dll" () As Integer
Private Declare Function SetAntenna Lib "rfidhid.dll" (ByVal mode As Integer) As Integer
Private Declare Function Inventory Lib "rfidhid.dll" (ByRef tagdata As Byte, ByVal mode As Integer, ByRef taglen As Integer) As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim desc As String
desc = "1. Click ""Connect"" to talk to reader." & vbCr & vbCr
desc &= "2. Click ""RF On"" to wake up the TAG." & vbCr & vbCr
desc &= "3. Click ""Read Tag"" to get tag PCEPC."
lblDesc.Text = desc
End Sub
Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click
If cmdConnect.Text = "Connect" Then
If ConnectReader() Then
cmdConnect.Text = "Disconnect"
Else
MsgBox("Unable to connect to RFID Reader. Please check re开发者_高级运维ader connection.")
End If
Else
If DisconnectReader() Then
cmdConnect.Text = "Connect"
End If
End If
End Sub
Private Sub cmdRF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRF.Click
If cmdRF.Text = "RF On" Then
If SetAntenna(&HFF) Then
cmdRF.Text = "RF Off"
End If
Else
If SetAntenna(&H0) Then
cmdRF.Text = "RF On"
End If
End If
End Sub
Private Sub cmdReadTag_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReadTag.Click
Dim tagdata(64) As Byte
Dim taglen As Integer, cnt As Integer
Dim pcepc As String
pcepc = ""
If Inventory(tagdata(0), 1, taglen) Then
For cnt = 0 To taglen - 1
pcepc &= tagdata(cnt).ToString("X2")
Next
txtPCEPC.Text = pcepc
Else
txtPCEPC.Text = "ReadError"
End If
End Sub
Java Code (Simplified)
import com.sun.jna.Library;
import com.sun.jna.Native;
public class HelloWorld {
public interface MyLibrary extends Library {
public int ConnectReader();
public int SetAntenna (int mode);
public int Inventory (byte tagdata, int mode, int taglen);
}
public static void main(String[] args) {
MyLibrary lib = (MyLibrary) Native.loadLibrary("rfidhid", MyLibrary.class);
System.out.println(lib.ConnectReader());
System.out.println(lib.SetAntenna(255));
byte[] tagdata = new byte[64];
int taglen = 0;
int cnt;
String pcepc;
pcepc = "";
if (lib.Inventory(tagdata[0], 1, taglen) == 1) {
for (cnt = 0; cnt < taglen; cnt++)
pcepc += String.valueOf(tagdata[cnt]);
}
}
}
The error happens when lib.Inventory is run. lib.Inventory is used to get the tag from the RFID reader. If there is no tag, no error.
The error code
An unexpected error has been detected by Java Runtime Environment:
EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0b1d41ab, pid=5744, tid=4584
Java VM: Java HotSpot(TM) Client VM (11.2-b01 mixed mode windows-x86)
Problematic frame:
C [rfidhid.dll+0x141ab]
An error report file with more information is saved as: C:\eclipse\workspace\FelmiReader\hs_err_pid5744.log
At first blush, I suspect the problem is an incorrect type signature in the Inventory declaration.
If the parameter is ByRef, that implies a pointer (in C this would be a *byte
instead of a byte
). JNA has a DoubleByReference
class type that you probably should be using. You'll have a similar issue with the 3rd parameter (which is really an int*, not an int as you have it coded).
As an FYI, the native crash that you are getting should leave a dump file in the application directory. If you open that up, you should find the native call that was in place when the failure occurred - I'll bet money that it was the lib.Inventory(tagdata[0], 1, taglen) call.
Figuring out exactly which call is causing the crash will be key to tracking it down.
精彩评论