J2Mod - I/O exception - failed to read

馋奶兔 提交于 2020-06-09 02:14:42

问题


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);
}

来源:https://stackoverflow.com/questions/61852628/j2mod-i-o-exception-failed-to-read

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!