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

com.iteaj.iot.modbus.server.rtu.ModbusRtuHeader 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.ProtocolType;
import com.iteaj.iot.message.DefaultMessageHead;
import com.iteaj.iot.modbus.server.tcp.ModbusTcpHeader;
import com.iteaj.iot.utils.ByteUtil;

/**
 * @see ModbusRtuBody
 * @see com.iteaj.iot.modbus.server.dtu.ModbusRtuForDtuMessage
 */
public class ModbusRtuHeader extends DefaultMessageHead {

    private byte unitId; // 单元标识 占1byte (从机地址)

    protected ModbusRtuHeader() {
        this(null, null, null);
    }

    protected ModbusRtuHeader(String equipCode, String messageId, ProtocolType type) {
        super(equipCode, messageId, type);
    }

    /**
     * 构建请求头报文头
     * @return
     */
    public static ModbusRtuHeader buildRequestHeader(byte unitId) {
        ModbusRtuHeader rtuHeader = new ModbusRtuHeader();
        rtuHeader.setUnitId(unitId);
        rtuHeader.setMessage(new byte[]{unitId});
        return rtuHeader;
    }

    /**
     *
     * @param equipCode
     * @param type
     * @return
     */
    public static ModbusRtuHeader buildRequestHeader(String equipCode, String messageId, ProtocolType type) {
        return new ModbusRtuHeader(equipCode, messageId, type);
    }

    public static ModbusRtuHeader buildRequestHeader(byte[] message) {
        return buildResponseHeader(message);
    }

    public static ModbusRtuHeader buildResponseHeader(byte[] message) {
        ModbusRtuHeader header = new ModbusRtuHeader();

        header.unitId = ByteUtil.getByte(message, 0);
        header.setMessage(ByteUtil.subBytes(message, 0, 1));
        return header;
    }

    public static ModbusRtuHeader formModbusTcpHeader(ModbusTcpHeader head) {
        ModbusRtuHeader header = new ModbusRtuHeader();
        header.unitId = head.getUnitId();
        header.setMessage(new byte[]{header.unitId});
        return header;
    }

    public byte getUnitId() {
        return unitId;
    }

    public void setUnitId(byte unitId) {
        this.unitId = unitId;
    }

    @Override
    public String toString() {
        return "ModbusRtuHeader{" +
                "unitId=" + unitId +
                '}';
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy