Android Studio Modbus rs485串口连接MacosX

Android Studio Modbus rs485 serial connection MacosX

我在 android 中尝试了 rs485 连接。我使用模式库 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) {


        }

全部无效:(

哪里会出错? logcat 中的 RXTX dalvikvm 加载库错误 原因:java.lang.UnsatisfiedLinkError:无法加载 rxtxSerial:findLibrary 返回 null

我不明白...我需要使用 Eclipse 吗?

平台:Android工作室 Java: 1.7.0_79-b15

应用清单: 互联网 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.access0(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.access0(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) 

RXTX 在 macbook 上不工作 android studio。

使用 windows PC 并解决问题。