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

com.iteaj.iot.modbus.ModbusRtuOverTcpUtils 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.ModbusCode;
import com.iteaj.iot.modbus.server.rtu.ModbusRtuBody;
import com.iteaj.iot.modbus.server.rtu.ModbusRtuHeader;
import com.iteaj.iot.modbus.server.tcp.ModbusTcpBody;
import com.iteaj.iot.modbus.server.tcp.ModbusTcpHeader;

/**
 * Modbus Rtu协议和Tcp协议相互转换工具
 */
public class ModbusRtuOverTcpUtils {

    public static ModbusTcpHeader toTcpHeader(ModbusRtuHeader header, short rtuLength) {
        return ModbusTcpHeader.buildRequestHeader(header.getUnitId(), rtuLength);
    }

    public static ModbusTcpBody toTcpBody(ModbusRtuBody body) {
        if(body.getCode().getCode() <= 0x04) {
            return ModbusTcpBody.read(body.getCode(), body.getStart(), body.getNum());
        } else if(body.getCode() == ModbusCode.Write0F) {
            return ModbusTcpBody.write0F(body.getStart(), body.getNum(), body.getContent());
        } else if(body.getCode() == ModbusCode.Write10) {
            return ModbusTcpBody.write10(body.getStart(), body.getNum(), body.getContent());
        } else {
            return ModbusTcpBody.writeSingle(body.getCode(), body.getStart(), body.getContent());
        }
    }

    public static ModbusRtuBody toRtuBody(ModbusTcpBody body) {
        return ModbusRtuBody.formModbusTcpBody(body);
    }

    public static ModbusRtuHeader toRtuHeader(ModbusTcpHeader head) {
        return ModbusRtuHeader.formModbusTcpHeader(head);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy