Search in sources :

Example 1 with ReadInputDiscretesRequest

use of net.wimpi.modbus.msg.ReadInputDiscretesRequest in project openhab1-addons by openhab.

the class ModbusSlave method update.

/**
     * Reads data from the connected device and updates items with the new data
     *
     * @param binding ModbusBindig that stores providers information
     */
public void update(ModbusBinding binding) {
    try {
        Object local = null;
        Exception localReadError = null;
        try {
            if (ModbusBindingProvider.TYPE_COIL.equals(getType())) {
                ModbusRequest request = new ReadCoilsRequest(getStart(), getLength());
                if (this instanceof ModbusSerialSlave) {
                    request.setHeadless();
                }
                ReadCoilsResponse response = (ReadCoilsResponse) getModbusData(request);
                local = response.getCoils();
            } else if (ModbusBindingProvider.TYPE_DISCRETE.equals(getType())) {
                ModbusRequest request = new ReadInputDiscretesRequest(getStart(), getLength());
                ReadInputDiscretesResponse response = (ReadInputDiscretesResponse) getModbusData(request);
                local = response.getDiscretes();
            } else if (ModbusBindingProvider.TYPE_HOLDING.equals(getType())) {
                ModbusRequest request = new ReadMultipleRegistersRequest(getStart(), getLength());
                ReadMultipleRegistersResponse response = (ReadMultipleRegistersResponse) getModbusData(request);
                local = response.getRegisters();
            } else if (ModbusBindingProvider.TYPE_INPUT.equals(getType())) {
                ModbusRequest request = new ReadInputRegistersRequest(getStart(), getLength());
                ReadInputRegistersResponse response = (ReadInputRegistersResponse) getModbusData(request);
                local = response.getRegisters();
            }
        } catch (ModbusException e) {
            // Logging already done in getModbusData
            localReadError = e;
        } catch (ModbusConnectionException e) {
            // Logging already done in getModbusData
            localReadError = e;
        } catch (ModbusUnexpectedTransactionIdException e) {
            // Logging already done in getModbusData
            localReadError = e;
        }
        if (storage == null) {
            storage = local;
            readError = localReadError;
        } else {
            synchronized (storage) {
                storage = local;
                readError = localReadError;
            }
        }
        Collection<String> items = binding.getItemNames();
        for (String item : items) {
            updateItem(binding, item);
        }
    } catch (Exception e) {
        logger.error("ModbusSlave ({}) error getting response from slave", name, e);
    }
}
Also used : ReadInputDiscretesResponse(net.wimpi.modbus.msg.ReadInputDiscretesResponse) ReadInputDiscretesRequest(net.wimpi.modbus.msg.ReadInputDiscretesRequest) ModbusRequest(net.wimpi.modbus.msg.ModbusRequest) ReadCoilsResponse(net.wimpi.modbus.msg.ReadCoilsResponse) ModbusException(net.wimpi.modbus.ModbusException) ReadInputRegistersResponse(net.wimpi.modbus.msg.ReadInputRegistersResponse) ModbusException(net.wimpi.modbus.ModbusException) ReadMultipleRegistersRequest(net.wimpi.modbus.msg.ReadMultipleRegistersRequest) ReadCoilsRequest(net.wimpi.modbus.msg.ReadCoilsRequest) ReadInputRegistersRequest(net.wimpi.modbus.msg.ReadInputRegistersRequest) ReadMultipleRegistersResponse(net.wimpi.modbus.msg.ReadMultipleRegistersResponse)

Example 2 with ReadInputDiscretesRequest

use of net.wimpi.modbus.msg.ReadInputDiscretesRequest in project openhab1-addons by openhab.

the class SerialDITest method main.

