modbus4j icon indicating copy to clipboard operation
modbus4j copied to clipboard

"Illegal function" response when read/write coils

Open yanxin9210 opened this issue 6 years ago • 3 comments

It returns "Illegal function" when I read or write coils. Version: 3.0.5 Code:

    public static void main(String[] args) {
        IpParameters ipParameters = new IpParameters();
        ipParameters.setHost("192.168.31.200");
        ipParameters.setPort(502);

        ModbusFactory modbusFactory = new ModbusFactory();
        ModbusMaster master = modbusFactory.createTcpMaster(ipParameters, false);
        int slaveId = 1;
        try {
            master.init();
            readCoilTest(master, slaveId, 0, 1);
        } catch (ModbusInitException e) {
            e.printStackTrace();
        }
    }

    public static void readCoilTest(ModbusMaster master, int slaveId, int start, int len) {
        try {
            ReadCoilsRequest request = new ReadCoilsRequest(slaveId, start, len);
            ReadCoilsResponse response = (ReadCoilsResponse) master.send(request);

            if (response.isException()) {
                System.out.println("Exception response: message=" + response.getExceptionMessage());
            } else {
                System.out.println(Arrays.toString(response.getBooleanData()));
            }
        } catch (ModbusTransportException e) {
            e.printStackTrace();
        }
    }

Logs:

10:58:15.121 [main] DEBUG com.serotonin.modbus4j.ip.tcp.TcpMaster - Encap Request: 00 01 00 00 00 06 01 01 00 00 00 01 
10:58:15.137 [main] DEBUG com.serotonin.modbus4j.ip.tcp.TcpMaster - Sending on port: 502
10:58:15.219 [main] DEBUG com.serotonin.modbus4j.ip.tcp.TcpMaster - Response: 00 01 00 00 00 03 01 81 01 
Exception response: message=Illegal function

yanxin9210 avatar Aug 16 '19 03:08 yanxin9210

see your log,00 01 00 00 00 03 01 81 01 , 81 is error code,normally,it will return function code,so the 81 is Illegal function code ,please use the modscan32 check you slave device. which you company is?

ghost avatar Aug 18 '19 05:08 ghost

your slave modbus server may not support some functions.

Owisky avatar May 31 '21 07:05 Owisky

The reason is that you have no create Mbslave in modbus slave

kay07 avatar Jun 14 '23 03:06 kay07