zhubaomin
2025-04-07 ecef3df4890be54c1da2a8a4fc1c8c1f50f1c263
pipIrr-platform/pipIrr-mw/pipIrr-mwTest-client/src/main/java/com/dy/testClient/tcpClient/RtuUpData.java
New file
@@ -0,0 +1,286 @@
package com.dy.testClient.tcpClient;
import com.dy.common.mw.protocol.p206V1.CommonV1;
import com.dy.common.mw.protocol.p206V1.ProtocolConstantV206V1;
import com.dy.common.mw.protocol.p206V1.parse.global.GlCreate;
import com.dy.common.util.ByteUtil;
/**
 * @Author: liurunyu
 * @Date: 2024/7/11 13:55
 * @Description
 */
public class RtuUpData {
    private static byte[] creatHead(String rtuAddr, String code, byte ctrl)throws Exception {
        byte[] bsHead = new byte[ProtocolConstantV206V1.lenHead2Code] ;
        byte index = 0 ;
        bsHead[index] = ProtocolConstantV206V1.P_Head_Byte ;
        index++ ;
        bsHead[index] = 0 ;//帧长度
        index++ ;
        bsHead[index] = ProtocolConstantV206V1.P_Head_Byte ;
        index++ ;
        bsHead[index] = ctrl; //控制域功能码
        index++ ;
        GlCreate.createRtuAddr4P206(rtuAddr, bsHead, index);
        index += 5 ;
        ByteUtil.hex2Bytes(code, bsHead, index) ;
        return bsHead ;
    }
    /**
     * 构造心跳数据
     * @return 字节数组
     * @throws Exception 异常
     */
    public static byte[] createHeartbeat(String rtuAddr) throws Exception {
        byte[] bytes = creatHead(rtuAddr, "02", (byte)0xB0);
        byte[] bs = new byte[1] ;
        bs[0] = (byte)0xF2 ;//数据域: 1 个字节,F0 登录, F1 退出登录,F2 在线保持。
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        GlCreate.createLen(bytes);//长度放字节数组中
        byte[] bsTail = GlCreate.createCrcTail4P206(bytes) ;//CRC和尾叠加字节数组中
        bytes = ByteUtil.bytesMerge(bytes, bsTail) ;
        return bytes ;
    }
    /**
     * 构造上报数据
     * @return 字节数组
     * @throws Exception 异常
     */
    public static byte[] createCd83Open(String rtuAddr, String icCardAddr, String icCardNo, String openValveDt) throws Exception {
        byte[] bytes = creatHead(rtuAddr, "83", (byte)0xB0);
        byte[] bs = new byte[1] ;
        bs[0] = (byte)0x01 ;//刷卡开阀
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[5] ;//累计流量
        bs[0] = ByteUtil.int2BCD_LE (21)[0] ;
        bs[1] = ByteUtil.int2BCD_LE (43)[0] ;
        bs[2] = ByteUtil.int2BCD_LE (0)[0] ;
        bs[3] = ByteUtil.int2BCD_LE (0)[0] ;
        bs[4] = ByteUtil.int2BCD_LE (0)[0] ;
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[8] ;//IC卡编号
        GlCreate.createIcCardNo(icCardNo, bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[4] ;//IC卡地址
        ByteUtil.hex2Bytes_LE(icCardAddr, bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[4] ;//用水户余额
        bs[0] = ByteUtil.int2BCD_LE (21)[0] ;
        bs[1] = ByteUtil.int2BCD_LE (43)[0] ;
        bs[2] = ByteUtil.int2BCD_LE (0)[0] ;
        bs[3] = ByteUtil.int2BCD_LE (0)[0] ;
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[6] ;//开阀时间
        GlCreate.createTp(openValveDt, bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[6] ;//控制器时钟
        GlCreate.createTp(bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        GlCreate.createLen(bytes);//长度放字节数组中
        byte[] bsTail = GlCreate.createCrcTail4P206(bytes) ;//CRC和尾叠加字节数组中
        bytes = ByteUtil.bytesMerge(bytes, bsTail) ;
        return bytes ;
    }
    /**
     * 构造上报数据
     * @return 字节数组
     * @throws Exception 异常
     */
    public static byte[] createCd83Close(String rtuAddr, String icCardAddr, String icCardNo, String openValveDt) throws Exception {
        byte[] bytes = creatHead(rtuAddr, "83", (byte)0xB0);
        byte[] bs = new byte[1] ;
        bs[0] = (byte)0x02 ;//刷卡关阀
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[5] ;//累计流量
        bs[0] = ByteUtil.int2BCD_LE (21)[0] ;
        bs[1] = ByteUtil.int2BCD_LE (53)[0] ;
        bs[2] = ByteUtil.int2BCD_LE (0)[0] ;
        bs[3] = ByteUtil.int2BCD_LE (0)[0] ;
        bs[4] = ByteUtil.int2BCD_LE (0)[0] ;
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[8] ;//IC卡编号
        GlCreate.createIcCardNo(icCardNo, bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[4] ;//IC卡地址
        ByteUtil.hex2Bytes_LE(icCardAddr, bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[4] ;//用水户余额
        bs[0] = ByteUtil.int2BCD_LE (21)[0] ;
        bs[1] = ByteUtil.int2BCD_LE (33)[0] ;
        bs[2] = ByteUtil.int2BCD_LE (0)[0] ;
        bs[3] = ByteUtil.int2BCD_LE (0)[0] ;
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[6] ;//开阀时间
        GlCreate.createTp(openValveDt, bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[6] ;//关阀时间
        GlCreate.createTp(bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[5] ;//用水户本次用水量
        bs[0] = ByteUtil.int2BCD_LE (0)[0] ;
        bs[1] = ByteUtil.int2BCD_LE (10)[0] ;
        bs[2] = ByteUtil.int2BCD_LE (0)[0] ;
        bs[3] = ByteUtil.int2BCD_LE (0)[0] ;
        bs[4] = ByteUtil.int2BCD_LE (0)[0] ;
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[4] ;//用水户本次消费金额
        bs[0] = ByteUtil.int2BCD_LE (0)[0] ;
        bs[1] = ByteUtil.int2BCD_LE (10)[0] ;
        bs[2] = ByteUtil.int2BCD_LE (0)[0] ;
        bs[3] = ByteUtil.int2BCD_LE (0)[0] ;
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[2] ;//用水户本次用水时长
        bs[0] = ByteUtil.int2BCD_LE (10)[0] ;
        bs[1] = ByteUtil.int2BCD_LE (0)[0] ;
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[1] ;//水价类型
        bs[0] = ByteUtil.int2BCD_LE (1)[0] ;
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[2] ;//水价
        bs[0] = ByteUtil.int2BCD_LE (0)[0] ;
        bs[1] = ByteUtil.int2BCD_LE (1)[0] ;
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[6] ;//控制器时钟
        GlCreate.createTp(bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[1] ;//卡类型
        bs[0] = ByteUtil.int2BCD_LE (1)[0] ;
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        GlCreate.createLen(bytes);//长度放字节数组中
        byte[] bsTail = GlCreate.createCrcTail4P206(bytes) ;//CRC和尾叠加字节数组中
        bytes = ByteUtil.bytesMerge(bytes, bsTail) ;
        return bytes ;
    }
    /**
     * 构造上报数据
     * @return 字节数组
     * @throws Exception 异常
     */
    public static byte[] createCdC0(String rtuAddr) throws Exception {
        CommonV1 commonV_1 = new CommonV1() ;
        byte[] bytes ;
        byte[] bsHead = new byte[ProtocolConstantV206V1.lenHead2Code] ;
        byte index = 0 ;
        bsHead[index] = ProtocolConstantV206V1.P_Head_Byte ;
        index++ ;
        bsHead[index] = 0 ;//帧长度
        index++ ;
        bsHead[index] = ProtocolConstantV206V1.P_Head_Byte ;
        index++ ;
        bsHead[index] = commonV_1.createCtrl((byte)0x80, (byte)3) ;//控制域功能码3:自报帧,流量(水量)参数
        index++ ;
        GlCreate.createRtuAddr4P206(rtuAddr, bsHead, index);
        index += 5 ;
        ByteUtil.hex2Bytes("C0", bsHead, index) ;
        byte[] bs = new byte[2] ;
        bs[0] = (byte)0x04 ;//有流量数据
        bs[1] = (byte)0xE0 ;//有水压、电池电压、信号强度
        bytes = ByteUtil.bytesMerge(bsHead, bs) ;
        bs = new byte[5] ;//瞬时流量
        bs[0] = ByteUtil.int2BCD_LE (21)[0] ;
        bs[1] = ByteUtil.int2BCD_LE (43)[0] ;
        bs[2] = ByteUtil.int2BCD_LE (65)[0] ;
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[5] ;//累计流量
        bs[0] = ByteUtil.int2BCD_LE (21)[0] ;
        bs[1] = ByteUtil.int2BCD_LE (43)[0] ;
        bs[2] = ByteUtil.int2BCD_LE (65)[0] ;
        bs[3] = ByteUtil.int2BCD_LE (87)[0] ;
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[5] ;//损失流量
        bs[0] = ByteUtil.int2BCD_LE (10)[0] ;
        bs[1] = ByteUtil.int2BCD_LE (32)[0] ;
        bs[2] = ByteUtil.int2BCD_LE (54)[0] ;
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[4] ;//水压
        bs[0] = ByteUtil.int2BCD_LE (10)[0] ;
        bs[1] = ByteUtil.int2BCD_LE (32)[0] ;
        bs[2] = ByteUtil.int2BCD_LE (4)[0] ;
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[4] ;//电池电压
        bs[0] = ByteUtil.int2BCD_LE (10)[0] ;
        bs[1] = ByteUtil.int2BCD_LE (32)[0] ;
        bs[2] = ByteUtil.int2BCD_LE (10)[0] ;
        bs[3] = ByteUtil.int2BCD_LE (32)[0] ;
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[1] ;//信号强度
        bs[0] = ByteUtil.int2BCD_LE (21)[0] ;
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        bs = new byte[6] ;//时钟
        GlCreate.createTp(bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        GlCreate.createLen(bytes);//长度放字节数组中
        byte[] bsTail = GlCreate.createCrcTail4P206(bytes) ;//CRC和尾叠加字节数组中
        bytes = ByteUtil.bytesMerge(bytes, bsTail) ;
        return bytes ;
    }
}