我在运行时调用库本机函数OpenSSPComPortUSB()
程序弹出窗口无法打开端口\。\ COM208。为什么回来
\。\ COM208,因为我已经通过PortNumber = 7;下面是Java代码和我的
目标是打开端口COM7以启动与Smart的串行通信
希望者。
// AccessITLLib.java
public class AccessITLLib
{
public static class SSP_COMMAND extends Structure
{
SSP_FULL_KEY Key;
public NativeLong BaudRate;
public byte PortNumber;
public NativeLong Timeout;
public byte SSPAddress;
public byte RetryLevel;
public byte EncryptionStatus;
public byte CommandDataLength;
public byte[] CommandData = new byte[255];
public byte ResponseStatus;
public byte ResponseDataLength;
public byte[] ResponseData = new byte[255];
public byte IgnoreError;
}
public interface ITLLib extends Library
{
ITLLib INSTANCE = (ITLLib) Native.loadLibrary("ITLSSPproc",
ITLLib.class);
public int OpenSSPComPort(SSP_COMMAND p);
public int OpenSSPComPortUSB(SSP_COMMAND p);
public int CloseSSPComPort();
public int CloseSSPComPortUSB();
}
public static void main(String[] args)
{
SSP_COMMAND commandStruct = new SSP_COMMAND();
commandStruct.BaudRate = new NativeLong(9600);
commandStruct.PortNumber =7;
commandStruct.Timeout = 500;
commandStruct.RetryLevel = 3;
commandStruct.IgnoreError = 1;
commandStruct.SSPAddress =16;
ITLLib.INSTANCE.CloseSSPComPort();
ITLLib.INSTANCE.CloseSSPComPortUSB();
ITLLib.INSTANCE.OpenSSPComPort(commandStruct);
ITLLib.INSTANCE.OpenSSPComPortUSB(commandStruct);
}
}
here are the c structures
typedef struct{
SSP_FULL_KEY Key;
unsigned long BaudRate;
unsigned long Timeout;
unsigned char PortNumber;
unsigned char SSPAddress;
unsigned char RetryLevel;
unsigned char EncryptionStatus;
unsigned char CommandDataLength;
unsigned char CommandData[255];
unsigned char ResponseStatus;
unsigned char ResponseDataLength;
unsigned char ResponseData[255];
unsigned char IgnoreError;
}SSP_COMMAND;
typedef struct{
unsigned long long FixedKey;
unsigned long long EncryptKey;
}SSP_FULL_KEY;
最佳答案
您的JNA结构定义与本机定义的布局不匹配。
JNA中的字段顺序必须与本机定义的字段顺序完全匹配(JNA的最新版本实际上要求您实现显式声明字段顺序的方法)。
您无意间交换了PortNumber
和Timeout
字段。
关于java - 无法打开端口,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14723188/