I want to improve my code. In this code I don't like my constructor, but without it I keep getting an error while trying to connect NullPointerException.package soft;import java.net.InetAddress;import java.net.UnknownHostException;import javax.swing.JOptionPane;import net.wimpi.modbus.ModbusException;import net.wimpi.modbus.ModbusIOException;import net.wimpi.modbus.ModbusSlaveException;import net.wimpi.modbus.io.ModbusSerialTransaction;import net.wimpi.modbus.io.ModbusTCPTransaction;import net.wimpi.modbus.msg.ReadCoilsRequest;import net.wimpi.modbus.msg.ReadCoilsResponse;import net.wimpi.modbus.msg.ReadInputDiscretesRequest;import net.wimpi.modbus.msg.ReadInputDiscretesResponse;import net.wimpi.modbus.msg.ReadInputRegistersRequest;import net.wimpi.modbus.msg.ReadInputRegistersResponse;import net.wimpi.modbus.msg.ReadMultipleRegistersRequest;import net.wimpi.modbus.msg.ReadMultipleRegistersResponse;import net.wimpi.modbus.msg.WriteMultipleRegistersRequest;import net.wimpi.modbus.msg.WriteMultipleRegistersResponse;import net.wimpi.modbus.msg.WriteSingleRegisterRequest;import net.wimpi.modbus.msg.WriteSingleRegisterResponse;import net.wimpi.modbus.net.SerialConnection;import net.wimpi.modbus.net.TCPMasterConnection;import net.wimpi.modbus.procimg.Register;import net.wimpi.modbus.procimg.SimpleRegister;import net.wimpi.modbus.util.SerialParameters;/** * This class realizes modbus protocol * communication over TCP and Serial Port and most of modbus functions. * * @author Tomas Ausvicas */public class BackEnd {    /** The tcp con. */    TCPMasterConnection tcpCon;    /** The serial con. */    SerialConnection serialCon;    /** The transaction tcp. */    ModbusTCPTransaction transactionTCP;    /** The transaction serial. */    ModbusSerialTransaction transactionSerial;    /** The params. */    SerialParameters params = new SerialParameters();    /** The rcreq. */    ReadCoilsRequest rcreq = new ReadCoilsRequest();    /** The rcres. */    ReadCoilsResponse rcres = new ReadCoilsResponse();    /** The rireq. */    ReadInputRegistersRequest rireq = new ReadInputRegistersRequest();    /** The rires. */    ReadInputRegistersResponse rires = new ReadInputRegistersResponse();    /** The ridreq. */    ReadInputDiscretesRequest ridreq = new ReadInputDiscretesRequest();    /** The ridres. */    ReadInputDiscretesResponse ridres = new ReadInputDiscretesResponse();    /** The write_sreq. */    WriteSingleRegisterRequest write_sreq = new WriteSingleRegisterRequest();    /** The write_sres. */    WriteSingleRegisterResponse write_sres = new WriteSingleRegisterResponse();    /** The read_mreq. */    ReadMultipleRegistersRequest read_mreq = new ReadMultipleRegistersRequest();    /** The read_mres. */    ReadMultipleRegistersResponse read_mres = new `ReadMultipleRegistersResponse();`    /** The write_mreq. */    WriteMultipleRegistersRequest write_mreq = new WriteMultipleRegistersRequest();    /** The write_mres. */    WriteMultipleRegistersResponse write_mres = new WriteMultipleRegistersResponse();    /** The register. */    SimpleRegister regL, regH, register;    /** The registers. */    Register[] registers;    /** The int16 result. */    private int[] int16Result;    /** The int32 result. */    private int[] int32Result;    /** The float result. */    private float[] floatResult;    /** The round float result. */    private float[] roundFloatResult;    /** The string16 result. */    private String[] string16Result;    /** The string32 result. */    private String[] string32Result;    /** The string float result. */    private String[] stringFloatResult;    /** The round string float result. */    private String[] roundStringFloatResult;    /** The string coil result. */    private String[] stringCoilResult;    /** The boolean coil result. */    private boolean[] booleanCoilResult;    /** The string discrete input result. */    private String[] stringDiscreteInputResult;    /** The boolean discrete input result. */    private boolean[] booleanDiscreteInputResult;    /** The string read bit result. */    private String[] stringReadBitResult;    /** The boolean read bit result. */    private boolean[] booleanReadBitResult;    /** The int analog input registers result. */    private int[] intAnalogInputRegistersResult;    /** The string analog input registers result. */    private String[] stringAnalogInputRegistersResult;    /** The to string. */    private String[] toString;    /**     * Default constructor with TCP and Serial Port objects created.     */    public BackEnd() {    try {        tcpCon = new TCPMasterConnection(InetAddress.getByName());    } catch (UnknownHostException e) {        System.out.println(Unknow Host);    }    params = new SerialParameters();    serialCon = new SerialConnection(params);    }    /**     * Connect over TCP.     *     * @param ipAddress the ip address     * @param port the port     */    public void connectTCP(String ipAddress, int port) {        try {            tcpCon = new TCPMasterConnection(InetAddress.getByName(ipAddress));        } catch (UnknownHostException e1) {            JOptionPane.showMessageDialog(null, veskite IP adres, Invalid input, JOptionPane.WARNING_MESSAGE);        }        tcpCon.setPort(port);        try {            tcpCon.connect();        } catch (Exception e) {            JOptionPane.showMessageDialog(null, Nepavyko prisijungti prie serverio. [TCP/IP], Connection error,                    JOptionPane.ERROR_MESSAGE);        }        transactionTCP = new ModbusTCPTransaction(tcpCon);    }    /**     * Set parameters for serial com.     *     * @param portName the port name     * @param baudRate the baud rate     * @param dataBits the data bits     * @param stopBits the stop bits     * @param parity the parity     * @param encoding the encoding     * @param echo the echo     */    public void connectSerial(String portName, int baudRate, int dataBits, int stopBits, String parity, String encoding,            boolean echo) {        SerialParameters params = new SerialParameters();        params.setPortName(portName);        params.setDatabits(dataBits);        params.setParity(parity);        params.setStopbits(stopBits);        params.setEncoding(encoding);        params.setEcho(echo);        params.setBaudRate(baudRate);        serialCon = new SerialConnection(params);        try {            serialCon.open();        } catch (Exception e) {            JOptionPane.showMessageDialog(null, Nepavyko prisijungti prie serverio. [Serial port], Connection error,                    JOptionPane.ERROR_MESSAGE);        }        transactionSerial = new ModbusSerialTransaction(serialCon);    }    /**     * Close TCP connection.     */    public void closeTCP() {        tcpCon.close();    }    /**     * Close Serial connection.     */    public void closeSerial() {        serialCon.close();    }    /**     * Check if TCP connected.     *     * @return boolean value     */    public boolean isConnectedTCP() {        return tcpCon.isConnected();    }    /**     * Check if Serial connected.     *     * @return boolean value     */    public boolean isOpenSerial() {        return serialCon.isOpen();    }    /**     * Writes single 16bit number to register (16 bit integer).     *     * @param unitID the unit id     * @param startAddress the start address     * @param dataToWrite the data to write     */    public void WriteSingle_16Bit(int unitID, int startAddress, int dataToWrite) {        SimpleRegister reg = new SimpleRegister(dataToWrite);        write_sreq.setReference(startAddress);        write_sreq.setUnitID(unitID);        write_sreq.setRegister(reg);        try {            transaction(transactionTCP, transactionSerial, write_sreq);        } catch (ModbusIOException e) {            System.out.println(Modbus IO Exception write 16);        } catch (ModbusSlaveException e) {            System.out.println(Modbus Slave Exception write 16);        } catch (ModbusException e) {            System.out.println(Modbus Exception write 16);        }    }    /**     * Writes double 16bit number to register (32 bit integer).     *     * @param unitID the unit id     * @param startAddress the start address     * @param dataToWrite the data to write     */    public void WriteSingle_32Bit(int unitID, int startAddress, int dataToWrite) {        int count = 2;        int HIGH = dataToWrite >> 16;        int LOW = dataToWrite & 0xFFFF;        regH = new SimpleRegister(HIGH);        regL = new SimpleRegister(LOW);        registers = new Register[count];        for (int i = 0, a = 1; i < registers.length; i += 2, a += 2) {            registers[i] = regL;            registers[a] = regH;        }        write_mreq.setReference(startAddress);        write_mreq.setUnitID(unitID);        write_mreq.setRegisters(registers);        try {            transaction(transactionTCP, transactionSerial, write_mreq);        } catch (ModbusIOException e) {            System.out.println(Modbus IO Exception write 32);        } catch (ModbusSlaveException e) {            System.out.println(Modbus Slave Exception write 32);        } catch (ModbusException e) {            System.out.println(Modbus Exception write 32);        }    }    /**     * Writes double 16bit number to register (32bit float).     *     * @param unitID the unit id     * @param startAddress the start address     * @param dataToWriteF the data to write f     */    public void WriteSingle_Float(int unitID, int startAddress, float dataToWriteF) {        int count = 2;        int number = Float.floatToIntBits(dataToWriteF);        int HIGH = number >> 16;        int LOW = number & 0xFFFF;        regH = new SimpleRegister(HIGH);        regL = new SimpleRegister(LOW);        registers = new Register[count];        for (int i = 1, a = 0; i < registers.length; i += 2, a += 2) {            registers[i] = regL;            registers[a] = regH;        }        write_mreq.setReference(startAddress);        write_mreq.setUnitID(unitID);        write_mreq.setRegisters(registers);        try {            transaction(transactionTCP, transactionSerial, write_mreq);        } catch (ModbusIOException e) {            System.out.println(Modbus IO Exception write float);        } catch (ModbusSlaveException e) {            System.out.println(Modbus Slave Exception write float);        } catch (ModbusException e) {            System.out.println(Modbus Exception write float);        }    }    /**     * Writes multiple 16bit numbers to chosen registers (16bit integer).     *     * @param unitID the unit id     * @param startAddress the start address     * @param dataToWrite the data to write     * @param count the count     */    public void WriteMultiple_16Bit(int unitID, int startAddress, int dataToWrite, int count) {        register = new SimpleRegister(dataToWrite);        registers = new Register[count];        for (int i = 0; i < registers.length; i++) {            registers[i] = register;        }        write_mreq.setReference(startAddress);        write_mreq.setUnitID(unitID);        write_mreq.setRegisters(registers);        try {            transaction(transactionTCP, transactionSerial, write_mreq);        } catch (ModbusIOException e) {            System.out.println(Modbus IO Exception mwrite 16);        } catch (ModbusSlaveException e) {            System.out.println(Modbus Slave Exception mwrite 16);        } catch (ModbusException e) {            System.out.println(Modbus Exception mwrite 16);        }    }    /**     * Writes multiple double 16bit numbers to chosen registers (32bit integer).     *     * @param unitID the unit id     * @param startAddress the start address     * @param dataToWrite the data to write     * @param count the count     */    public void WriteMultiple_32Bit(int unitID, int startAddress, int dataToWrite, int count) {        int HIGH = dataToWrite >> 16;        int LOW = dataToWrite & 0xFFFF;        regH = new SimpleRegister(HIGH);        regL = new SimpleRegister(LOW);        registers = new Register[count * 2];        for (int i = 0, a = 1; i < registers.length; i += 2, a += 2) {            registers[i] = regL;            registers[a] = regH;        }        write_mreq.setReference(startAddress);        write_mreq.setUnitID(unitID);        write_mreq.setRegisters(registers);        try {            transaction(transactionTCP, transactionSerial, write_mreq);        } catch (ModbusIOException e) {            System.out.println(Modbus IO Exception mwrite 32);        } catch (ModbusSlaveException e) {            System.out.println(Modbus Slave Exception mwrite 32);        } catch (ModbusException e) {            System.out.println(Modbus Exception mwrite 32);        }    }    /**     * Writes multiple double 16bit numbers to chosen registers (32bit float).     *     * @param unitID the unit id     * @param startAddress the start address     * @param dataToWriteF the data to write f     * @param count the count     */    public void WriteMultiple_Float(int unitID, int startAddress, float dataToWriteF, int count) {        int number = Float.floatToIntBits(dataToWriteF);        int HIGH = number >> 16;        int LOW = number & 0xFFFF;        regH = new SimpleRegister(HIGH);        regL = new SimpleRegister(LOW);        registers = new Register[count * 2];        for (int i = 1, a = 0; i < registers.length; i += 2, a += 2) {            registers[i] = regL;            registers[a] = regH;        }        write_mreq.setReference(startAddress);        write_mreq.setUnitID(unitID);        write_mreq.setRegisters(registers);        try {            transaction(transactionTCP, transactionSerial, write_mreq);        } catch (ModbusIOException e) {            System.out.println(Modbus IO Exception mwrite float);        } catch (ModbusSlaveException e) {            System.out.println(Modbus Slave Exception mwrite float);        } catch (ModbusException e) {            System.out.println(Modbus Exception mwrite float);        }    }    /**     * Reads multiple 16bit numbers from chosen registers (16bit integer).     *     * @param unitID the unit id     * @param startAddress the start address     * @param count the count     * @return multiple registers     */    public BackEnd ReadMultiple_16Bit(int unitID, int startAddress, int count) {        this.int16Result = new int[count];        this.string16Result = new String[count];        read_mreq.setUnitID(unitID);        read_mreq.setReference(startAddress);        read_mreq.setWordCount(count);        try {            transaction(transactionTCP, transactionSerial, read_mreq);        } catch (ModbusIOException e) {            System.out.println(Modbus IO Exception mread 16);        } catch (ModbusSlaveException e) {            System.out.println(Modbus Slave Exception mread 16);        } catch (ModbusException e) {            System.out.println(Modbus Exception mread 16);        }        for (int n = 0; n < read_mres.getWordCount(); n++) {            int16Result[n] = read_mres.getRegisterValue(n);            string16Result[n] = String.valueOf(int16Result[n]);        }        return this;    }    /**     * Reads multiple double 16bit numbers from chosen registers (32bit integer).     *     * @param unitID the unit id     * @param startAddress the start address     * @param count the count     * @return multiple registers     */    public BackEnd ReadMultiple_32Bit(int unitID, int startAddress, int count) {        this.int32Result = new int[count];        this.string32Result = new String[count];        int[] z = new int[count * 2];        int[] y = new int[count * 2];        int[] h = new int[count];        int[] l = new int[count];        read_mreq.setUnitID(unitID);        read_mreq.setReference(startAddress);        read_mreq.setWordCount(count * 2);        try {            transaction(transactionTCP, transactionSerial, read_mreq);        } catch (ModbusIOException e) {            System.out.println(Modbus IO Exception mread 32);        } catch (ModbusSlaveException e) {            System.out.println(Modbus Slave Exception mread 32);        } catch (ModbusException e) {            System.out.println(Modbus Exception mread 32);        }        for (int n = 0, i = 1; n < read_mres.getWordCount(); n += 2, i += 2) {            z[n] = read_mres.getRegisterValue(n);            y[i] = (read_mres.getRegisterValue(i) << 16);        }        for (int a = 0, b = 0; a < l.length; a++, b += 2) {            l[a] = z[b];        }        for (int a = 0, b = 1; a < h.length; a++, b += 2) {            h[a] = y[b];        }        for (int n = 0; n < int32Result.length; n++) {            int32Result[n] = h[n] | l[n];            string32Result[n] = String.valueOf(int32Result[n]);        }        return this;    }    /**     * Reads multiple double 16bit numbers from chosen registers (32 bit float).     *     * @param unitID the unit id     * @param startAddress the start address     * @param count the count     * @return multiple registers     */    public BackEnd ReadMultiple_Float(int unitID, int startAddress, int count) {        this.floatResult = new float[count];        this.stringFloatResult = new String[count];        this.roundFloatResult = new float[count];        this.roundStringFloatResult = new String[count];        int[] x = new int[count * 2];        int[] y = new int[count * 2];        int[] h = new int[count];        int[] l = new int[count];        read_mreq.setUnitID(unitID);        read_mreq.setReference(startAddress);        read_mreq.setWordCount(count * 2);        try {            transaction(transactionTCP, transactionSerial, read_mreq);        } catch (ModbusIOException e) {            System.out.println(Modbus IO Exception mread float);        } catch (ModbusSlaveException e) {            System.out.println(Modbus Slave Exception mread float);        } catch (ModbusException e) {            System.out.println(Modbus Exception mread float);        }        for (int n = 1, i = 0; n < read_mres.getWordCount(); n += 2, i += 2) {            x[n] = read_mres.getRegisterValue(n);            y[i] = (read_mres.getRegisterValue(i) << 16);        }        for (int a = 0, b = 1; a < l.length; a++, b += 2) {            l[a] = x[b];        }        for (int a = 0, b = 0; a < h.length; a++, b += 2) {            h[a] = y[b];        }        for (int n = 0; n < floatResult.length; n++) {            floatResult[n] = Float.intBitsToFloat(h[n] | l[n]);            stringFloatResult[n] = String.valueOf(floatResult[n]);            roundFloatResult[n] = (float) Math.round(floatResult[n] * 100) / 100;            roundStringFloatResult[n] = Float.toString(roundFloatResult[n]);        }        return this;    }    /**     * Reads multiple coils from chosen registers.     *     * @param unitID the unit id     * @param startAddress the start address     * @param count the count     * @return coil as boolean or String     */    public BackEnd ReadCoilStatus(int unitID, int startAddress, int count) {        this.booleanCoilResult = new boolean[count];        this.stringCoilResult = new String[count];        rcreq.setReference(startAddress);        rcreq.setBitCount(count);        rcreq.setUnitID(unitID);        try {            transaction(transactionTCP, transactionSerial, rcreq);        } catch (ModbusIOException e) {            e.printStackTrace();        } catch (ModbusSlaveException e) {            e.printStackTrace();        } catch (ModbusException e) {            e.printStackTrace();        }        for (int i = 0; i < count; i++) {            booleanCoilResult[i] = rcres.getCoilStatus(i);            stringCoilResult[i] = String.valueOf(booleanCoilResult[i]);        }        return this;    }    /**     * Reads multiple discrete inputs from chosen registers.     *     * @param unitID the unit id     * @param startAddress the start address     * @param count the count     * @return discrete input as boolean or String     */    public BackEnd ReadDiscreteInputStatus(int unitID, int startAddress, int count) {        this.booleanDiscreteInputResult = new boolean[count];        this.stringDiscreteInputResult = new String[count];        ridreq.setReference(startAddress);        ridreq.setBitCount(count);        ridreq.setUnitID(unitID);        try {            transaction(transactionTCP, transactionSerial, ridreq);        } catch (ModbusIOException e) {            e.printStackTrace();        } catch (ModbusSlaveException e) {            e.printStackTrace();        } catch (ModbusException e) {            e.printStackTrace();        }        for (int i = 0; i < count; i++) {            booleanDiscreteInputResult[i] = ridres.getDiscreteStatus(i);            stringDiscreteInputResult[i] = String.valueOf(booleanDiscreteInputResult[i]);        }        return this;    }    /**     * Reads multiple analog inputs from chosen registers.     *     * @param unitID the unit id     * @param startAddress the start address     * @param count the count     * @return analog input register value (int or String)     */    public BackEnd ReadAnalogInputRegisters(int unitID, int startAddress, int count) {        this.intAnalogInputRegistersResult = new int[count];        this.stringAnalogInputRegistersResult = new String[count];        rireq.setReference(startAddress);        rireq.setWordCount(count);        rireq.setUnitID(unitID);        try {            transaction(transactionTCP, transactionSerial, rireq);        } catch (ModbusIOException e) {            e.printStackTrace();        } catch (ModbusSlaveException e) {            e.printStackTrace();        } catch (ModbusException e) {            e.printStackTrace();        }        for (int i = 0; i < count; i++) {            intAnalogInputRegistersResult[i] = rires.getRegisterValue(i);            stringAnalogInputRegistersResult[i] = String.valueOf(intAnalogInputRegistersResult[i]);        }        return this;    }    /**     * Read bit from a register.     *     * @param unitID the unit id     * @param startAddress the start address     * @param position            D15..D0     * @param count the count     * @return bit as boolean or String     */    public BackEnd ReadBit(int unitID, int startAddress, int position, int count) {        this.stringReadBitResult = new String[count];        this.booleanReadBitResult = new boolean[count];        this.toString = new String[count];        // String[] toString = new String[count];        read_mreq.setUnitID(unitID);        read_mreq.setReference(startAddress);        read_mreq.setWordCount(count);        try {            transaction(transactionTCP, transactionSerial, read_mreq);        } catch (ModbusIOException e) {            System.out.println(Modbus IO Exception read bit);        } catch (ModbusSlaveException e) {            System.out.println(Modbus Slave Exception read bit);        } catch (ModbusException e) {            System.out.println(Modbus Exception read bit);        }        for (int n = 0; n < read_mres.getWordCount(); n++) {            try {                toString[n] = Integer.toBinaryString(0x10000 | read_mres.getRegisterValue(n)).substring(1);                if (toString[n].charAt(15 - position) == '0') {                    booleanReadBitResult[n] = false;                    stringReadBitResult[n] = String.valueOf(false);                } else if (toString[n].charAt(15 - position) == '1') {                    booleanReadBitResult[n] = true;                    stringReadBitResult[n] = String.valueOf(true);                }            } catch (IndexOutOfBoundsException e) {            }        }        return this;    }    /**     * Write bit to a register.     *     * @param unitID the unit id     * @param startAddress the start address     * @param position            D15..D0     * @param bitas            true=0, false=1     * @param count the count     */    public void WriteBit(int unitID, int startAddress, int position, boolean bitas, int count) {        String value = ;        if (bitas == true) {            value = 1;        } else if (bitas == false) {            value = 0;        }        int[] x = new int[count];        String[] toString = new String[count];        String[] Format16Bit = new String[count];        read_mreq.setUnitID(unitID);        read_mreq.setReference(startAddress);        read_mreq.setWordCount(count);        try {            transaction(transactionTCP, transactionSerial, read_mreq);        } catch (ModbusIOException e) {            System.out.println(Modbus IO Exception Readwrite bit);        } catch (ModbusSlaveException e) {            System.out.println(Modbus Slave Exception Readwrite bit);        } catch (ModbusException e) {            System.out.println(Modbus Exception Readwrite bit);        }        for (int n = 0; n < read_mres.getWordCount(); n++) {            toString[n] = Integer.toBinaryString(0x10000 | read_mres.getRegisterValue(n)).substring(1);        }        registers = new Register[count];        for (int n = 0; n < count; n++) {            Format16Bit[n] = toString[n].substring(0, 15 - position) + value + toString[n].substring(15 - position + 1);            x[n] = Integer.parseInt(Format16Bit[n], 2);            register = new SimpleRegister(x[n]);            registers[n] = register;        }        write_mreq.setReference(startAddress);        write_mreq.setRegisters(registers);        write_mreq.setUnitID(unitID);        try {            transaction(transactionTCP, transactionSerial, write_mreq);        } catch (ModbusIOException e) {            System.out.println(Modbus IO Exception Write bit);        } catch (ModbusSlaveException e) {            System.out.println(Modbus Slave Exception Write bit);        } catch (ModbusException e) {            System.out.println(Modbus Exception Write bit);        }    }    /**     * Overloading method for transaction over modbus protocol.     *     * @param transactionTCP the transaction tcp     * @param transactionSerial the transaction serial     * @param write_mreq the write_mreq     * @throws ModbusIOException the modbus io exception     * @throws ModbusSlaveException the modbus slave exception     * @throws ModbusException the modbus exception     */    public void transaction(ModbusTCPTransaction transactionTCP, ModbusSerialTransaction transactionSerial,            WriteMultipleRegistersRequest write_mreq) throws ModbusIOException, ModbusSlaveException, ModbusException {        try {            transactionTCP.setRequest(write_mreq);            transactionTCP.execute();            transactionTCP.getResponse();        } catch (Exception e) {            System.out.println([TCP] write_mreq error);        }        try {            transactionSerial.setRequest(write_mreq);            transactionSerial.execute();            transactionSerial.getResponse();        } catch (Exception e) {            System.out.println([Serial] write_mreq error);        }    }    /**     * Overloading method.     *     * @param transactionTCP the transaction tcp     * @param transactionSerial the transaction serial     * @param write_sreq the write_sreq     * @throws ModbusIOException the modbus io exception     * @throws ModbusSlaveException the modbus slave exception     * @throws ModbusException the modbus exception     */    public void transaction(ModbusTCPTransaction transactionTCP, ModbusSerialTransaction transactionSerial,            WriteSingleRegisterRequest write_sreq) throws ModbusIOException, ModbusSlaveException, ModbusException {        try {            transactionTCP.setRequest(write_sreq);            transactionTCP.execute();            transactionTCP.getResponse();        } catch (Exception e) {            System.out.println([TCP] write_sreq error);        }        try {            transactionSerial.setRequest(write_sreq);            transactionSerial.execute();            transactionSerial.getResponse();        } catch (Exception e) {            System.out.println([Serial] write_sreq error);        }    }    /**     * Overloading method.     *     * @param transactionTCP the transaction tcp     * @param transactionSerial the transaction serial     * @param read_mreq the read_mreq     * @throws ModbusIOException the modbus io exception     * @throws ModbusSlaveException the modbus slave exception     * @throws ModbusException the modbus exception     */    public void transaction(ModbusTCPTransaction transactionTCP, ModbusSerialTransaction transactionSerial,            ReadMultipleRegistersRequest read_mreq) throws ModbusIOException, ModbusSlaveException, ModbusException {        try {            transactionTCP.setRequest(read_mreq);            transactionTCP.execute();            read_mres = (ReadMultipleRegistersResponse) transactionTCP.getResponse();        } catch (Exception e) {            System.out.println([TCP] read_mreq error);        }        try {            transactionSerial.setRequest(read_mreq);            transactionSerial.execute();            read_mres = (ReadMultipleRegistersResponse) transactionSerial.getResponse();        } catch (Exception e) {            System.out.println([Serial] read_mreq error);        }    }    /**     * Overloading method.     *     * @param transactionTCP the transaction tcp     * @param transactionSerial the transaction serial     * @param rcreq the rcreq     * @throws ModbusIOException the modbus io exception     * @throws ModbusSlaveException the modbus slave exception     * @throws ModbusException the modbus exception     */    public void transaction(ModbusTCPTransaction transactionTCP, ModbusSerialTransaction transactionSerial,            ReadCoilsRequest rcreq) throws ModbusIOException, ModbusSlaveException, ModbusException {        try {            transactionTCP.setRequest(rcreq);            transactionTCP.execute();            rcres = (ReadCoilsResponse) transactionTCP.getResponse();        } catch (Exception e) {            System.out.println([TCP] rcreq error);        }        try {            transactionSerial.setRequest(rcreq);            transactionSerial.execute();            rcres = (ReadCoilsResponse) transactionSerial.getResponse();        } catch (Exception e) {            System.out.println([Serial] rcreq error);        }    }    /**     * Overloading method.     *     * @param transactionTCP the transaction tcp     * @param transactionSerial the transaction serial     * @param rireq the rireq     * @throws ModbusIOException the modbus io exception     * @throws ModbusSlaveException the modbus slave exception     * @throws ModbusException the modbus exception     */    public void transaction(ModbusTCPTransaction transactionTCP, ModbusSerialTransaction transactionSerial,            ReadInputRegistersRequest rireq) throws ModbusIOException, ModbusSlaveException, ModbusException {        try {            transactionTCP.setRequest(rireq);            transactionTCP.execute();            rires = (ReadInputRegistersResponse) transactionTCP.getResponse();        } catch (Exception e) {            System.out.println([TCP] rireq error);        }        try {            transactionSerial.setRequest(rireq);            transactionSerial.execute();            rires = (ReadInputRegistersResponse) transactionSerial.getResponse();        } catch (Exception e) {            System.out.println([Serial] rireq error);        }    }    /**     * Overloading method.     *     * @param transactionTCP the transaction tcp     * @param transactionSerial the transaction serial     * @param ridreq the ridreq     * @throws ModbusIOException the modbus io exception     * @throws ModbusSlaveException the modbus slave exception     * @throws ModbusException the modbus exception     */    public void transaction(ModbusTCPTransaction transactionTCP, ModbusSerialTransaction transactionSerial,            ReadInputDiscretesRequest ridreq) throws ModbusIOException, ModbusSlaveException, ModbusException {        try {            transactionTCP.setRequest(ridreq);            transactionTCP.execute();            ridres = (ReadInputDiscretesResponse) transactionTCP.getResponse();        } catch (Exception e) {            System.out.println([TCP] ridreq error);        }        try {            transactionSerial.setRequest(ridreq);            transactionSerial.execute();            ridres = (ReadInputDiscretesResponse) transactionSerial.getResponse();        } catch (Exception e) {            System.out.println([Serial] ridreq error);        }    }        /**     * Gets the 16 bit int.     *     * @return 16bit numbers as int array     */    public int[] get16BitInt() {        return int16Result;    }    /**     * Gets the 32 bit int.     *     * @return 32bit numbers as int array     */    public int[] get32BitInt() {        return int32Result;    }    /**     * Gets the float.     *     * @return 32bit float numbers as float array     */    public float[] getFloat() {        return floatResult;    }    /**     * Gets the round float.     *     * @return 32bit float numbers as rounded float array     */    public float[] getRoundFloat() {        return roundFloatResult;    }    /**     * Gets the 16 bit string.     *     * @return 16bit numbers as String array     */    public String[] get16BitString() {        return string16Result;    }    /**     * Gets the 32 bit string.     *     * @return 32bit numbers as String array     */    public String[] get32BitString() {        return string32Result;    }    /**     * Gets the float string.     *     * @return 32bit float numbers as String array     */    public String[] getFloatString() {        return stringFloatResult;    }    /**     * Gets the round float string.     *     * @return 32bit float numbers as rounded String array     */    public String[] getRoundFloatString() {        return roundStringFloatResult;    }    /**     * Gets the 16 bit at index.     *     * @param index the index     * @return 16bit number from int array at index     */    public int get16BitAtIndex(int index) {        return int16Result[index];    }    /**     * Gets the 32 bit at index.     *     * @param index the index     * @return 32bit number from int array at index     */    public int get32BitAtIndex(int index) {        return int32Result[index];    }    /**     * Gets the float at index.     *     * @param index the index     * @return 32bit foat number from float array at index     */    public float getFloatAtIndex(int index) {        return floatResult[index];    }    /**     * Gets the round float at index.     *     * @param index the index     * @return 32bit rounded float number from rounded float array at index     */    public float getRoundFloatAtIndex(int index) {        return roundFloatResult[index];    }    /**     * Gets the 16 bit string at index.     *     * @param index the index     * @return 16bit rounded float number from rounded String array at index     */    public String get16BitStringAtIndex(int index) {        return string16Result[index];    }    /**     * Gets the 32 bit string at index.     *     * @param index the index     * @return 32bit number from String array at index     */    public String get32BitStringAtIndex(int index) {        return string32Result[index];    }    /**     * Gets the float string at index.     *     * @param index the index     * @return 32bit float number from String array at index     */    public String getFloatStringAtIndex(int index) {        return stringFloatResult[index];    }    /**     * Gets the round float string at index.     *     * @param index the index     * @return 32bit rounded float number from rounded String array at index     */    public String getRoundFloatStringAtIndex(int index) {        return roundStringFloatResult[index];    }    /**     * Gets the coil boolean at index.     *     * @param index the index     * @return coil status from boolean array at index     */    public Boolean getCoilBooleanAtIndex(int index) {        return booleanCoilResult[index];    }    /**     * Gets the coil string at index.     *     * @param index the index     * @return coil status from String array at index     */    public String getCoilStringAtIndex(int index) {        return stringCoilResult[index];    }    /**     * Gets the coil boolean.     *     * @return coil status from boolean array     */    public boolean[] getCoilBoolean() {        return booleanCoilResult;    }    /**     * Gets the coil string.     *     * @return coil status from String array     */    public String[] getCoilString() {        return stringCoilResult;    }    /**     * Gets the read bit boolean.     *     * @return bit status from boolean array     */    public boolean[] getReadBitBoolean() {        return booleanReadBitResult;    }    /**     * Gets the read bit boolean at index.     *     * @param index the index     * @return bit status from boolean array at index     */    public boolean getReadBitBooleanAtIndex(int index) {        return booleanReadBitResult[index];    }    /**     * Gets the read bit string.     *     * @return bit status from String array     */    public String[] getReadBitString() {        return stringReadBitResult;    }    /**     * Gets the read bit string bin.     *     * @return String binary format (01010101..)     */    public String[] getReadBitStringBin() {        StringBuilder[] str = new StringBuilder[toString.length];        for (int i = 0; i < str.length; i++) {            str[i] = new StringBuilder(toString[i]);            int idx = str[i].length() - 4;            while (idx > 0) {                str[i].insert(idx,  );                toString[i] = str[i].toString();                idx = idx - 4;            }        }        return toString;    }    /**     * Gets the read bit string at index.     *     * @param index the index     * @return bit status from String array at index     */    public String getReadBitStringAtIndex(int index) {        return stringReadBitResult[index];    }    /**     * Gets the boolean discrete input result.     *     * @return dicrete input status from boolean array     */    public boolean[] getBooleanDiscreteInputResult() {        return booleanDiscreteInputResult;    }    /**     * Gets the boolean discrete input result at index.     *     * @param index the index     * @return dicrete input status from boolean array at index     */    public boolean getBooleanDiscreteInputResultAtIndex(int index) {        return booleanDiscreteInputResult[index];    }    /**     * Gets the string discrete input result.     *     * @return dicrete input status from String array     */    public String[] getStringDiscreteInputResult() {        return stringDiscreteInputResult;    }    /**     * Gets the string discrete input result at index.     *     * @param index the index     * @return dicrete input status from String array at index     */    public String getStringDiscreteInputResultAtIndex(int index) {        return stringDiscreteInputResult[index];    }    /**     * Gets the int analog input registers result.     *     * @return analog input status from int array     */    public int[] getIntAnalogInputRegistersResult() {        return intAnalogInputRegistersResult;    }    /**     * Gets the int analog input registers result at index.     *     * @param index the index     * @return analog input status from int array at index     */    public int getIntAnalogInputRegistersResultAtIndex(int index) {        return intAnalogInputRegistersResult[index];    }    /**     * Gets the string analog input registers result.     *     * @return analog input status from String array     */    public String[] getStringAnalogInputRegistersResult() {        return stringAnalogInputRegistersResult;    }    /**     * Gets the string analog input registers result at index.     *     * @param index the index     * @return analog input status from String array at index     */    public String getStringAnalogInputRegistersResultAtIndex(int index) {        return stringAnalogInputRegistersResult[index];    }}
Modbus connection over TCP and Serial port
java;beginner;object oriented;tcp;serial port