Android-студия Modbus RS485 последовательное подключение MacosX
Я попробовал rs485 соединение в Android. Я использовал библиотеку режимов jamod, android4j ...
Но все не работает.
Я пробую коды:
Первая попытка:
ModbusFactory factory = new ModbusFactory();
SerialParameters params = new SerialParameters();
params.setCommPortId(path_serial);
params.setBaudRate(Baud_Rate);
params.setDataBits(8);
params.setStopBits(1);
params.setParity(0);
ModbusMaster master = factory.createRtuMaster(params);
master.setTimeout(1000);
master.setRetries(0);
long start = System.currentTimeMillis();
try {
master.init();
} catch (ModbusInitException e) {
System.out.println( "Modbus Master Init Error: " + e.getMessage());
return;
}
try {
byte [] RIR,RHR,RDI,RCR;
int slaveId=1;
int startOffset=0;
int numberOfRegisters=10;
int numberOfBits=10;
ReadHoldingRegistersRequest reqRHR = new ReadHoldingRegistersRequest(slaveId, startOffset, numberOfRegisters);
ReadHoldingRegistersResponse resRHR = (ReadHoldingRegistersResponse) master.send(reqRHR);
RHR=resRHR.getData();
System.out.println("HoldingRegister :" + RHR);
//System.out.println("Reg. 1001 Value:" + master.getValue(7, RegisterRange.HOLDING_REGISTER, 1000, DataType.FOUR_BYTE_FLOAT_SWAPPED));
// more like the above until all required register values are read.
// ..
} catch (ModbusTransportException e) {
e.printStackTrace();
} finally {
master.destroy();
}
Не работает
Вторая попытка
SerialConnection con = null; // the connection
ModbusSerialTransaction trans = null; // the transaction
//ReadInputRegistersRequest req = null; // the request
ReadHoldingRegistersRequest req = null;
ReadHoldingRegistersResponse res = null;
//ReadInputRegistersResponse res = null; // the response
String portname = null; // the name of the serial port to be used
int unitid = 0; // the unit identifier we will be talking to
int ref = 0; // the reference, where to start reading from
int count = 0; // the count of IR's to read
int repeat = 1; // a loop for repeating the transaction
try {
portname = path_serial;
//System.setProperty("gnu.io.rxtx.SerialPorts", portname);
unitid = 2;
ref = 0;
count = 4;
} catch (Exception ex) {
ex.printStackTrace();
System.exit(1);
}
// 2. Set master identifier
// ModbusCoupler.createModbusCoupler(null);
ModbusCoupler.getReference().setUnitID(1);
// 3. Setup serial parameters
SerialParameters params = new SerialParameters();
params.setPortName(portname);
params.setBaudRate(Baud_Rate);
params.setDatabits(8);
params.setParity("None");
params.setStopbits(1);
params.setEncoding("rtu");
params.setEcho(false);
// 4. Open the connection
con = new SerialConnection(params);
try {
con.isOpen();
// con.open();
}catch (Exception ex) {
}
con.close();
еще попробуй...
String portname = "/dev/ttyS3";
int baud = 57600;
//check params
// open the serial communications port
SerialParameters params = new SerialParameters();
params.setPortName(portname);
params.setBaudRate(Baud_Rate);
params.setDatabits(8);
params.setParity("None");
params.setStopbits(1);
params.setEncoding(Modbus.SERIAL_ENCODING_RTU);
params.setEcho(false);
con = new SerialConnection(params);
try {
con.open();
} catch (Exception ex) {
}
все не работает:(
Где я могу ошибаться? RXTX dalvikvm Ошибка загрузки библиотеки в logcat. Причина: java.lang.UnsatisfiedLinkError: Не удалось загрузить rxtxSerial: findLibrary вернул ноль
Я не понимаю... Мне нужно использовать Eclipse?
Платформа: Android Studio Java: 1.7.0_79-b15
Манифест приложения: INTERNET ACCESS_WIFI_STATE ACCESS_NETWORK_STATE WRITE_SETTINGS
minSdkVersion = "8" targetSdkVersion = "18 Я получаю одну и ту же ошибку все:
03-24 07:37:55.635 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources
03-24 07:37:55.645 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.pruneResourceCache
03-24 07:37:55.695 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:37:55.695 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:37:55.695 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:37:55.695 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:37:55.695 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:37:55.695 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.app.TaskStackBuilder', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:37:55.705 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.app.TaskStackBuilder', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:37:55.715 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:37:55.725 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:37:55.725 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:37:55.725 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:37:55.725 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:37:55.725 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.transition.TransitionManager', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:37:55.735 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:37:55.745 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:37:55.745 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:37:55.755 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:37:55.755 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:37:55.755 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:37:55.755 4681-4681/com.example.jealian.modbustest E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.jealian.modbustest.MainActivity.access$super
03-24 07:38:28.915 4681-4681/com.example.jealian.modbustest E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at com.ghgande.j2mod.modbus.net.SerialConnection.open(SerialConnection.java:119)
at com.example.jealian.modbustest.MainActivity.onCreate(MainActivity.java:65)
at android.app.Activity.performCreate(Activity.java:4492)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.UnsatisfiedLinkError: Couldn't load rxtxSerial: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:365)
at java.lang.System.loadLibrary(System.java:535)
at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:83)
at com.ghgande.j2mod.modbus.net.SerialConnection.open(SerialConnection.java:119)
at com.example.jealian.modbustest.MainActivity.onCreate(MainActivity.java:65)
at android.app.Activity.performCreate(Activity.java:4492)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
1 ответ
Решение
RXTX не работает Android Studio на MacBook.
Используйте Windows PC и проблема решена.