Search in sources :

Example 1 with ModbusUDPListener

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

the class UDPSlaveTest method main.

public static void main(String[] args) {
    ModbusUDPListener listener = null;
    SimpleProcessImage spi = null;
    int port = Modbus.DEFAULT_PORT;
    try {
        if (args != null && args.length == 1) {
            port = Integer.parseInt(args[0]);
        }
        System.out.println("jModbus Modbus/UDP Slave v0.1");
        // 1. Prepare a process image
        spi = new SimpleProcessImage();
        spi.addDigitalOut(new SimpleDigitalOut(true));
        spi.addDigitalIn(new SimpleDigitalIn(false));
        spi.addDigitalIn(new SimpleDigitalIn(true));
        spi.addDigitalIn(new SimpleDigitalIn(false));
        spi.addDigitalIn(new SimpleDigitalIn(true));
        spi.addRegister(new SimpleRegister(251));
        spi.addInputRegister(new SimpleInputRegister(45));
        ModbusCoupler.getReference().setProcessImage(spi);
        ModbusCoupler.getReference().setMaster(false);
        ModbusCoupler.getReference().setUnitID(15);
        // 2. Setup and start listener
        listener = new ModbusUDPListener();
        listener.setPort(port);
        listener.start();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : SimpleDigitalIn(net.wimpi.modbus.procimg.SimpleDigitalIn) SimpleDigitalOut(net.wimpi.modbus.procimg.SimpleDigitalOut) SimpleInputRegister(net.wimpi.modbus.procimg.SimpleInputRegister) ModbusUDPListener(net.wimpi.modbus.net.ModbusUDPListener) SimpleProcessImage(net.wimpi.modbus.procimg.SimpleProcessImage) SimpleRegister(net.wimpi.modbus.procimg.SimpleRegister)

Example 2 with ModbusUDPListener

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

the class TestCaseSupport method startUDPServer.

private void startUDPServer() throws UnknownHostException, InterruptedException {
    udpListener = new ModbusUDPListener(localAddress(), udpTerminalFactory);
    for (int portCandidate = 10000 + udpServerIndex.increment(); portCandidate < 20000; portCandidate++) {
        try {
            DatagramSocket socket = new DatagramSocket(portCandidate);
            socket.close();
            udpListener.setPort(portCandidate);
            break;
        } catch (SocketException e) {
            continue;
        }
    }
    udpListener.start();
    waitForUDPServerStartup();
    Assert.assertNotSame(-1, udpModbusPort);
    Assert.assertNotSame(0, udpModbusPort);
}
Also used : SocketException(java.net.SocketException) DatagramSocket(java.net.DatagramSocket) ModbusUDPListener(net.wimpi.modbus.net.ModbusUDPListener)

Aggregations

ModbusUDPListener (net.wimpi.modbus.net.ModbusUDPListener)2 DatagramSocket (java.net.DatagramSocket)1 SocketException (java.net.SocketException)1 SimpleDigitalIn (net.wimpi.modbus.procimg.SimpleDigitalIn)1 SimpleDigitalOut (net.wimpi.modbus.procimg.SimpleDigitalOut)1 SimpleInputRegister (net.wimpi.modbus.procimg.SimpleInputRegister)1 SimpleProcessImage (net.wimpi.modbus.procimg.SimpleProcessImage)1 SimpleRegister (net.wimpi.modbus.procimg.SimpleRegister)1