YtUtil.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package com.mes.util.ytnetty;
  2. import io.netty.buffer.ByteBuf;
  3. import io.netty.buffer.Unpooled;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. public class YtUtil {
  7. public static final Logger log = LoggerFactory.getLogger(YtUtil.class);
  8. // 获取功能码
  9. public static String getCode(String msg){
  10. return msg.substring(0,4);
  11. }
  12. /**
  13. * 计算16进制字符串的BCC校验码
  14. * @param hexData 16进制字符串
  15. * @return BCC校验码
  16. */
  17. public static byte calculateBCC(String hexData) {
  18. byte bcc = 0; // 初始化BCC校验码为0
  19. for (int i = 0; i < hexData.length(); i += 2) { // 每次处理两个字符(1个字节)
  20. String byteStr = hexData.substring(i, i + 2); // 获取当前16进制字节
  21. int byteValue = Integer.parseInt(byteStr, 16); // 将16进制字符串转换为整数
  22. bcc ^= byteValue; // 执行异或操作
  23. }
  24. return bcc; // 返回计算后的BCC校验码
  25. }
  26. public static String formatCommon(String msg){
  27. String start = "02";
  28. String end = "03";
  29. String content = start+msg+end;
  30. String bcc = String.format("%02x",calculateBCC(content));
  31. return content + bcc;
  32. }
  33. // 启动指令
  34. public static String getStartCommon(){
  35. return formatCommon("5354");
  36. }
  37. // 停止/复位指令
  38. public static String getStopCommon(){
  39. return formatCommon("5254");
  40. }
  41. // 锁机指令
  42. public static String getLockCommon(){
  43. return formatCommon("4C4D31");
  44. }
  45. // 解锁指令
  46. public static String getUnLockCommon(){
  47. return formatCommon("4C4D30");
  48. }
  49. // 选频指令
  50. public static String getSelectProgramCommon(String programNo){
  51. String no = String.format("%03d", Integer.parseInt(programNo));
  52. return formatCommon("4348"+asciiToHex(no));
  53. }
  54. // 读取当前通道
  55. public static String getCurProgramCommon(){
  56. return formatCommon("5250");
  57. }
  58. // 读取实时
  59. public static String getCurDataCommon(){
  60. return formatCommon("5253");
  61. }
  62. // 设备状态
  63. public static String getStateCommon(){
  64. return formatCommon("5453");
  65. }
  66. // 传输结果
  67. public static String getResultCommon(){
  68. return formatCommon("4353");
  69. }
  70. public static String asciiToHex(String input){
  71. StringBuilder hexValues = new StringBuilder();
  72. for (char c : input.toCharArray()) {
  73. int asciiValue = (int) c; // 获取ASCII值
  74. String hexValue = Integer.toHexString(asciiValue); // 转换为16进制字符串
  75. if (hexValues.length() > 0) { // 如果不是第一个字符,添加空格分隔
  76. hexValues.append("");
  77. }
  78. hexValues.append(hexValue); // 添加到StringBuilder中
  79. }
  80. return hexValues.toString();
  81. }
  82. public static byte[] hexStringToByteArray(String s) {
  83. int len = s.length();
  84. byte[] data = new byte[len / 2];
  85. for (int i = 0; i < len; i += 2) {
  86. data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
  87. + Character.digit(s.charAt(i+1), 16));
  88. }
  89. return data;
  90. }
  91. /**
  92. * 将字符串消息转为ByteBuf
  93. * 16进制字符串转二进制
  94. * @param hex 字符串消息
  95. */
  96. public static ByteBuf getSendByteBuf(String hex) {
  97. byte[] bytes = new byte[hex.length() / 2];
  98. for (int i = 0; i < hex.length(); i += 2) {
  99. int value = Integer.parseInt(hex.substring(i, i + 2), 16);
  100. bytes[i/2] = (byte)(value & 0xFF);
  101. }
  102. return Unpooled.wrappedBuffer(bytes);
  103. }
  104. }