Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
477 views
in Technique[技术] by (71.8m points)

java - J2Mod - I/O exception - failed to read

I am using for J2Mod java jar for my project. I am connected with the device through comport but, neither able to read nor write data into the device. Both time when I am trying to read or write data into the device getting the same exception. Whenever, Using QModMaster I am able to read data from the devices.

Connection Code -

SerialParameters parameters;
SerialConnection serialMaster;

try {
        parameters = new SerialParameters();

        parameters.setPortName((String) jComboBox4.getSelectedItem());
        //parameters.setCommPortId("/dev/ttyUSB1");
        parameters.setBaudRate(9600);
        parameters.setDatabits(8);
        parameters.setStopbits(1);
        parameters.setParity("none");
        // parameters.setEncoding("rtu");
        // parameters.setEcho(false);
        System.out.println("Connection Section" + parameters);
        serialMaster = new SerialConnection(parameters);

        serialMaster.open();
        System.out.println("Connection open :" + serialMaster.isOpen());

    } catch (Exception ex) {
        Logger.getLogger(TankIQ_Configurator.class.getName()).log(Level.SEVERE, null, ex);
    }

Code for reading data -

ReadMultipleRegistersRequest readMultipleRegistersRequest;
ReadMultipleRegistersResponse readMultipleRegistersResponse;
ModbusSerialTransaction trans;    

trans = new ModbusSerialTransaction(serialMaster);

Register[] register = {simpleRegister1, simpleRegister2};

int unitid = 1; //the unit identifier we will be talking to
int ref = 50; //the reference, where to start reading from
int count = 2; //the count of IR's to read
int repeat = 1; //a loop for repeating the transaction

//4. Open the connection
try{
    //5. Prepare a request
    readMultipleRegistersRequest = new ReadMultipleRegistersRequest(50, 2);
    readMultipleRegistersRequest.setUnitID(unitid);
    readMultipleRegistersRequest.setHeadless();

    //6. Prepare a transaction
    trans = new ModbusSerialTransaction(serialMaster);
    trans.setRequest(readMultipleRegistersRequest);

    int k = 0;
    do {
        trans.setTransDelayMS(1000);
        trans.execute();
        readMultipleRegistersResponse = (ReadMultipleRegistersResponse) trans.getResponse();
        for (int n = 0; n < readMultipleRegistersResponse.getWordCount(); n++) {
            System.out.println("Word " + n + "=" + readMultipleRegistersResponse.getRegisterValue(n));
        }
        k++;
    } while (k < repeat);
    serialMaster.close();//8. Close the connection
} catch (Exception ex) {
    Logger.getLogger(WriteMultipleRegistersRequest.class.getName()).log(Level.SEVERE, null, ex);
}

Output

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Thanks! all those who participated in this question.

Now, Let me explain, why I was getting this error.

Everything is fine with code except the unitid. unitid is nothing but slave address only. In my case slave address was 5.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...