67,514
社区成员
发帖
与我相关
我的任务
分享
public class DataRecord extends AbstractBasicData<DataRecord> {
private static final int MAX_RECEIVE_BUF = 1024;
byte[] m_userid = new byte[12]; // 终端模块号码m_userid(1 To 12) As Byte
byte[] m_recv_date = new byte[20]; // 接收到数据包的时间m_recv_date(1 To 20) As Byte
byte[] m_data_buf = new byte[MAX_RECEIVE_BUF]; // 存储接收到的数据m_data_buf(1 To MAX_RECEIVE_BUF) As Byte
short m_data_len; // 接收到的数据包长度m_data_len As Integer
byte m_data_type; // 接收到的数据包类型m_data_type As Byte
public DataRecord() throws NativeException {
super(null);
createPointer();
mValue = this;
}
public Pointer createPointer() throws NativeException {
pointer = new Pointer(MemoryBlockFactory.createMemoryBlock(getSizeOf()));
return pointer;
}
public int getSizeOf() {
return 12 + 20 + 1024 + 16 + 1; // 8 WORDS of 2 bytes
}
public DataRecord getValueFromPointer() throws NativeException {
for(int i=0; i< m_userid.length; i++){
m_userid[i] = getNextByte();
}
for(int i=0; i< m_recv_date.length; i++){
m_recv_date[i] = getNextByte();
}
for(int i=0; i< m_data_buf.length; i++){
m_data_buf[i] = getNextByte();
}
m_data_len = getNextShort();
m_data_type = getNextByte();
return this;
}
public static DataRecord nGetDataRecord() throws NativeException, IllegalAccessException {
JNative nGetDataRecord = new JNative("wcomm_dll.dll", "do_read_proc");
DataRecord dataRecord = new DataRecord();
nGetDataRecord.setParameter(0, dataRecord.getPointer());
nGetDataRecord.setParameter(1, new NullPointer());
//nGetDataRecord.setParameter(2, ??);
nGetDataRecord.invoke();// 调用方法
return dataRecord.getValueFromPointer();
}
public static void main(String[] args) throws NativeException, IllegalAccessException {
DataRecord dataRecord = nGetDataRecord();
System.out.println("m_data_type:"+dataRecord.m_data_type);
System.out.println("m_data_len:"+dataRecord.m_data_len);
System.out.print("m_userid:");
for(int i=0; i< dataRecord.m_userid.length; i++){
System.out.print(dataRecord.m_userid[i]);
}
System.out.println();
System.out.print("m_recv_date:");
for(int i=0; i< dataRecord.m_recv_date.length; i++){
System.out.print(dataRecord.m_recv_date[i]);
}
System.out.println();
System.out.print("m_data_buf:");
for(int i=0; i< dataRecord.m_data_buf.length; i++){
System.out.print(dataRecord.m_data_buf[i]);
}
System.out.println();
}
}
Public Type data_record
m_userid(1 To 12) As Byte '终端模块号码
m_recv_date(1 To 20) As Byte '接收到数据包的时间
m_data_buf(1 To MAX_RECEIVE_BUF) As Byte '存储接收到的数据
m_data_len As Integer '接收到的数据包长度
m_data_type As Byte '接收到的数据包类型
End Type
package test;
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.pointers.NullPointer;
public class JnativeCall implements Runnable {
public static void main(String[] args) {
try {
JnativeCall jnativeCall = new JnativeCall();
//设置传输模式
System.out.println("传输模式:" + jnativeCall.SetWorkMode(0));
//设置传输协议
System.out.println("传输协议:" + jnativeCall.SelectProtocol(1));
//启动服务并设置端口
String startFlag = jnativeCall.startNetService();
if(startFlag.equals("0")){
System.out.println("服务启动成功!");
}else{
System.out.println("服务启动失败!");
}
DataRecord dataRecord = jnativeCall.doReadProc();
System.out.println("m_data_type:"+dataRecord.m_data_type);
System.out.println("m_data_len:"+dataRecord.m_data_len);
System.out.print("m_userid:");
for(int i=0; i< dataRecord.m_userid.length; i++){
System.out.print(dataRecord.m_userid[i]);
}
System.out.println();
System.out.print("m_recv_date:");
for(int i=0; i< dataRecord.m_recv_date.length; i++){
System.out.print(dataRecord.m_recv_date[i]);
}
System.out.println();
System.out.print("m_data_buf:");
for(int i=0; i< dataRecord.m_data_buf.length; i++){
System.out.print(dataRecord.m_data_buf[i]);
}
System.out.println();
System.out.println("---------------------------------");
// Thread thread = new Thread(jnativeCall);
// thread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public String SetWorkMode(int mode) throws Exception{
JNative start = null;
try {
start = new JNative("wcomm_dll.dll", "SetWorkMode");
start.setRetVal(Type.INT);
start.setParameter(0, mode);
start.invoke();// 调用方法
return start.getRetVal();
}finally{
if (start != null) {
start.dispose();// 释放
}
}
}
public String SelectProtocol(int protocal) throws Exception{
JNative start = null;
try {
start = new JNative("wcomm_dll.dll", "SelectProtocol");
start.setRetVal(Type.INT);
start.setParameter(0, protocal);
start.invoke();// 调用方法
return start.getRetVal();
}finally{
if (start != null) {
start.dispose();// 释放
}
}
}
public String startNetService() throws Exception{
JNative start = null;
try {
start = new JNative("wcomm_dll.dll", "start_net_service");
start.setRetVal(Type.INT);
start.setParameter(0, new NullPointer());
start.setParameter(1, 0);
start.setParameter(2, 3000);
start.setParameter(3, new NullPointer());
start.invoke();// 调用方法
return start.getRetVal();
}finally{
if (start != null) {
start.dispose();// 释放
}
}
}
public String stopNetService() throws Exception{
JNative start = null;
try {
start = new JNative("wcomm_dll.dll", "stop_net_service");
start.setRetVal(Type.INT);
start.invoke();// 调用方法
return start.getRetVal();
}finally{
if (start != null) {
start.dispose();// 释放
}
}
}
public DataRecord doReadProc() throws Exception{
JNative start = null;
DataRecord dataRecord = new DataRecord();
try {
start = new JNative("wcomm_dll.dll", "do_read_proc");
start.setRetVal(Type.VOID);
start.setParameter(0, dataRecord.getPointer());
start.setParameter(1, new NullPointer());
start.invoke();// 调用方法
return dataRecord.getValueFromPointer();
}finally{
if (start != null) {
start.dispose();// 释放
}
}
}
/**
* 让代码再飞一会
*/
public void run() {
System.out.println("JnativeCall run ...");
try {
boolean flag = true;
while(flag){
doReadProc();
Thread.sleep(3000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}