public static void main(String[] args) {
    SerialConnection con = null;
    ModbusSerialTransaction trans = null;
    ReadInputDiscretesRequest req = null;
    ReadInputDiscretesResponse res = null;
    String portname = null;
    int unitid = 0;
    int ref = 0;
    int count = 0;
    int repeat = 1;
    try {
        // 1. Setup the parameters
        if (args.length < 4) {
            printUsage();
            System.exit(1);
        } else {
            try {
                portname = args[0];
                unitid = Integer.parseInt(args[1]);
                ref = Integer.parseInt(args[2]);
                count = Integer.parseInt(args[3]);
                if (args.length == 5) {
                    repeat = Integer.parseInt(args[4]);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                printUsage();
                System.exit(1);
            }
        }
        // 2. Set slave identifier for master response parsing
        ModbusCoupler.getReference().setUnitID(unitid);
        System.out.println("net.wimpi.modbus.debug set to: " + System.getProperty("net.wimpi.modbus.debug"));
        // 3. Setup serial parameters
        SerialParameters params = new SerialParameters();
        params.setPortName(portname);
        params.setBaudRate(115200);
        params.setDatabits(7);
        params.setParity("None");
        params.setStopbits(2);
        // params.setEcho(true);
        if (Modbus.debug) {
            System.out.println("Encoding [" + params.getEncoding() + "]");
        }
        // 4. Open the connection
        con = new SerialConnection(params);
        con.open();
        // 5. Prepare a request
        req = new ReadInputDiscretesRequest(ref, count);
        req.setUnitID(unitid);
        req.setHeadless();
        if (Modbus.debug) {
            System.out.println("Request: " + req.getHexMessage());
        }
        // 6. Prepare the transaction
        trans = new ModbusSerialTransaction(con);
        trans.setRequest(req);
        // 7. Execute the transaction repeat times
        int k = 0;
        do {
            trans.execute();
            res = (ReadInputDiscretesResponse) trans.getResponse();
            if (Modbus.debug) {
                System.out.println("Response: " + res.getHexMessage());
            }
            BitVector inputs = res.getDiscretes();
            byte[] ret = new byte[inputs.size()];
            for (int i = 0; i < count; i++) {
                System.out.println("Bit " + i + " = " + inputs.getBit(i));
            }
            k++;
        } while (k < repeat);
        // 8. Close the connection
        con.close();
    } catch (Exception ex) {
        ex.printStackTrace();
        // Close the connection
        con.close();
    }
}
Also used : ReadInputDiscretesResponse(net.wimpi.modbus.msg.ReadInputDiscretesResponse) BitVector(net.wimpi.modbus.util.BitVector) ReadInputDiscretesRequest(net.wimpi.modbus.msg.ReadInputDiscretesRequest) SerialConnection(net.wimpi.modbus.net.SerialConnection) ModbusSerialTransaction(net.wimpi.modbus.io.ModbusSerialTransaction) SerialParameters(net.wimpi.modbus.util.SerialParameters)

Example 3 with ReadInputDiscretesRequest

use of net.wimpi.modbus.msg.ReadInputDiscretesRequest in project openhab1-addons by openhab.

the class UDPDITest method main.

public static void main(String[] args) {
    UDPMasterConnection conn = null;
    ModbusUDPTransaction trans = null;
    ReadInputDiscretesRequest req = null;
    ReadInputDiscretesResponse res = null;
    InetAddress addr = null;
    int ref = 0;
    int count = 0;
    int repeat = 1;
    int port = Modbus.DEFAULT_PORT;
    try {
        // 1. Setup the parameters
        if (args.length < 3) {
            printUsage();
            System.exit(1);
        } else {
            try {
                String astr = args[0];
                int idx = astr.indexOf(':');
                if (idx > 0) {
                    port = Integer.parseInt(astr.substring(idx + 1));
                    astr = astr.substring(0, idx);
                }
                addr = InetAddress.getByName(astr);
                ref = Integer.parseInt(args[1]);
                count = Integer.parseInt(args[2]);
                if (args.length == 4) {
                    repeat = Integer.parseInt(args[3]);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                printUsage();
                System.exit(1);
            }
        }
        // 2. Open the connection
        conn = new UDPMasterConnection(addr);
        conn.setPort(port);
        conn.connect();
        // 3. Prepare the request
        req = new ReadInputDiscretesRequest(ref, count);
        req.setUnitID(0);
        if (Modbus.debug) {
            System.out.println("Request: " + req.getHexMessage());
        }
        // 4. Prepare the transaction
        trans = new ModbusUDPTransaction(conn);
        trans.setRequest(req);
        // 5. Execute the transaction repeat times
        int k = 0;
        do {
            trans.execute();
            res = (ReadInputDiscretesResponse) trans.getResponse();
            if (Modbus.debug) {
                System.out.println("Response: " + res.getHexMessage());
            }
            System.out.println("Digital Inputs Status=" + res.getDiscretes().toString());
            k++;
        } while (k < repeat);
        // 6. Close the connection
        conn.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : ReadInputDiscretesResponse(net.wimpi.modbus.msg.ReadInputDiscretesResponse) ReadInputDiscretesRequest(net.wimpi.modbus.msg.ReadInputDiscretesRequest) ModbusUDPTransaction(net.wimpi.modbus.io.ModbusUDPTransaction) InetAddress(java.net.InetAddress) UDPMasterConnection(net.wimpi.modbus.net.UDPMasterConnection)

Example 4 with ReadInputDiscretesRequest

use of net.wimpi.modbus.msg.ReadInputDiscretesRequest in project openhab1-addons by openhab.

the class DIDOTest method main.

public static void main(String[] args) {
    InetAddress addr = null;
    TCPMasterConnection con = null;
    ModbusRequest di_req = null;
    WriteCoilRequest do_req = null;
    ModbusTCPTransaction di_trans = null;
    ModbusTCPTransaction do_trans = null;
    int di_ref = 0;
    int do_ref = 0;
    int port = Modbus.DEFAULT_PORT;
    try {
        // 1. Setup the parameters
        if (args.length < 3) {
            printUsage();
            System.exit(1);
        } else {
            try {
                String astr = args[0];
                int idx = astr.indexOf(':');
                if (idx > 0) {
                    port = Integer.parseInt(astr.substring(idx + 1));
                    astr = astr.substring(0, idx);
                }
                addr = InetAddress.getByName(astr);
                di_ref = Integer.parseInt(args[1]);
                do_ref = Integer.parseInt(args[2]);
            } catch (Exception ex) {
                ex.printStackTrace();
                printUsage();
                System.exit(1);
            }
        }
        // 2. Open the connection
        con = new TCPMasterConnection(addr);
        con.setPort(port);
        con.connect();
        if (Modbus.debug) {
            System.out.println("Connected to " + addr.toString() + ":" + con.getPort());
        }
        // 3. Prepare the requests
        di_req = new ReadInputDiscretesRequest(di_ref, 1);
        do_req = new WriteCoilRequest();
        do_req.setReference(do_ref);
        di_req.setUnitID(0);
        do_req.setUnitID(0);
        // 4. Prepare the transactions
        di_trans = new ModbusTCPTransaction(con);
        di_trans.setRequest(di_req);
        di_trans.setReconnecting(false);
        do_trans = new ModbusTCPTransaction(con);
        do_trans.setRequest(do_req);
        do_trans.setReconnecting(false);
        // 5. Holders for last states
        boolean last_out = false;
        boolean new_in = false;
        // 6. Execute the transactions repeatedly
        do {
            di_trans.execute();
            new_in = ((ReadInputDiscretesResponse) di_trans.getResponse()).getDiscreteStatus(0);
            // write only if differ
            if (new_in != last_out) {
                do_req.setCoil(new_in);
                do_trans.execute();
                last_out = new_in;
                if (Modbus.debug) {
                    System.out.println("Updated coil with state from DI.");
                }
            }
        } while (true);
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        // 7. Close the connection
        con.close();
    }
}
Also used : ModbusTCPTransaction(net.wimpi.modbus.io.ModbusTCPTransaction) TCPMasterConnection(net.wimpi.modbus.net.TCPMasterConnection) ReadInputDiscretesRequest(net.wimpi.modbus.msg.ReadInputDiscretesRequest) ModbusRequest(net.wimpi.modbus.msg.ModbusRequest) InetAddress(java.net.InetAddress) WriteCoilRequest(net.wimpi.modbus.msg.WriteCoilRequest)

Example 5 with ReadInputDiscretesRequest

use of net.wimpi.modbus.msg.ReadInputDiscretesRequest in project openhab1-addons by openhab.

the class DITest method main.

public static void main(String[] args) {
    TCPMasterConnection con = null;
    ModbusTCPTransaction trans = null;
    ReadInputDiscretesRequest req = null;
    ReadInputDiscretesResponse res = null;
    InetAddress addr = null;
    int ref = 0;
    int count = 0;
    int repeat = 1;
    int port = Modbus.DEFAULT_PORT;
    try {
        // 1. Setup the parameters
        if (args.length < 3) {
            printUsage();
            System.exit(1);
        } else {
            try {
                String astr = args[0];
                int idx = astr.indexOf(':');
                if (idx > 0) {
                    port = Integer.parseInt(astr.substring(idx + 1));
                    astr = astr.substring(0, idx);
                }
                addr = InetAddress.getByName(astr);
                ref = Integer.parseInt(args[1]);
                count = Integer.parseInt(args[2]);
                if (args.length == 4) {
                    repeat = Integer.parseInt(args[3]);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                printUsage();
                System.exit(1);
            }
        }
        // 2. Open the connection
        con = new TCPMasterConnection(addr);
        con.setPort(port);
        con.connect();
        if (Modbus.debug) {
            System.out.println("Connected to " + addr.toString() + ":" + con.getPort());
        }
        // 3. Prepare the request
        req = new ReadInputDiscretesRequest(ref, count);
        // ReadCoilsRequest req = new ReadCoilsRequest(ref, count);
        req.setUnitID(0);
        if (Modbus.debug) {
            System.out.println("Request: " + req.getHexMessage());
        }
        // 4. Prepare the transaction
        trans = new ModbusTCPTransaction(con);
        trans.setRequest(req);
        trans.setReconnecting(false);
        // 5. Execute the transaction repeat times
        int k = 0;
        do {
            trans.execute();
            res = (ReadInputDiscretesResponse) trans.getResponse();
            if (Modbus.debug) {
                System.out.println("Response: " + res.getHexMessage());
            }
            System.out.println("Digital Inputs Status=" + res.getDiscretes().toString());
            // System.out.println("Coils Status=" + res.getCoils().toString());
            k++;
        } while (k < repeat);
        // 6. Close the connection
        con.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : ModbusTCPTransaction(net.wimpi.modbus.io.ModbusTCPTransaction) ReadInputDiscretesResponse(net.wimpi.modbus.msg.ReadInputDiscretesResponse) TCPMasterConnection(net.wimpi.modbus.net.TCPMasterConnection) ReadInputDiscretesRequest(net.wimpi.modbus.msg.ReadInputDiscretesRequest) InetAddress(java.net.InetAddress)

Aggregations

ReadInputDiscretesRequest (net.wimpi.modbus.msg.ReadInputDiscretesRequest)5 ReadInputDiscretesResponse (net.wimpi.modbus.msg.ReadInputDiscretesResponse)4 InetAddress (java.net.InetAddress)3 ModbusTCPTransaction (net.wimpi.modbus.io.ModbusTCPTransaction)2 ModbusRequest (net.wimpi.modbus.msg.ModbusRequest)2 TCPMasterConnection (net.wimpi.modbus.net.TCPMasterConnection)2 ModbusException (net.wimpi.modbus.ModbusException)1 ModbusSerialTransaction (net.wimpi.modbus.io.ModbusSerialTransaction)1 ModbusUDPTransaction (net.wimpi.modbus.io.ModbusUDPTransaction)1 ReadCoilsRequest (net.wimpi.modbus.msg.ReadCoilsRequest)1 ReadCoilsResponse (net.wimpi.modbus.msg.ReadCoilsResponse)1 ReadInputRegistersRequest (net.wimpi.modbus.msg.ReadInputRegistersRequest)1 ReadInputRegistersResponse (net.wimpi.modbus.msg.ReadInputRegistersResponse)1 ReadMultipleRegistersRequest (net.wimpi.modbus.msg.ReadMultipleRegistersRequest)1 ReadMultipleRegistersResponse (net.wimpi.modbus.msg.ReadMultipleRegistersResponse)1 WriteCoilRequest (net.wimpi.modbus.msg.WriteCoilRequest)1 SerialConnection (net.wimpi.modbus.net.SerialConnection)1 UDPMasterConnection (net.wimpi.modbus.net.UDPMasterConnection)1 BitVector (net.wimpi.modbus.util.BitVector)1 SerialParameters (net.wimpi.modbus.util.SerialParameters)1