Search in sources :

Example 1 with ModbusTCPListener

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

the class TestCaseSupport method startTCPServer.

private void startTCPServer() throws UnknownHostException, InterruptedException {
    // Serve single user at a time
    tcpListener = new ModbusTCPListener(SERVER_THREADS, localAddress(), tcpConnectionFactory);
    // Use any open port
    tcpListener.setPort(0);
    tcpListener.start();
    // Query server port. It seems to take time (probably due to thread starting)
    waitForTCPServerStartup();
    Assert.assertNotSame(-1, tcpModbusPort);
    Assert.assertNotSame(0, tcpModbusPort);
}
Also used : ModbusTCPListener(net.wimpi.modbus.net.ModbusTCPListener)

Example 2 with ModbusTCPListener

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

the class TCPSlaveTest method main.

public static void main(String[] args) {
    ModbusTCPListener 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 Slave (Server)");
        // 1. prepare a process image
        spi = new SimpleProcessImage();
        spi.addDigitalOut(new SimpleDigitalOut(true));
        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));
        // allow checking LSB/MSB order
        spi.addDigitalIn(new SimpleDigitalIn(true));
        spi.addDigitalIn(new SimpleDigitalIn(true));
        spi.addDigitalIn(new SimpleDigitalIn(true));
        spi.addDigitalIn(new SimpleDigitalIn(true));
        spi.addRegister(new SimpleRegister(251));
        spi.addInputRegister(new SimpleInputRegister(45));
        // 2. create the coupler holding the image
        ModbusCoupler.getReference().setProcessImage(spi);
        ModbusCoupler.getReference().setMaster(false);
        ModbusCoupler.getReference().setUnitID(15);
        // 3. create a listener with 3 threads in pool
        if (Modbus.debug) {
            System.out.println("Listening...");
        }
        listener = new ModbusTCPListener(3);
        listener.setPort(port);
        listener.start();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : SimpleDigitalIn(net.wimpi.modbus.procimg.SimpleDigitalIn) SimpleDigitalOut(net.wimpi.modbus.procimg.SimpleDigitalOut) ModbusTCPListener(net.wimpi.modbus.net.ModbusTCPListener) SimpleInputRegister(net.wimpi.modbus.procimg.SimpleInputRegister) SimpleProcessImage(net.wimpi.modbus.procimg.SimpleProcessImage) SimpleRegister(net.wimpi.modbus.procimg.SimpleRegister)

Aggregations

ModbusTCPListener (net.wimpi.modbus.net.ModbusTCPListener)2 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