2013-08-20 3 views
2

TCP/Modbus를 통해 PLC에 연결을 시도하는 스마트 폰용 프로그램을 만들고 있는데 Jamod 라이브러리를 사용하고 있습니다. 연결된,하지만 안드로이드에 변화가 ...., 누군가 나를 도울 수 있습니까?.안드로이드 용 TCP/Modbus

나는 인터넷을 사용하는 응용 프로그램에 대한 허가도 받았고 왜 연결하지 않을지 모르겠다. 필사적이다.

  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 

:

 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"); 
     } 

    } 

그것은 나에게 오류를 제공합니다.

+0

도와 드릴까요? – David

답변

3

API 레벨 11 이상에서 실행하는 경우 주 스레드에서 네트워크 작업을 차단하는 것과 관련 될 수 있습니다 (onResume은 주 스레드에서 실행되어 UI 업데이트를 차단 함). 이 답변이 도움이 될해야합니다 https://stackoverflow.com/a/6343299/1045199

출처 : http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

P.S. 당신은 정말로 다른 사람이 실제 오류인지 알 수 있도록 예외 stacktrace를 게시해야합니다.

+1

다른 사람들이 실제 실수인지 알 수 있도록 예외 스택 추적을 만드는 방법. 나는이 일에 새로운 사람이다. 죄송합니다 – Isma9

+0

예외 개체를 Log.d 호출에 추가하면 logcat에 스택 트레이스가 출력됩니다 : 'Log.d ("MODBUS", "연결 오류", e); ' –

+0

오류가 발생했습니다. 많이 – Isma9

0

기본 TCP 연결을 설정하지 못했기 때문에 Modbus 관련 버그처럼 보이지 않습니다.

아마도 ConnectivityManager에서 NetworkInfo를 가져 와서 장치가 연결되어 있는지 확인하십시오.

+0

다른 프로그램 Modbus/Tcp를 전화로 다운로드하여 아무런 문제없이 장치에 연결하면 장치가 연결됩니다. – Isma9