All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ghgande.j2mod.modbus.msg.WriteMultipleRegistersResponse Maven / Gradle / Ivy

There is a newer version: 3.2.1
Show newest version
/*
 * Copyright 2002-2016 jamod & j2mod development teams
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ghgande.j2mod.modbus.msg;

import com.ghgande.j2mod.modbus.Modbus;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * Class implementing a WriteMultipleRegistersResponse. The
 * implementation directly correlates with the class 0 function preset multiple
 * registers (FC 16). It encapsulates the corresponding response message.
 *
 * @author Dieter Wimberger
 * @author Steve O'Hara (4NG)
 * @version 2.0 (March 2016)
 */
public class WriteMultipleRegistersResponse extends ModbusResponse {
    // instance attributes
    private int wordCount;
    private int reference;

    /**
     * Constructs a new WriteMultipleRegistersResponse instance.
     */
    public WriteMultipleRegistersResponse() {
        super();

        setFunctionCode(Modbus.WRITE_MULTIPLE_REGISTERS);
        setDataLength(4);
    }

    /**
     * Constructs a new WriteMultipleRegistersResponse instance.
     *
     * @param reference the offset to start writing from.
     * @param wordCount the number of words (registers) to be written.
     */
    public WriteMultipleRegistersResponse(int reference, int wordCount) {
        super();

        setFunctionCode(Modbus.WRITE_MULTIPLE_REGISTERS);
        setDataLength(4);

        this.reference = reference;
        this.wordCount = wordCount;
    }

    /**
     * Returns the reference of the register to start writing to with this
     * WriteMultipleRegistersResponse.
     * 

* * @return the reference of the register to start writing to as int * . */ public int getReference() { return reference; } /** * Sets the reference of the register to start writing to with this * WriteMultipleRegistersResponse. *

* * @param ref the reference of the register to start writing to as * int. */ public void setReference(int ref) { reference = ref; } /** * Returns the number of bytes that have been written. * * @return the number of bytes that have been written as int. */ public int getByteCount() { return wordCount * 2; } /** * Returns the number of words that have been written. The returned value * should be half of the byte count of the response. *

* * @return the number of words that have been written as int. */ public int getWordCount() { return wordCount; } /** * Sets the number of words that have been returned. * * @param count the number of words as int. */ public void setWordCount(int count) { wordCount = count; } @Override public void writeData(DataOutput dout) throws IOException { dout.write(getMessage()); } @Override public void readData(DataInput din) throws IOException { setReference(din.readUnsignedShort()); setWordCount(din.readUnsignedShort()); setDataLength(4); } @Override public byte[] getMessage() { byte[] result = new byte[4]; result[0] = (byte)((reference >> 8) & 0xff); result[1] = (byte)(reference & 0xff); result[2] = (byte)((wordCount >> 8) & 0xff); result[3] = (byte)(wordCount & 0xff); return result; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy