|
|
|
package com.zhonglai.luhui.smart.feeder.dto;
|
|
|
|
|
|
|
|
import cn.hutool.core.util.ArrayUtil;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.ruoyi.common.utils.ByteUtil;
|
|
|
|
import com.ruoyi.common.utils.GsonConstructor;
|
|
|
|
import lombok.Data;
|
|
|
|
import org.apache.commons.lang3.ArrayUtils;
|
|
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modbus协议
|
|
...
|
...
|
@@ -17,11 +16,26 @@ import java.util.Map; |
|
|
|
@Data
|
|
|
|
public class ModbusDto implements Serializable {
|
|
|
|
private static final long serialVersionUID = -6008279428004571734L;
|
|
|
|
protected String hstr;
|
|
|
|
private String hstr;
|
|
|
|
protected Integer address; //地址位
|
|
|
|
protected Integer commdcode; //功能码
|
|
|
|
protected byte[] data; //数据
|
|
|
|
protected String crc; //16CRC 码
|
|
|
|
|
|
|
|
public ModbusDto(Integer address,Integer commdcode,byte[] data)
|
|
|
|
{
|
|
|
|
this.address = address;
|
|
|
|
this.commdcode = commdcode;
|
|
|
|
this.data = data;
|
|
|
|
|
|
|
|
byte[] heardbyte = new byte[2];
|
|
|
|
heardbyte[0] = address.byteValue();
|
|
|
|
heardbyte[1] = commdcode.byteValue();
|
|
|
|
byte[] notlrcdata = ArrayUtil.addAll(heardbyte,data);
|
|
|
|
this.crc = generateLRC(notlrcdata);
|
|
|
|
this.hstr = ByteUtil.toHexString(notlrcdata)+this.crc;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ModbusDto()
|
|
|
|
{
|
|
|
|
|
|
...
|
...
|
@@ -48,6 +62,12 @@ public class ModbusDto implements Serializable { |
|
|
|
new ModbusDto(ByteUtil.hexStringToByte(str));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public byte[] generateCommd()
|
|
|
|
{
|
|
|
|
return ByteUtil.hexStringToByte(hstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 计算CRC校验码
|
|
|
|
public static String generateLRC(byte[] data)
|
|
|
|
{
|
|
...
|
...
|
@@ -63,6 +83,8 @@ public class ModbusDto implements Serializable { |
|
|
|
tmp += 1;
|
|
|
|
return (byte) tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
String hexData = "01 03 8E 00 01 00 04 FF E0 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 07 00 03 00 00 00 1D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00".replace(" ","");
|
|
|
|
String crc = ByteUtil.getCRC16(ByteUtil.hexStringToByte(hexData));
|
...
|
...
|
|