java - multiple instances of a JNI wrapper -


i have class modbus

public class modbus {   // init tcp   public native void init(string ip, int port, int id);   public native short[] read_registers(int slaveid, int addr, int len); 

the id in init method, printed stdout if execute read_registers

if do

modbus mb1 = new modbus("192.168.0.250", 502, 1); modbus mb2 = new modbus("192.168.0.250", 502, 2); 

and then

mb1.read_registers(....); mb2.read_registers(....); 

in both cases prints 2 stdout. seems second instance overwrites first. how can use multiple instances of jni-wrapper?

edit:

here c code i'm using, goal use multiple connections samre or different modbus devices

#include "eu_company_lib_comm_modbus_modbus.h" #include <modbus/modbus-rtu.h> #include <modbus/modbus.h> #include <errno.h> modbus_t *mb; int readerror; int writeerror; int errnr; const char* errstr; int c_id; jniexport void jnicall  java_eu_company_lib_comm_modbus_modbus_init__ljava_lang_string_2i(     jnienv * env, jobject obj, jstring ip, jint port, jint id) {  const char* c_ip = (*env)->getstringutfchars(env, ip, 0); mb = modbus_new_tcp(c_ip, port); c_id=id; if (mb == null) {     fprintf(stderr, "unable allocate libmodbus context\n");     fflush(stderr);     return; } modbus_set_debug(mb, 1);  if (modbus_connect(mb) == -1) {     fprintf(stderr, "connection failed: %s\n",     modbus_strerror(errno));     fflush(stderr);     modbus_free(mb);     return; }  (*env)->releasestringutfchars(env, ip, c_ip); } jniexport jshortarray jnicall java_eu_company_lib_comm_modbus_modbus_read_1registers(     jnienv * env, jobject obj, jint slave_id, jint address, jint len) {  jshortarray result = (*env)->newshortarray(env, len); uint16_t tab_reg[len]; printf("%d\n",c_id); fflush(stdout); readerror = modbus_read_registers(mb, address, len, tab_reg);  errnr = errno; errstr = modbus_strerror(errno);  (*env)->setshortarrayregion(env, result, 0, len, tab_reg);  return result; } 

your main problem in jni part using global variables. global variables. therefore variables not related in way modbus instance.

from point of view should move code jni java implementation of modbus , use local fields storing persistent values need on java jni level:

public class modbus {

private final string ip; private final int port; private final int id;  public modbus(string ip, int port, ind id) {      this.ip = ip;      this.port = port;      this.id = id; }      // init tcp public native void init(string ip, int port, int id); public native short[] read_registers(int slaveid, int addr, int len); 

}

on jni side can access al java fields retrieving values using jni methods getobjectclass(..), getfieldid(..) , getobjectfield(..).

search examples find lot of them c , c++.

alternatively can make native methods private , add wrapper method on every calls delivers needed values jni part:

public void init() {     init(ip, port, id); }  private native void init(string ip, int port, int id); 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -