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

com.iteaj.iot.modbus.client.rtu.ModbusRtuClientMessage Maven / Gradle / Ivy

There is a newer version: 3.1.1
Show newest version
package com.iteaj.iot.modbus.client.rtu;

import com.iteaj.iot.client.ClientMessage;
import com.iteaj.iot.modbus.server.rtu.ModbusRtuBody;
import com.iteaj.iot.modbus.server.rtu.ModbusRtuHeader;
import com.iteaj.iot.modbus.server.rtu.ModbusRtuMessage;
import com.iteaj.iot.utils.ByteUtil;
import com.iteaj.iot.modbus.ModbusUtil;

public class ModbusRtuClientMessage extends ClientMessage implements ModbusRtuMessage {

    private String equipCode;

    public ModbusRtuClientMessage(byte[] message) {
        super(message);
    }

    public ModbusRtuClientMessage(ModbusRtuHeader head) {
        super(head);
    }

    public ModbusRtuClientMessage(ModbusRtuHeader head, ModbusRtuBody body) {
        super(head, body);
    }

    /**
     * 进行CRC计算
     */
    @Override
    public void writeBuild() {
        super.writeBuild();
        String crc = ModbusUtil.getCRC(ByteUtil.subBytes(this.message
                , 0, this.message.length - 2), true);

        byte[] bytes = ByteUtil.hexToByte(crc);
        this.message[this.length() - 1] = bytes[1];
        this.message[this.length() - 2] = bytes[0];
    }

    @Override
    protected ModbusRtuHeader doBuild(byte[] message) {
        this.messageBody = ModbusRtuBody.buildResponseBody(message);
        this.messageHead = ModbusRtuHeader.buildResponseHeader(message);

        this.getHead().setType(getBody().getCode());
        this.getHead().setEquipCode(this.getEquipCode());
        return this.getHead();
    }

    @Override
    public ModbusRtuHeader getHead() {
        return (ModbusRtuHeader) super.getHead();
    }

    @Override
    public ModbusRtuBody getBody() {
        return (ModbusRtuBody) super.getBody();
    }

    @Override
    public String getEquipCode() {
        return equipCode;
    }

    public void setEquipCode(String equipCode) {
        this.equipCode = equipCode;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy