Search in sources :

Example 1 with ModbusSerialMaster

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

the class SerialFacadeTest method main.

public static void main(String[] args) {
    int inChar = -1;
    int result = 0;
    boolean finished = false;
    int slaveId = 88;
    String portname = null;
    ModbusSerialMaster msm = null;
    try {
        // 1. Setup the parameters
        if (args.length < 2) {
            printUsage();
            System.exit(1);
        } else {
            try {
                portname = args[0];
                slaveId = Integer.parseInt(args[1]);
            } catch (Exception ex) {
                ex.printStackTrace();
                printUsage();
                System.exit(1);
            }
        }
        System.out.println(" sending test messages to slave: " + slaveId);
        System.out.println("net.wimpi.modbus.debug set to: " + System.getProperty("net.wimpi.modbus.debug"));
        System.out.println("Hit enter to start and <s enter> to terminate the test.");
        inChar = System.in.read();
        if ((inChar == 's') || (inChar == 'S')) {
            System.out.println("Exiting");
            System.exit(0);
        }
        // 2. Setup serial parameters
        SerialParameters params = new SerialParameters();
        params.setPortName(portname);
        params.setBaudRate(38400);
        params.setDatabits(8);
        params.setParity("None");
        params.setStopbits(1);
        params.setEncoding("rtu");
        params.setEcho(true);
        if (Modbus.debug) {
            System.out.println("Encoding [" + params.getEncoding() + "]");
        }
        // 3. Create the master facade
        msm = new ModbusSerialMaster(params);
        msm.connect();
        do {
            if (msm.writeCoil(slaveId, 4, true) == true) {
                System.out.println("Set output 5 to true");
            } else {
                System.err.println("Error setting slave " + slaveId + " output 5");
            }
            BitVector coils = msm.readCoils(slaveId, 0, 8);
            if (coils != null) {
                System.out.print("Coils:");
                for (int i = 0; i < coils.size(); i++) {
                    System.out.print(" " + i + ": " + coils.getBit(i));
                }
                System.out.println();
                try {
                    msm.writeMultipleCoils(slaveId, 0, coils);
                } catch (ModbusException ex) {
                    System.out.println("Error writing coils: " + result);
                }
            } else {
                System.out.println("Outputs: null");
                msm.disconnect();
                System.exit(-1);
            }
            BitVector digInp = msm.readInputDiscretes(slaveId, 0, 8);
            if (digInp != null) {
                System.out.print("Digital Inputs:");
                for (int i = 0; i < digInp.size(); i++) {
                    System.out.print(" " + i + ": " + digInp.getBit(i));
                }
                System.out.println();
                System.out.println("Inputs: " + ModbusUtil.toHex(digInp.getBytes()));
            } else {
                System.out.println("Inputs: null");
                msm.disconnect();
                System.exit(-1);
            }
            InputRegister[] ai = null;
            for (int i = 1000; i < 1010; i++) {
                ai = msm.readInputRegisters(slaveId, i, 1);
                if (ai != null) {
                    System.out.print("Tag " + i + ": ");
                    for (int n = 0; n < ai.length; n++) {
                        System.out.print(" " + ai[n].getValue());
                    }
                    System.out.println();
                } else {
                    System.out.println("Tag: " + i + " null");
                    msm.disconnect();
                    System.exit(-1);
                }
            }
            Register[] regs = null;
            for (int i = 1000; i < 1005; i++) {
                regs = msm.readMultipleRegisters(slaveId, i, 1);
                if (regs != null) {
                    System.out.print("RWRegisters " + i + " length: " + regs.length);
                    for (int n = 0; n < regs.length; n++) {
                        System.out.print(" " + regs[n].getValue());
                    }
                    System.out.println();
                } else {
                    System.out.println("RWRegisters " + i + ": null");
                    msm.disconnect();
                    System.exit(-1);
                }
            }
            regs = msm.readMultipleRegisters(slaveId, 0, 10);
            System.out.println("Registers: ");
            if (regs != null) {
                System.out.print("regs :");
                for (int n = 0; n < regs.length; n++) {
                    System.out.print("  " + n + "= " + regs[n]);
                }
                System.out.println();
            } else {
                System.out.println("Registers: null");
                msm.disconnect();
                System.exit(-1);
            }
            while (System.in.available() > 0) {
                inChar = System.in.read();
                if ((inChar == 's') || (inChar == 'S')) {
                    finished = true;
                }
            }
        } while (!finished);
    } catch (Exception e) {
        System.err.println("SerialFacadeTest driver: " + e);
        e.printStackTrace();
    }
    msm.disconnect();
}
Also used : BitVector(net.wimpi.modbus.util.BitVector) InputRegister(net.wimpi.modbus.procimg.InputRegister) InputRegister(net.wimpi.modbus.procimg.InputRegister) Register(net.wimpi.modbus.procimg.Register) ModbusSerialMaster(net.wimpi.modbus.facade.ModbusSerialMaster) ModbusException(net.wimpi.modbus.ModbusException) SerialParameters(net.wimpi.modbus.util.SerialParameters) ModbusException(net.wimpi.modbus.ModbusException)

Aggregations

ModbusException (net.wimpi.modbus.ModbusException)1 ModbusSerialMaster (net.wimpi.modbus.facade.ModbusSerialMaster)1 InputRegister (net.wimpi.modbus.procimg.InputRegister)1 Register (net.wimpi.modbus.procimg.Register)1 BitVector (net.wimpi.modbus.util.BitVector)1 SerialParameters (net.wimpi.modbus.util.SerialParameters)1