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

com.iteaj.iot.modbus.RealCoilPayload Maven / Gradle / Ivy

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

import com.iteaj.iot.modbus.consts.ModbusBitStatus;
import com.iteaj.iot.utils.ByteUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 读线圈负载
 */
public class RealCoilPayload extends Payload {

    /**
     * 读几个线圈寄存器
     */
    private int num;

    /**
     * 每个线圈(二进制位)的状态 0.OFF  1.ON
     */
    private List statuses;

    public RealCoilPayload(int num, byte[] payload) {
        super(payload);
        this.num = num;
        this.statuses = new ArrayList<>();
        this.buildBitStatus(payload, this.statuses);
    }

    /**
     * 获取指定位的boolean状态
     * @param bit
     * @return  1. true 0. false
     */
    @Override
    public boolean readBoolean(int bit) {
        return this.getStatuses().get(bit) == ModbusBitStatus.ON;
    }

    /**
     * 读取指定位的状态
     * @param bit
     * @return
     */
    @Override
    public ModbusBitStatus readStatus(int bit) {
        return this.getStatuses().get(bit);
    }

    public List getStatuses() {
        return statuses;
    }

    public void setStatuses(List statuses) {
        this.statuses = statuses;
    }

    private void buildBitStatus(byte[] bits, List statuses) {
        for(int j=0; j this.num) return;

                final byte bit = ByteUtil.bitAtByte((byte) (item & 0xFF), i);
                statuses.add(bit == 0 ? ModbusBitStatus.OFF : ModbusBitStatus.ON);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy