有人可以指出我做错了什么吗?尝试使用 JNA 将 VB 映射到 Java 来访问该库
原始工作 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 reader 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 代码(简化)
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]);
}
}
}
运行 lib.Inventory 时发生错误。 lib.Inventory 用于从 RFID 阅读器获取标签。如果没有标签,则不会出现错误。
错误代码
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]
包含更多信息的错误报告文件保存为: C:\eclipse\workspace\FelmiReader\hs_err_pid5744.log
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 reader 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
乍一看,我怀疑问题是 Inventory 声明中的类型签名不正确。
如果参数是 ByRef,则意味着一个指针(在 C 中,这将是
*byte
而不是byte
)。 JNA 有一个您可能应该使用的DoubleByReference
类类型。第三个参数也会遇到类似的问题(它实际上是一个 int*,而不是您编码的 int)。仅供参考,您遇到的本机崩溃应该在应用程序目录中留下转储文件。如果你打开它,你应该找到发生故障时到位的本机调用 - 我敢打赌,这是 lib.Inventory(tagdata[0], 1, taglen) 调用。
准确找出导致崩溃的调用将是追踪崩溃的关键。
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 abyte
). JNA has aDoubleByReference
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.