| | |
| | | |
| | | import com.dy.common.mw.protocol.*; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.*; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.parse.global.GlCreate; |
| | | import com.dy.common.util.ByteUtil; |
| | | import com.dy.common.util.ByteUtilUnsigned; |
| | | import com.dy.common.util.CRC8_for_2_0; |
| | | import com.dy.common.util.DateTime; |
| | | |
| | | @AnnotationCodeDown(ifAny={ |
| | | CodeV1_0_1.cd_06 |
| | | CodeV1_0_1.cd_02 |
| | | }) |
| | | public class Cd_02_Down implements CodeParse { |
| | | |
| | | //private static Logger log = LogManager.getLogger(Cd_XY_Down.class); |
| | | //private static Logger log = LogManager.getLogger(Cd_02_Down.class); |
| | | |
| | | @Override |
| | | public MidResult[] parse(Boolean isLowPower, CodeParseParams params, CodeParseCallback callback) throws Exception { |
| | |
| | | |
| | | MidResultToRtu midRs = new MidResultToRtu() ; |
| | | midRs.protocolName = para.protocolName ;//协议名称 |
| | | midRs.rtuAddr = para.rtuAddr ;//IMEI号(电信平台设备IMEI) |
| | | midRs.rtuAddr = para.rtuAddr ;//Rtu地址 |
| | | midRs.commandId = para.commandId ;//命令ID,发起命令的客户端(web端)生成,以匹配命令结果 |
| | | midRs.downCode = para.commandCode ;//下行命令功能码; |
| | | midRs.downBuffer = bs ;//下行命令数据 |
| | | midRs.downBufHex = ByteUtil.bytes2Hex(bs, true) ;//下行命令数据十六进制形式 |
| | | midRs.hasResponse = true ;//是否有应答 |
| | | midRs.maxSendTimes = null ;//命令最大发送次数(当收不到应答时,将重发),如果不设置,命令缓存器进行补充设置 |
| | | midRs.isCachForOffLine = true ;//RTU不在线,命令是否缓存,低功耗时为true |
| | | |
| | | //对RTU上行数据的应答 |
| | | midRs.hasResponse = false ;//是否有应答 |
| | | midRs.maxSendTimes = 1 ;//对RTU上行数据应答,所以只需发一次 |
| | | midRs.maxSendTimes = 1 ;//命令最大发送次数(当收不到应答时,将重发),如果不设置,命令缓存器进行补充设置 |
| | | midRs.isCachForOffLine = false ;//RTU不在线,命令是否缓存,低功耗时为true |
| | | |
| | | midRs.isSendFirst = true ;//命令应答,首先发送,防止RTU重新上报 |
| | | if(isLowPower != null && isLowPower.booleanValue()){ |
| | | //低功耗时 |
| | | //低功耗时,尽快发送 |
| | | midRs.isQuickSend = true ; |
| | | } |
| | | |
| | |
| | | } |
| | | /** |
| | | * 构造下行数据 |
| | | * @param para |
| | | * @return |
| | | * @throws Exception |
| | | * @param para 参数 |
| | | * @return 字节数组 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public byte[] doParse(ParseParamsForDownV1_0_1 para) throws Exception { |
| | | CommonV1_0_1 commonV1_0_1 = new CommonV1_0_1() ; |
| | |
| | | bsHead[index] = ProtocolConstantV206V1_0_0.P_Head_Byte ; |
| | | |
| | | index++ ; |
| | | bsHead[index] = commonV1_0_1.createCtrl((byte)0) ; |
| | | |
| | | String rtuAddr = para.rtuAddr ; |
| | | String rtuAddr1 = rtuAddr.substring(0, 6) ; |
| | | String rtuAddr2 = rtuAddr.substring(6) ; |
| | | bsHead[index] = commonV1_0_1.createCtrl((byte)0, (byte)0) ; |
| | | |
| | | index++ ; |
| | | ByteUtil.string2BCD_BE(bsHead, rtuAddr1, index) ; |
| | | index += 3 ; |
| | | |
| | | Integer rtuAddr2Int = Integer.parseInt(rtuAddr2) ; |
| | | ByteUtilUnsigned.short2Bytes_BE(bsHead, rtuAddr2Int.shortValue(), index); |
| | | index += 2 ; |
| | | GlCreate.createRtuAddr(para.rtuAddr, bsHead, index); |
| | | index += 5 ; |
| | | |
| | | ByteUtil.hex2Bytes(para.commandCode, bsHead, index) ; |
| | | |
| | | byte[] bs = new byte[7] ; |
| | | bs[0] = (byte)0xF2 ; |
| | | ByteUtil.string2BCD_BE(bs, DateTime.yyMMddhhmmss(), 1) ; |
| | | bs[0] = (byte)0xF2 ;//数据域: 1 个字节,F0 登录, F1 退出登录,F2 在线保持。 |
| | | GlCreate.createTp(bs, 1); |
| | | |
| | | bytes = ByteUtil.bytesMerge(bsHead, bs) ; |
| | | |
| | | int len = bytes.length + ProtocolConstantV206V1_0_0.lenTail; |
| | | bytes[ProtocolConstantV206V1_0_0.dataLenIndex] = (byte)len ; |
| | | GlCreate.createLen(bytes);//长度放字节数组中 |
| | | |
| | | int crc = new CRC8_for_2_0().CRC8(bytes, ProtocolConstantV206V1_0_0.ctrlIndex, bytes.length -1) ; |
| | | bs = new byte[ProtocolConstantV206V1_0_0.lenTail] ; |
| | | bs[0] = (byte)crc ; |
| | | bs[1] = ProtocolConstantV206V1_0_0.P_Tail_Byte ; |
| | | bytes = ByteUtil.bytesMerge(bytes, bs) ; |
| | | byte[] bsTail = GlCreate.createCrcTail(bytes) ;//CRC和尾叠加字节数组中 |
| | | |
| | | bytes = ByteUtil.bytesMerge(bytes, bsTail) ; |
| | | |
| | | return bytes ; |
| | | } |
| | | |
| | | public static void main(String[] args) throws Exception{ |
| | | ParseParamsForDownV1_0_1 p = new ParseParamsForDownV1_0_1() ; |
| | | p.commandCode = "02" ; |
| | | p.rtuAddr = "9672950001"; |
| | | |
| | | Cd_02_Down obj = new Cd_02_Down() ; |
| | | byte[] bs = obj.doParse(p) ; |
| | | |
| | | String hex1 = ByteUtil.bytes2Hex(bs,true) ; |
| | | String hex2 = ByteUtil.bytes2Hex(bs,false) ; |
| | | |
| | | System.out.println(hex1); |
| | | System.out.println(hex2); |
| | | } |
| | | |
| | | } |