2012-06-08 69 views
0

我已經編寫了以下程序,以瞭解如何使用JAMOD訪問線圈。不幸的是它拋出錯誤 -使用JAMOD讀取線圈

java.lang.IndexOutOfBoundsException 
at java.io.BufferedInputStream.read(Unknown Source) 
at java.io.DataInputStream.read(Unknown Source) 
at net.wimpi.modbus.io.ModbusTCPTransport.readResponse(ModbusTCPTransport.java:177) 
at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:193) 
at TempReader.main(TempReader.java:27) 
. x2 more times 
. 

java.io.EOFException 
at java.io.DataInputStream.readUnsignedByte(Unknown Source) 
at net.wimpi.modbus.io.BytesInputStream.readUnsignedByte(BytesInputStream.java:153) 
at net.wimpi.modbus.io.ModbusTCPTransport.readResponse(ModbusTCPTransport.java:182) 
at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:193) 
at TempReader.main(TempReader.java:30) 
net.wimpi.modbus.ModbusIOException: Executing transaction failed (tried 3 times) 
at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:197) 
at TempReader.main(TempReader.java:30) 

在程序行27是「trans.execute()」。我試圖讀取的線圈地址是0x7D1(2001)或通道0數據類型是2個字。 -

import java.net.*; 
import java.io.*; 
import net.wimpi.modbus.*; 
import net.wimpi.modbus.msg.*; 
import net.wimpi.modbus.io.*; 
import net.wimpi.modbus.net.*; 
import net.wimpi.modbus.util.*; 

public class TempReader { 

    public static void main(String Args[]){ 
     TCPMasterConnection con = null; 
     ModbusTCPTransaction trans = null; 
     ReadCoilsRequest rcreq=null; 
     ReadCoilsResponse rcres=null; 
     InetAddress addr = null; 
     int port = Modbus.DEFAULT_PORT; 
     byte byteArray[]={(byte)192, (byte)168, (byte)0, (byte)182}; 
     try { 
      addr=InetAddress.getByAddress(byteArray); 
      con=new TCPMasterConnection(addr); 
      con.setPort(502); 
      con.connect(); 
      rcreq=new ReadCoilsRequest(2001, 1); 
      trans=new ModbusTCPTransaction(con); 
      trans.setRequest(rcreq); 
      trans.execute(); 
      rcres=(ReadCoilsResponse)trans.getResponse(); 
      System.out.println("Response : "+rcres.getCoils().toString()); 
      con.close(); 
     } 
     catch(Exception e){ 
      con.close(); 
      e.printStackTrace(); 
     } 
    } 
} 

課程的誤差重複3次在ModbusTCPTransport類中設置(I檢查類源)之前即3次嘗試退出和程序退出。

我哪裏錯了?

+0

對不起,挖線索。你是如何讀取從線圈的?謝謝 – noidea

回答

1

我的示例Modbus TCP ReadMultipleRegistersRequest。

 public static void main(String[] args) { 
    try {   

     /**************************************/ 

     //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.1.202"; //Modbus Device     

     InetAddress addr = InetAddress.getByName(astr); 
     TCPMasterConnection con = new TCPMasterConnection(addr); //the connection 
     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(); 

     System.out.println("Connected to= "+ astr + con.isConnected() + "/Start Register " + Integer.toHexString(ref)); 
     count=1; 
    for (int k=0;k<count;k++){ 
     System.out.println("The value READ: " + Rres.getRegisterValue(k)); 
    }  

    /****************Close Connection**************/ 
     con.close(); 
     System.out.println("\nConnected = " + con.isConnected()); 
     System.exit(0);//edit Java bug error 


    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 




    }//main 

如果你想從嘗試這個例子:

import net.wimpi.modbus.net.*; 
import net.wimpi.modbus.procimg.*; 
import net.wimpi.modbus.ModbusCoupler; 

public class TCPSlaveTest { 

    public static void main(String[] args) { 
    try { 


/* The important instances and variables */ 
ModbusTCPListener listener = null; 
SimpleProcessImage spi = null; 
int port = Modbus.DEFAULT_PORT; 

    //1. Set port number from commandline parameter 
    if(args != null && args.length ==1) { 
    port = Integer.parseInt(args[0]); 
    } 

//2. Prepare a process image 
spi = new SimpleProcessImage(); 
spi.addDigitalOut(new SimpleDigitalOut(true)); 
spi.addDigitalOut(new SimpleDigitalOut(false)); 
spi.addDigitalIn(new SimpleDigitalIn(false)); 
spi.addDigitalIn(new SimpleDigitalIn(true)); 
spi.addDigitalIn(new SimpleDigitalIn(false)); 
spi.addDigitalIn(new SimpleDigitalIn(true)); 
spi.addRegister(new SimpleRegister(251)); 
spi.addInputRegister(new SimpleInputRegister(45)); 

//3. Set the image on the coupler 
ModbusCoupler.getReference().setProcessImage(spi); 
ModbusCoupler.getReference().setMaster(false); 
ModbusCoupler.getReference().setUnitID(15); 

//4. Create a listener with 3 threads in pool 
listener = new ModbusTCPListener(3); 
listener.setPort(port); 
listener.start(); 


    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
    }//main 

}//class TCPSlaveTest 
+0

你能幫我解決你的問題嗎,因爲我一直都有讀取超時的問題 – David

+0

好了我明白這不是奴隸,它是主人......你能給我一個工作的奴隸例子嗎? – David

+0

我不測試與jamod奴隸模式, 我覺得這個例子很好:http://jamod.sourceforge.net/kb/tcp_slave_howto.html – ISCI