YgslUtil.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package com.mes.ygsl;
  2. import io.netty.buffer.ByteBuf;
  3. import io.netty.buffer.Unpooled;
  4. public class YgslUtil {
  5. // 心跳
  6. public static Boolean comHeart(YgslClient ygslClient){
  7. try{
  8. String synr_str = "002099990050 ";
  9. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  10. return true;
  11. }catch (Exception e){
  12. e.printStackTrace();
  13. return false;
  14. }
  15. }
  16. // 通讯启动 对应回复 0002 通讯启动确认
  17. public static Boolean comStart(YgslClient ygslClient){
  18. try{
  19. String synr_str = "002000010050 ";
  20. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  21. return true;
  22. }catch (Exception e){
  23. e.printStackTrace();
  24. return false;
  25. }
  26. }
  27. // 通讯停止 对应回复 0005 命令被接受 0004 命令错误
  28. public static Boolean comStop(YgslClient ygslClient){
  29. try{
  30. String synr_str = "002000030050 ";
  31. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  32. return true;
  33. }catch (Exception e){
  34. e.printStackTrace();
  35. return false;
  36. }
  37. }
  38. // 使能工具 回复 0004/0005
  39. public static Boolean enableTool(YgslClient ygslClient){
  40. try{
  41. String synr_str = "00200043000 ";
  42. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  43. return true;
  44. }catch (Exception e){
  45. e.printStackTrace();
  46. return false;
  47. }
  48. }
  49. // 禁用工具 回复 0004/0005
  50. public static Boolean disableTool(YgslClient ygslClient){
  51. try{
  52. String synr_str = "00200042000 ";
  53. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  54. return true;
  55. }catch (Exception e){
  56. e.printStackTrace();
  57. return false;
  58. }
  59. }
  60. //上次拧紧结果数据订阅 0061 拧紧数据上报
  61. public static Boolean lastTighteningResultSubscribe(YgslClient ygslClient){
  62. try{
  63. String synr_str = "002000600010 ";
  64. System.out.println("lastTighteningResultSubscribe:"+synr_str);
  65. System.out.println(stringToHex(synr_str));
  66. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  67. return true;
  68. }catch (Exception e){
  69. e.printStackTrace();
  70. return false;
  71. }
  72. }
  73. // 接收到订阅拧紧数据后回复
  74. public static Boolean lastTighteningResultDataAcknowledge(YgslClient ygslClient){
  75. try{
  76. String synr_str = "0020006200010 ";
  77. System.out.println("接收到订阅拧紧数据后回复");
  78. System.out.println("lastTighteningResultDataAcknowledge:"+synr_str);
  79. System.out.println(stringToHex(synr_str));
  80. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  81. return true;
  82. }catch (Exception e){
  83. e.printStackTrace();
  84. return false;
  85. }
  86. }
  87. // 设置setId 对应回复 0010 pset
  88. public static Boolean pSet(YgslClient ygslClient){
  89. try{
  90. String synr_str = "002000100010 ";
  91. System.out.println("pSet:"+synr_str);
  92. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  93. return true;
  94. }catch (Exception e){
  95. e.printStackTrace();
  96. return false;
  97. }
  98. }
  99. // 选择 setId 0018 pset
  100. public static Boolean selectSet(YgslClient ygslClient){
  101. try{
  102. String synr_str = "002300180010 001";
  103. System.out.println("selectSet:"+synr_str);
  104. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  105. return true;
  106. }catch (Exception e){
  107. e.printStackTrace();
  108. return false;
  109. }
  110. }
  111. public static Boolean jobIdUpload(YgslClient ygslClient){
  112. try{
  113. String synr_str = "002000300010 ";
  114. System.out.println("jobIdUpload:"+synr_str);
  115. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  116. return true;
  117. }catch (Exception e){
  118. e.printStackTrace();
  119. return false;
  120. }
  121. }
  122. public static Boolean selectJob(YgslClient ygslClient){
  123. try{
  124. String synr_str = "002000380010 01";
  125. System.out.println("selectJob:"+synr_str);
  126. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  127. return true;
  128. }catch (Exception e){
  129. e.printStackTrace();
  130. return false;
  131. }
  132. }
  133. public static Boolean restartJob(YgslClient ygslClient){
  134. try{
  135. String synr_str = "002000390010 01";
  136. System.out.println("restartJob:"+synr_str);
  137. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  138. return true;
  139. }catch (Exception e){
  140. e.printStackTrace();
  141. return false;
  142. }
  143. }
  144. public static String stringToHex(String input) {
  145. StringBuilder hexString = new StringBuilder();
  146. for (char ch : input.toCharArray()) {
  147. hexString.append(Integer.toHexString((int) ch));
  148. }
  149. return hexString.toString();
  150. }
  151. /**
  152. * 将字符串消息转为ByteBuf
  153. * 16进制字符串转二进制
  154. * @param hex 字符串消息
  155. */
  156. public static ByteBuf getSendByteBuf(String hex) {
  157. hex = hex + "00";
  158. byte[] bytes = new byte[hex.length() / 2];
  159. for (int i = 0; i < hex.length(); i += 2) {
  160. int value = Integer.parseInt(hex.substring(i, i + 2), 16);
  161. bytes[i/2] = (byte)(value & 0xFF);
  162. }
  163. return Unpooled.wrappedBuffer(bytes);
  164. }
  165. }