liurunyu
2024-11-08 04779efe2410fb0df1ff983b26384d56471b85cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.dy.pipIrrMwTestRtu.tcpClient.upData;
 
import com.dy.common.mw.protocol.p206V1.parse.global.GlCreate;
import com.dy.common.util.ByteUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
/**
 * @Author: liurunyu
 * @Date: 2024/7/15 14:18
 * @Description
 */
public class UpHeartBeat extends UpData {
 
    private static final Logger log = LogManager.getLogger(UpHeartBeat.class) ;
 
    public static void upCd02Data(String rtuAddr){
        try{
            if(UpData.session != null && UpData.session.isConnected()){
                byte[] bs = createData(rtuAddr) ;
                UpData.upSend(bs) ;
            }else{
                log.error("未连接通信中间件,不能发送数据");
            }
        }catch (Exception e){
            log.error("向通信中间件发送数据产生异常", e);
        }
    }
 
 
    /**
     * 构造心跳数据
     * @return 字节数组
     * @throws Exception 异常
     */
    private static byte[] createData(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 ;
    }
}