title: TCP/Modbus for android, (Android,Jamod)
body: <p>I'm making a program for smartphones which attempts to connect to a PLC via TCP / Modbus and I am using the library Jamod, this same program I put in java and if connected, but no change in android .... , Someone can help me?.</p>
  
  <p>I have also given permission for the application to use the internet and do not know why not connect, please I'm desperate!</p>
  
  <pre><code>        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");
          }
  
      }
  </code></pre>
  
  <p>it gives me error:</p>
  
  <pre><code>         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.&lt;init&gt;(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
  </code></pre>
  
  <p>.</p>