TCP / Modbus for android,(Android,Jamod)

时间:2013-08-20 09:46:50

标签: android tcp modbus

我正在制作一个智能手机程序试图通过TCP / Modbus连接到PLC,我正在使用库Jamod,这个程序我放在java中,如果连接,但没有改变android .... ,有人可以帮助我吗?。

我还允许申请使用互联网,不知道为什么不连接,请我绝望!

        package com.JR.scada;

import java.net.InetAddress;

import net.wimpi.modbus.Modbus;
import net.wimpi.modbus.io.ModbusTCPTransaction;
import net.wimpi.modbus.msg.ReadInputDiscretesRequest;
import net.wimpi.modbus.msg.ReadInputDiscretesResponse;
import net.wimpi.modbus.net.TCPMasterConnection;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;




public class Main extends Activity implements OnClickListener{
    TextView text, depurar;
    EditText IP;
    Button boton;
    //Thread comm = new Thread(this);
    int i=0;
    /*private final int PUERTO=502;
    private Socket socket = null;
    private OutputStream output = null;
    private BufferedInputStream input = null;
    private byte buffer[] = new byte[261];*/

    TCPMasterConnection con = null;     //the TCP connection
    ModbusTCPTransaction trans = null;  //the Modbus transaction

    InetAddress addr = null;        //direccion del esclavo
    int port = Modbus.DEFAULT_PORT;//puerto por defecto 502 


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView) findViewById(R.id.lblRegistro);
        IP = (EditText) findViewById(R.id.txtIp);
        depurar = (TextView) findViewById(R.id.txtdepurar);
        boton = (Button)findViewById(R.id.btnVerRegistro);
        boton.setOnClickListener(this);



    }

            @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }   
    @Override
    protected void onStop() {
        super.onStop();
        //Close the TCP connection
        con.close();
    }
    @Override
    /*public void run() {*/
    protected void onResume() {
        super.onResume();

        try {
            text.setText("Entro en el try");
             //IP address;
            addr = InetAddress.getByName("212.170.50.238");
            // Open the connection
            con = new TCPMasterConnection(addr);
            con.setPort(port);
            con.connect ();
            if(con.isConnected()){
                depurar.setText("conecta");
            }


        } catch (Exception e) {
            Log.d("MODBUS","connection error");
           depurar.setText("no conecta");
        }

    }

它给了我错误:

         08-20 10:25:39.299: D/MODBUS(1238): connection error
08-20 10:25:39.299: D/MODBUS(1238): android.os.NetworkOnMainThreadException
08-20 10:25:39.299: D/MODBUS(1238):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
08-20 10:25:39.299: D/MODBUS(1238):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
08-20 10:25:39.299: D/MODBUS(1238):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
08-20 10:25:39.299: D/MODBUS(1238):     at libcore.io.IoBridge.connect(IoBridge.java:112)
08-20 10:25:39.299: D/MODBUS(1238):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
08-20 10:25:39.299: D/MODBUS(1238):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
08-20 10:25:39.299: D/MODBUS(1238):     at java.net.Socket.startupSocket(Socket.java:566)
08-20 10:25:39.299: D/MODBUS(1238):     at java.net.Socket.<init>(Socket.java:225)
08-20 10:25:39.299: D/MODBUS(1238):     at net.wimpi.modbus.net.TCPMasterConnection.connect(TCPMasterConnection.java:65)
08-20 10:25:39.299: D/MODBUS(1238):     at com.JR.scada.Main.onResume(Main.java:96)
08-20 10:25:39.299: D/MODBUS(1238):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)
08-20 10:25:39.299: D/MODBUS(1238):     at android.app.Activity.performResume(Activity.java:5182)
08-20 10:25:39.299: D/MODBUS(1238):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)
08-20 10:25:39.299: D/MODBUS(1238):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
08-20 10:25:39.299: D/MODBUS(1238):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235)
08-20 10:25:39.299: D/MODBUS(1238):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-20 10:25:39.299: D/MODBUS(1238):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-20 10:25:39.299: D/MODBUS(1238):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-20 10:25:39.299: D/MODBUS(1238):     at android.os.Looper.loop(Looper.java:137)
08-20 10:25:39.299: D/MODBUS(1238):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-20 10:25:39.299: D/MODBUS(1238):     at java.lang.reflect.Method.invokeNative(Native Method)
08-20 10:25:39.299: D/MODBUS(1238):     at java.lang.reflect.Method.invoke(Method.java:511)
08-20 10:25:39.299: D/MODBUS(1238):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-20 10:25:39.299: D/MODBUS(1238):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-20 10:25:39.299: D/MODBUS(1238):     at dalvik.system.NativeStart.main(Native Method)
08-20 10:25:40.140: D/dalvikvm(1238): GC_CONCURRENT freed 117K, 9% free 2653K/2892K, paused 15ms+23ms, total 236ms
08-20 10:25:40.449: D/gralloc_goldfish(1238): Emulator without GPU emulation detected

2 个答案:

答案 0 :(得分:3)

如果您在API级别11或更高级别上执行此操作,则可能与在主线程上执行阻塞网络操作有关(onResume在主线程上执行,从而阻止UI更新)。这个答案应该有所帮助: https://stackoverflow.com/a/6343299/1045199

来源:http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

P.S。你真的应该发布异常堆栈跟踪让其他人知道实际错误是什么。

答案 1 :(得分:0)

这看起来不像是Modbus特定的错误,因为您无法建立基本的TCP连接。

也许首先从NetworkctivityManager中取出NetworkInfo,并检查设备是否已连接?