Search in sources :

Example 1 with ModbusSerialListener

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

the class SerialSlaveTest method main.

public static void main(String[] args) {
    ModbusSerialListener listener = null;
    SimpleProcessImage spi = new SimpleProcessImage();
    String portname = args[0];
    if (Modbus.debug) {
        System.out.println("jModbus ModbusSerial Slave");
    }
    try {
        // 1. Prepare a process image
        spi = new SimpleProcessImage();
        spi.addDigitalOut(new SimpleDigitalOut(true));
        spi.addDigitalOut(new SimpleDigitalOut(false));
        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));
        // 2. Create the coupler and set the slave identity
        ModbusCoupler.getReference().setProcessImage(spi);
        ModbusCoupler.getReference().setMaster(false);
        ModbusCoupler.getReference().setUnitID(2);
        // 3. Set up serial parameters
        SerialParameters params = new SerialParameters();
        params.setPortName(portname);
        params.setBaudRate(115200);
        params.setDatabits(8);
        params.setParity("None");
        params.setStopbits(1);
        params.setEncoding("ascii");
        params.setEcho(false);
        params.setReceiveTimeoutMillis(100);
        if (Modbus.debug) {
            System.out.println("Encoding [" + params.getEncoding() + "]");
        }
        // 4. Set up serial listener
        listener = new ModbusSerialListener(params);
        listener.setListening(true);
    } 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) SimpleProcessImage(net.wimpi.modbus.procimg.SimpleProcessImage) SimpleRegister(net.wimpi.modbus.procimg.SimpleRegister) ModbusSerialListener(net.wimpi.modbus.net.ModbusSerialListener) SerialParameters(net.wimpi.modbus.util.SerialParameters)

Aggregations

ModbusSerialListener (net.wimpi.modbus.net.ModbusSerialListener)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 SerialParameters (net.wimpi.modbus.util.SerialParameters)1