2014-02-14 257 views
0

我寫了下面的程序來理解如何使用jamod來訪問寄存器。首先,我使用「Modbus從站」模擬虛擬TCP-MODBUS Holding寄存器。我用我的程序與jamod庫讀取我剛剛製作的保存寄存器。jamod的Modbus/TCP

但我得到以下錯誤:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
     MAX_IP_MESSAGE_LENGTH cannot be resolved or is not a field 

     at net.wimpi.modbus.io.ModbusTCPTransport.prepareStreams(ModbusTCPTransport.java:223) 
     at net.wimpi.modbus.io.ModbusTCPTransport.setSocket(ModbusTCPTransport.java:79) 
     at net.wimpi.modbus.io.ModbusTCPTransport.<init>(ModbusTCPTransport.java:59) 
     at net.wimpi.modbus.net.TCPMasterConnection.prepareTransport(TCPMasterConnection.java:104) 
     at net.wimpi.modbus.net.TCPMasterConnection.connect(TCPMasterConnection.java:67) 
     at test_modbus.main(test_modbus.java:36) 

這裏是我的程序

import java.io.*; 
import java.lang.*; 
import java.net.InetAddress; 
import net.wimpi.modbus.Modbus; 
import net.wimpi.modbus.io.ModbusTCPTransaction; 
import net.wimpi.modbus.msg.WriteCoilRequest; 
import net.wimpi.modbus.msg.ReadInputRegistersRequest; 
import net.wimpi.modbus.msg.ReadInputRegistersResponse ; 
import net.wimpi.modbus.net.TCPMasterConnection; 

public class test_modbus { 

    public static void main(String args[]) { 
     try { 
      /* The important instances of the class */ 
      TCPMasterConnection con = null; // the connection 
      ModbusTCPTransaction trans = null; // the transaction 
      ReadInputRegistersRequest rreq = null; // the read request 
      ReadInputRegistersResponse rres = null; // the read response 
      WriteCoilRequest req = null; // the write request 


      /* Variables for storing the parameters */ 
      InetAddress addr = null; // the slave's address 
      int port = 502; // the default port 


      // 1. Setup the parameters 
      addr = InetAddress.getByName("127.0.0.1"); // ** The address 
                  // assigned to the 
                  // module ** 

      // 2. Open the connection 
      con = new TCPMasterConnection(addr); 
      con.setPort(port); 
      con.connect(); 
      System.out.println("--- Message: Line:36 success --- "); 
      // ~~~~~~~~~~~~~~~~~~~~ The faulty Read Request ~~~~~~~~~~~~~~~~~~~~ 
      // 3r. Prepare the READ request 
      int k = 4000; 
      rreq = new ReadInputRegistersRequest(k, 2); // Reading 8 bytes (of 
                 // what??) 

      // 4r. Prepare the READ transaction 
      trans = new ModbusTCPTransaction(con); 
      trans.setRequest(rreq); 
      System.out.println("--- Message: Line:46 success --- "); 
      // 5r. Execute the READ transaction 
      trans.execute(); 
      rres = (ReadInputRegistersResponse) trans.getResponse(); 
      System.out.println("Hex Value of register " + "= " 
        + rres.getHexMessage()); 

      // ~~~~~~~~~~~~~~~~~~~~ The functional Write Request 
      // ~~~~~~~~~~~~~~~~~~~~ 
      // 3w. Prepare the request 
      //req = new WriteCoilRequest(coil, true); // Switching ON the "DO 1" 
                // (= address 17) 

      // 4w. Prepare the transaction 
      trans = new ModbusTCPTransaction(con); 
      trans.setRequest(req); 

      // 5w. Execute the transaction repeat times 
      trans.execute(); 

      // 6. Close the connection 
      con.close(); 

     } catch (Exception ex) { 
      System.out.println("Error"); 
      ex.printStackTrace(); 
     } 
    } 
} 

感謝。

回答

1

Exception in thread "main" java.lang.Error: Unresolved compilation problem: MAX_IP_MESSAGE_LENGTH cannot be resolved or is not a field

意味着編譯器找不到(常數)值MAX_IP_MESSAGE_LENGTH

在net.wimpi.modbus.io.ModbusTCPTransport.prepareStreams(ModbusTCPTransport.java:223)

意味着在線路ModbusTCPTransport.java MAX_IP_MESSAGE_LENGTH 223被引用。

該問題不在您的代碼中,而是在jamod庫的編譯中。你是否已經將jamod代碼複製到了你的項目中,或者你是否將jamod jar包含到了classpath中?如果第一個那麼這是你的問題,做後者(使用maven http://mvnrepository.com/artifact/net.wimpi/jamod/1.2)。

+0

謝謝!我找到了!問題是我的示例代碼基於jamod-1.0,但我使用jamod-1.2。萬分感激!現在它在我的設備上運行良好! – user1776434

相關問題