Commit c1dfa26a authored by 耿迪迪's avatar 耿迪迪

设备上报问题修改

parent 7e3a8a11
...@@ -79,9 +79,9 @@ public class TCPServerHandler extends ChannelInboundHandlerAdapter { ...@@ -79,9 +79,9 @@ public class TCPServerHandler extends ChannelInboundHandlerAdapter {
String msgStr = HexUtils.bytesToHex(bytes1); String msgStr = HexUtils.bytesToHex(bytes1);
logger.info("receiveTCPMsgstr======="+msgStr); logger.info("receiveTCPMsgstr======="+msgStr);
byte[] bytes = HexUtils.hexToByteArray( msgStr.substring(0,msgStr.length()-4)); byte[] bytes = HexUtils.hexToByteArray( msgStr.substring(0,msgStr.length()-4));
int checkVule = CrcUtil.getCrcToHex(bytes,true); String checkVule = CrcUtil.getCrcToHex(bytes,true);
String checkCode = msgStr.substring(msgStr.length()-4,msgStr.length()); String checkCode = msgStr.substring(msgStr.length()-4,msgStr.length());
if(checkVule != Integer.parseInt(checkCode, 16)){ if(!checkVule.equals(checkCode)){
logger.error("接收数据校验失败"); logger.error("接收数据校验失败");
throw new Exception("接收数据校验失败"); throw new Exception("接收数据校验失败");
} }
......
...@@ -44,9 +44,9 @@ public class UDPServerHandler extends SimpleChannelInboundHandler<DatagramPacket ...@@ -44,9 +44,9 @@ public class UDPServerHandler extends SimpleChannelInboundHandler<DatagramPacket
String msgStr = HexUtils.bytesToHex(bytes1); String msgStr = HexUtils.bytesToHex(bytes1);
logger.info("receiveUPDMsgstr======="+msgStr); logger.info("receiveUPDMsgstr======="+msgStr);
byte[] bytes = HexUtils.hexToByteArray( msgStr.substring(0,msgStr.length()-4)); byte[] bytes = HexUtils.hexToByteArray( msgStr.substring(0,msgStr.length()-4));
int checkVule = CrcUtil.getCrcToHex(bytes,true); String checkVule = CrcUtil.getCrcToHex(bytes,true);
String checkCode = msgStr.substring(msgStr.length()-4,msgStr.length()); String checkCode = msgStr.substring(msgStr.length()-4,msgStr.length());
if(checkVule != Integer.parseInt(checkCode, 16)){ if(!checkVule.equals(checkCode)){
logger.error("接收数据校验失败"); logger.error("接收数据校验失败");
throw new Exception("接收数据校验失败"); throw new Exception("接收数据校验失败");
} }
......
...@@ -39,15 +39,14 @@ public class CrcUtil { ...@@ -39,15 +39,14 @@ public class CrcUtil {
* @param reverse 是否转换高低位 * @param reverse 是否转换高低位
* @return * @return
*/ */
public static int getCrcToHex(byte[] bytes,boolean reverse) { public static String getCrcToHex(byte[] bytes,boolean reverse) {
int CRC = getCRC(bytes); int CRC = getCRC(bytes);
//高低位转换,看情况使用(譬如本人这次对led彩屏的通讯开发就规定校验码高位在前低位在后,也就不需要转换高低位) //高低位转换,看情况使用(譬如本人这次对led彩屏的通讯开发就规定校验码高位在前低位在后,也就不需要转换高低位)
if(reverse){ if(reverse){
CRC = ( (CRC & 0x0000FF00) >> 8) | ( (CRC & 0x000000FF ) << 8); CRC = ( (CRC & 0x0000FF00) >> 8) | ( (CRC & 0x000000FF ) << 8);
} }
return CRC; return Integer.toHexString(CRC);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment