package com.dy.pipirrComCreator.p206V1; 
 | 
  
 | 
import com.dy.common.mw.protocol.p206V1.parse.global.GlCreate; 
 | 
import com.dy.common.util.ByteUtil; 
 | 
import com.dy.common.util.ByteUtilUnsigned; 
 | 
import com.dy.common.util.IPUtils; 
 | 
import com.dy.common.util.NumUtil; 
 | 
  
 | 
/** 
 | 
 * @Author: liurunyu 
 | 
 * @Date: 2025/5/7 11:25 
 | 
 * @Description 
 | 
 */ 
 | 
public class P206V1Cd21 extends P206V1Cd { 
 | 
  
 | 
    public static final String ComCode = "21" ; 
 | 
  
 | 
    public String checkParams(String ...params){ 
 | 
        if(params.length != 2){ 
 | 
            return "只有IP和端口两个参数" ; 
 | 
        } 
 | 
        if(!IPUtils.ipValid(params[0])){ 
 | 
            return "IP地址不正确" ; 
 | 
        } 
 | 
        if(IPUtils.internalIp(params[0])){ 
 | 
            return "IP地址必须是外网IP" ; 
 | 
        } 
 | 
        if(!NumUtil.isPlusIntNumber(params[1])){ 
 | 
            return "端口号必须是正整数" ; 
 | 
        } 
 | 
        int port = Integer.parseInt(params[1]) ; 
 | 
        if(port < 1 || port > 65535){ 
 | 
            return "端口号必须在1-65535之间" ; 
 | 
        } 
 | 
        return null ; 
 | 
    } 
 | 
    public String helpInfo() { 
 | 
        return ComCode + " *...*(IP) *...*(端口)[Enter](设置服务端IP和端口)" ; 
 | 
    } 
 | 
    /** 
 | 
     * 构造命令数据(十六进制) 
 | 
     * @return 字符串 
 | 
     * @throws Exception 异常 
 | 
     */ 
 | 
    public String hex(String rtuAddr, String ...params) throws Exception { 
 | 
        byte[] bytes = bs(rtuAddr, params) ; 
 | 
        return ByteUtil.bytes2Hex(bytes, false) ; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 构造命令数据(字节数组) 
 | 
     * @return 字节数组 
 | 
     * @throws Exception 异常 
 | 
     */ 
 | 
    public byte[] bs(String rtuAddr, String ...params) throws Exception { 
 | 
        byte[] bytes = createHead(rtuAddr, ComCode, P206V1Cd.CTRL); 
 | 
  
 | 
        byte n = 0 ; 
 | 
        byte[] bs = new byte[14] ; 
 | 
        String[] ip = params[0].split("\\.") ; 
 | 
        ByteUtilUnsigned.byte2Byte(bs, Byte.parseByte(ip[0]), n++); 
 | 
        ByteUtilUnsigned.byte2Byte(bs, Byte.parseByte(ip[1]), n++); 
 | 
        ByteUtilUnsigned.byte2Byte(bs, Byte.parseByte(ip[2]), n++); 
 | 
        ByteUtilUnsigned.byte2Byte(bs, Byte.parseByte(ip[3]), n++); 
 | 
        ByteUtilUnsigned.short2Bytes_LE(bs, Short.parseShort(params[1]), n); 
 | 
        n += 2 ; 
 | 
        GlCreate.createPw(bs, n); 
 | 
        n += 2 ; 
 | 
        GlCreate.createTp(bs, n); 
 | 
  
 | 
        bytes = ByteUtil.bytesMerge(bytes, bs); 
 | 
  
 | 
        GlCreate.createLen(bytes);//长度放字节数组中 
 | 
  
 | 
        byte[] bsTail = GlCreate.createCrcTail4P206(bytes) ;//CRC和尾叠加字节数组中 
 | 
  
 | 
        bytes = ByteUtil.bytesMerge(bytes, bsTail) ; 
 | 
  
 | 
        return bytes ; 
 | 
    } 
 | 
  
 | 
    public static void main(String[] args) throws Exception { 
 | 
        String[] ps = new String[]{"1.2.3.4", "123"} ; 
 | 
        new P206V1Cd21().hex("530115059980", ps) ; 
 | 
    } 
 | 
} 
 |