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

com.iteaj.iot.modbus.server.tcp.ModbusTcpServerComponent Maven / Gradle / Ivy

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

import com.iteaj.iot.AbstractProtocol;
import com.iteaj.iot.Message;
import com.iteaj.iot.Protocol;
import com.iteaj.iot.config.ConnectProperties;
import com.iteaj.iot.server.component.LengthFieldBasedFrameDecoderServerComponent;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;

import java.util.Optional;

/**
 * 标准的Modbus Tcp服务组件
 * @param 
 */
public abstract class ModbusTcpServerComponent extends LengthFieldBasedFrameDecoderServerComponent {

    public ModbusTcpServerComponent(ConnectProperties connectProperties) {
        this(connectProperties, 512);
    }

    public ModbusTcpServerComponent(ConnectProperties connectProperties, int maxFrameLength) {
        super(connectProperties, maxFrameLength, 4, 2, 0, 0, true);
    }

    @Override
    public String getName() {
        return "ModbusTcpServer";
    }

    @Override
    public String getDesc() {
        return "标准ModbusTcp协议的iot服务端实现";
    }

    @Override
    public AbstractProtocol getProtocol(ModbusTcpServerMessage message) {
        Message.MessageHead head = message.getHead();
        return remove(head.getMessageId());
    }

    @Override
    public Optional writeAndFlush(String equipCode, Protocol protocol) {
        Channel channel = getDeviceManager().find(equipCode);
        if(channel != null) {
            ModbusTcpHeader head = (ModbusTcpHeader)protocol.requestMessage().getHead();

            // 设置Modbus的递增值和messageId
            short nextId = ModbusTcpMessageBuilder.getNextId(channel);
            ModbusTcpMessageBuilder.buildMessageHeadByNextId(nextId, head);
        }

        return super.writeAndFlush(equipCode, protocol);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy