Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/* ==================================================================
* RtuModbusMessageDecoder.java - 1/12/2022 4:52:07 pm
*
* Copyright 2022 SolarNetwork.net Dev Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
* ==================================================================
*/
package net.solarnetwork.io.modbus.rtu.netty;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import net.solarnetwork.io.modbus.AddressedModbusMessage;
import net.solarnetwork.io.modbus.ModbusMessage;
import net.solarnetwork.io.modbus.netty.handler.NettyModbusClient;
import net.solarnetwork.io.modbus.netty.msg.ModbusMessageUtils;
import net.solarnetwork.io.modbus.netty.msg.SimpleModbusMessageReply;
import net.solarnetwork.io.modbus.rtu.netty.RtuModbusMessageDecoder.DecoderState;
/**
* Decoder for RTU Modbus messages.
*
* @author matt
* @version 1.0
*/
public class RtuModbusMessageDecoder extends ReplayingDecoder {
private static final Logger log = LoggerFactory.getLogger(RtuModbusMessageDecoder.class);
/** The length of the fixed-length header. */
public static final int FIXED_HEADER_LENGTH = 7;
/*- RTU frame structure:
|0||1|---||--|
+-++-+---++--+
|a||f|...||cc|
+-++-+---++--+
a = address (unit ID)
f = function code + data
cc = 16-bit CRC (LE order)
*/
/**
* States of the decoder.
*/
enum DecoderState {
READ_FIXED_HEADER,
READ_PAYLOAD,
BAD_DATA,
}
/** True if decoding response messages, false for requests. */
private final boolean controller;
private short unitId;
/**
* Constructor.
*
* @param controller
* {@literal true} if operating as a controller where decoding is for
* Modbus response message, or {@literal false} if operating as a
* responder where decoding is for Modbus request messages
*/
public RtuModbusMessageDecoder(boolean controller) {
super(DecoderState.READ_FIXED_HEADER);
this.controller = controller;
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List