2016-07-27 131 views
0

我通過以太網modbus tcp連接有問題。 我插入到AndroidManifest權限: 而我創建任務或連接和讀取Modbus。 我使用jamod庫。未連接modbus TCP

當我啓動應用程序給我一個連接失敗的錯誤。

這是我的代碼,我使用:

class Task implements Runnable { 

    @ 
    Override 

    public void run() { 

     try { 
      ReadMultipleRegistersResponse result = null; 

      //Read And Write Register Sample 
      int port = Modbus.DEFAULT_PORT; 
      String refe = "4000"; //HEX Address 
      int ref = Integer.parseInt(refe, 16); //Hex to int   
      int count = 98; //the number Address to read 
      int SlaveAddr = 1; 
      String astr = "192.168.0.18"; //Modbus Device     

      InetAddress addr = InetAddress.getByName(astr); 
      TCPMasterConnection con = new TCPMasterConnection(addr); 
      ModbusTCPTransaction trans = null; //the transaction 


      //1.Prepare the request 
      /************************************/ 
      ReadMultipleRegistersRequest Rreq = new ReadMultipleRegistersRequest(ref, count); 
      ReadMultipleRegistersResponse Rres = new ReadMultipleRegistersResponse(); 

      Rreq.setUnitID(SlaveAddr); //set Slave Address 
      Rres.setUnitID(SlaveAddr); //set Slave Address 

      //2. Open the connection 
      con.setPort(port); 

      con.connect(); 

      con.setTimeout(2500); 

      //3. Start Transaction 
      trans = new ModbusTCPTransaction(con); 
      trans.setRetries(5); 

      trans.setReconnecting(true); 
      trans.setRequest(Rreq); 
      trans.execute(); 

      /*Print Response*/ 
      Rres = (ReadMultipleRegistersResponse) trans.getResponse(); 
     } catch (ModbusSlaveException me) { 

     } catch (UnknownHostException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (java.lang.Exception e) {; 
      e.printStackTrace(); 
     } 

    } 

} 

我在哪裏做錯了嗎?

+0

那個ip地址是? – greenapps

回答

0

按照以下步驟

  1. 檢查你的奴隸正在

  2. 檢查你的奴隸的設備具有相同的網絡與主

  3. 檢查安全端口設置(默認502)