package com.dy.common.mw.protocol.pMeterV1_0_1;
|
|
import com.dy.common.util.ByteUtil;
|
import com.dy.common.util.ByteUtilUnsigned;
|
|
|
public class CommonV1_0_1 {
|
/**
|
* 检查头
|
* @param bs
|
* @return
|
* @throws Exception
|
*/
|
public Boolean isThisProtocolHead(byte[] bs) throws Exception{
|
if(bs == null){
|
return null ;
|
}else if(bs.length >= (ProtocolConstantV1_0_1.versionIndex + 1)
|
&& bs[0] == ProtocolConstantV1_0_1.P_Head_Byte){
|
String vs = this.parseVersion(bs) ;
|
if(vs.equals(ProtocolConstantV1_0_1.version)){
|
return true ;
|
}else{
|
return false ;
|
}
|
}else{
|
return false ;
|
}
|
}
|
|
/**
|
* 检查头
|
* @param bs
|
* @return
|
* @throws Exception
|
*/
|
public void checkHead(byte[] bs) throws Exception{
|
if(bs.length >= 1 && bs[0] == ProtocolConstantV1_0_1.P_Head_Byte){
|
return ;
|
}else{
|
throw new Exception("上行数据帧头不正确!") ;
|
}
|
}
|
|
/**
|
* 检查尾
|
* @param bs
|
* @return
|
* @throws Exception
|
*/
|
public void checkTail(byte[] bs) throws Exception{
|
if(bs.length >= 1 && bs[bs.length - 1] == ProtocolConstantV1_0_1.P_Tail_Byte){
|
return ;
|
}else{
|
throw new Exception("上行数据尾不正确!") ;
|
}
|
}
|
|
|
|
/**
|
* 校验和检查
|
* @param bs
|
* @param userdataLen
|
* @param hasPackages
|
* @return
|
* @throws Exception
|
*/
|
public boolean checkCrc(byte[] bs) throws Exception {
|
byte he = 0 ;
|
for(int i = 0 ; i <= bs.length - 3 ; i++){
|
he = (byte)(he + bs[i]) ;
|
}
|
if(bs[bs.length - 2] == he){
|
return true ;
|
}else{
|
return false ;
|
}
|
}
|
|
/**
|
* 校验和检查
|
* @param bs
|
* @param userdataLen
|
* @param hasPackages
|
* @return
|
* @throws Exception
|
*/
|
public String checkCrc_str(byte[] bs) throws Exception {
|
byte[] he = new byte[1] ;
|
for(int i = 0 ; i <= bs.length - 3 ; i++){
|
he[0] = (byte)(he[0] + bs[i]) ;
|
}
|
short hes = ByteUtilUnsigned.byte2Byte(he, 0);
|
short heOrg = ByteUtilUnsigned.byte2Byte(bs, bs.length - 2);
|
if(hes == heOrg){
|
return null ;
|
}else{
|
return "计算校验和是:" + hes + ",上传校验和是" + heOrg ;
|
}
|
}
|
|
/**
|
* 分析版本号
|
* @param bs
|
* @return
|
* @throws Exception
|
*/
|
public String parseVersion(byte[] bs)throws Exception{
|
Short ver = ByteUtilUnsigned.byte2Byte(bs, ProtocolConstantV1_0_1.versionIndex) ;
|
char[] cs = ("" + ver).toCharArray() ;
|
String vs = "" ;
|
for(byte i = 0 ; i < cs.length; i++){
|
if(i == 0){
|
vs += "" + cs[i] ;
|
}else{
|
vs += "." + cs[i] ;
|
}
|
}
|
return vs ;
|
}
|
|
|
/**
|
* 分析数据字节数
|
* @param bs
|
* @return
|
* @throws Exception
|
*/
|
public int parseDataLen(byte[] bs)throws Exception{
|
return ByteUtilUnsigned.bytes2Short_BE(bs, ProtocolConstantV1_0_1.dataLenIndex_start) ;
|
}
|
|
|
/**
|
* 分析IMEI号
|
* @param bs
|
* @return
|
* @throws Exception
|
*/
|
public String parseRtuAddr(byte[] bs)throws Exception{
|
return "" + ByteUtil.BCD2Long_BE(bs, ProtocolConstantV1_0_1.rtuAddrIndex_start, ProtocolConstantV1_0_1.rtuAddrIndex_end) ;
|
}
|
|
|
/**
|
* 分析表号
|
* @param bs
|
* @return
|
* @throws Exception
|
*/
|
public String parseMeterNo(byte[] bs)throws Exception{
|
return "" + ByteUtil.BCD2Long_BE(bs, ProtocolConstantV1_0_1.meterNoIndex_start, ProtocolConstantV1_0_1.meterNoIndex_end) ;
|
}
|
|
|
/**
|
* 分析功能码
|
* @param bs
|
* @return
|
* @throws Exception
|
*/
|
public String parseCode(byte[] bs)throws Exception{
|
return ByteUtil.bytes2Hex(bs, false, ProtocolConstantV1_0_1.codeIndex, 1) ;
|
}
|
|
}
|