|
@@ -16,7 +16,7 @@ public class CommBuildService {
|
|
|
private static final Logger l = LoggerFactory.getLogger(CommBuildService.class);
|
|
|
/**
|
|
|
* CRC-16(Modbus)
|
|
|
- *
|
|
|
+ * <p>
|
|
|
* https://www.23bei.com/tool/59.html
|
|
|
*/
|
|
|
public static boolean CRC_16_MODBUS = true;
|
|
@@ -71,19 +71,19 @@ public class CommBuildService {
|
|
|
SimMsg smS = new SimMsg();
|
|
|
smS.setSimId(idService.nextId());
|
|
|
if (StringUtils.isEmpty(simNum) || StringUtils.isEmpty(cmd) || StringUtils.isEmpty(cmdId) || StringUtils.isEmpty(data)) {
|
|
|
- throw new IllegalArgumentException("buildSendMsg isEmpty");
|
|
|
+ throw new IllegalArgumentException("buildSendMsg error 01");
|
|
|
}
|
|
|
if (simNum.length() != LENGTH_2) {
|
|
|
- throw new IllegalArgumentException("buildSendMsg length error");
|
|
|
+ throw new IllegalArgumentException("buildSendMsg error 02");
|
|
|
}
|
|
|
if (cmd.length() != LENGTH_2) {
|
|
|
- throw new IllegalArgumentException("buildSendMsg length error");
|
|
|
+ throw new IllegalArgumentException("buildSendMsg error 03");
|
|
|
}
|
|
|
if (cmdId.length() != LENGTH_2) {
|
|
|
- throw new IllegalArgumentException("buildSendMsg length error");
|
|
|
+ throw new IllegalArgumentException("buildSendMsg error 04");
|
|
|
}
|
|
|
if (data.length() != LENGTH_8) {
|
|
|
- throw new IllegalArgumentException("buildSendMsg length error");
|
|
|
+ throw new IllegalArgumentException("buildSendMsg error 05");
|
|
|
}
|
|
|
StringBuffer m = new StringBuffer();
|
|
|
m.append(PREFIX);
|
|
@@ -92,20 +92,21 @@ public class CommBuildService {
|
|
|
m.append(cmd);
|
|
|
m.append(cmdId);
|
|
|
m.append(data);
|
|
|
+ // 追加CRC校验
|
|
|
if (CRC_16_MODBUS) {
|
|
|
String crcContent = m.toString();
|
|
|
byte[] sendByteContent = CommSendService.hexStrToByteArrs(crcContent);
|
|
|
- l.info("sendByteContent = {}", CommSendService.bytesToHexV2(sendByteContent));
|
|
|
+ // l.info("sendByteContent = {}", CommSendService.bytesToHexV2(sendByteContent));
|
|
|
byte[] sendByteCrc = CRC16Modbus.calculateCRC(sendByteContent);
|
|
|
- l.info("sendByteCrc = {}", CommSendService.bytesToHexV2(sendByteCrc));
|
|
|
+ // l.info("sendByteCrc = {}", CommSendService.bytesToHexV2(sendByteCrc));
|
|
|
String crc = CommSendService.bytesToHexV2(sendByteCrc);
|
|
|
- l.debug("crc: {}", crc.toUpperCase());
|
|
|
+ // l.debug("crc: {}", crc.toUpperCase());
|
|
|
m.append(crc.toUpperCase());
|
|
|
}
|
|
|
m.append(SUFFIX);
|
|
|
final String mFinal = m.toString();
|
|
|
if (mFinal.length() != LENGTH_24) {
|
|
|
- throw new IllegalArgumentException("buildSendMsg length error");
|
|
|
+ throw new IllegalArgumentException("buildSendMsg error 06");
|
|
|
}
|
|
|
smS.setSendMsg(mFinal);
|
|
|
return smS;
|
|
@@ -118,22 +119,39 @@ public class CommBuildService {
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean checkReceiveMsg(String receiveMsg) {
|
|
|
+ // 不能是empty
|
|
|
if (StringUtils.isEmpty(receiveMsg)) {
|
|
|
return false;
|
|
|
}
|
|
|
+ // 长度
|
|
|
if (receiveMsg.length() != LENGTH_24) {
|
|
|
return false;
|
|
|
}
|
|
|
+ // 数据方向
|
|
|
final String orn = StringUtils.substring(receiveMsg, 4, 6);
|
|
|
if (!ORN_RECEIVE.equals(orn)) {
|
|
|
return false;
|
|
|
}
|
|
|
+ // 前缀
|
|
|
if (!StringUtils.startsWith(receiveMsg, PREFIX)) {
|
|
|
return false;
|
|
|
}
|
|
|
+ // 后缀
|
|
|
if (!StringUtils.endsWith(receiveMsg, SUFFIX)) {
|
|
|
return false;
|
|
|
}
|
|
|
+ // 计算CRC16
|
|
|
+ if (false) {
|
|
|
+ String crcContent = receiveMsg.substring(0, 18);
|
|
|
+ l.debug("crcContent: {}", crcContent.toUpperCase());
|
|
|
+ byte[] receiveByteContent = CommSendService.hexStrToByteArrs(crcContent);
|
|
|
+ byte[] receiveByteCrc = CRC16Modbus.calculateCRC(receiveByteContent);
|
|
|
+ String crc = CommSendService.bytesToHexV2(receiveByteCrc);
|
|
|
+ l.debug("crc: {}", crc.toUpperCase());
|
|
|
+// if (!receiveMsg.substring(19, 22).equals(crc.toUpperCase())) {
|
|
|
+// throw new IllegalArgumentException("checkReceiveMsg length error");
|
|
|
+// }
|
|
|
+ }
|
|
|
return true;
|
|
|
}
|
|
|
}
|