2017-08-27 10 views
0

こんにちは私のmodbusデバイスとのシリアル接続にj2modを使用していますが、com.ghgande.j2mod.modbus.ModbusSlaveException例外があります。誰かが助けてくれる人がいます。以下は私のコードです。j2modで不正なデータアドレス例外

public static void main(String[] args) throws Exception { 
    SerialConnection con = null; 
    ModbusSerialTransaction trans = null; 
    ReadMultipleRegistersRequest req = null; 
    ReadMultipleRegistersResponse res = null; 

    String portname= "COM4"; //the name of the serial port to be used 
    int unitid = 1; //the unit identifier we will be talking to 
    int ref = 41799; //the reference, where to start reading from 
    int count = 100; //the count of IR's to read 
    int repeat = 1; //a loop for repeating the transaction 

    // ModbusCoupler.createModbusCoupler(null); 
    ModbusCoupler.getReference().setUnitID(1); 

    SerialParameters params = new SerialParameters(); 
    params.setPortName(portname); 
    params.setBaudRate(19200); 
    params.setDatabits(8); 
    params.setParity("None"); 
    params.setStopbits(1); 
    params.setEncoding(Modbus.SERIAL_ENCODING_RTU); 
    params.setEcho(false); 

    //4. Open the connection 
    con = new SerialConnection(params); 
    con.open(); 

    //5. Prepare a request 
    req = new ReadMultipleRegistersRequest(ref, count); 
    req.setUnitID(unitid); 
    req.setHeadless(); 

    //6. Prepare a transaction 
    trans = new ModbusSerialTransaction(con); 
    trans.setRequest(req); 

    int k = 0; 
    do { 
     trans.execute(); 
     res = (ReadMultipleRegistersResponse) trans.getResponse(); 
     for (int n = 0; n < res.getWordCount(); n++) { 
     System.out.println("Word " + n + "=" + res.getRegisterValue(n)); 
     } 
     k++; 
    } while (k < repeat); 

    //8. Close the connection 
    con.close(); 
} 

答えて

0

無効なデータアドレスは、存在しないレジスタにアクセスしようとしていることを示すModbus例外です。

コードでは、レジスタ41799-42798にアクセスしようとします。お使いのデバイスにはおそらくこれらのレジスタがありません。

+0

私の問題を解決しました。私の登録アドレスに問題がありました。とにかくありがとうございました –

関連する問題