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

com.iteaj.iot.modbus.server.rtu.ModbusRtuServerMessage Maven / Gradle / Ivy

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

import com.iteaj.iot.server.ServerMessage;
import com.iteaj.iot.utils.ByteUtil;
import com.iteaj.iot.modbus.ModbusUtil;

public abstract class ModbusRtuServerMessage extends ServerMessage implements ModbusRtuMessage {

    private String equipCode;

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

    public ModbusRtuServerMessage(String equipCode) {
        super(EMPTY);
        this.equipCode = equipCode;
    }

    protected ModbusRtuServerMessage(ModbusRtuHeader head) {
        super(head);
    }

    protected ModbusRtuServerMessage(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);

        ModbusRtuHeader rtuHeader = ModbusRtuHeader.buildResponseHeader(message);
        rtuHeader.setEquipCode(this.equipCode);
        rtuHeader.setMessageId(getChannelId());

        // #解决头部没有设置协议类型问题
        rtuHeader.setType(getBody().getCode());
        return rtuHeader;
    }

    public String getEquipCode() {
        return equipCode;
    }

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy