YgslUtil.java 7.0 KB

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