| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- package com.mes.ygsl;
- import io.netty.buffer.ByteBuf;
- import io.netty.buffer.Unpooled;
- public class YgslUtil {
- // 心跳
- public static Boolean comHeart(YgslClient ygslClient){
- try{
- String synr_str = "002099990050 ";
- ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
- return true;
- }catch (Exception e){
- e.printStackTrace();
- return false;
- }
- }
- // 通讯启动 对应回复 0002 通讯启动确认
- public static Boolean comStart(YgslClient ygslClient){
- try{
- String synr_str = "002000010050 ";
- ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
- return true;
- }catch (Exception e){
- e.printStackTrace();
- return false;
- }
- }
- // 通讯停止 对应回复 0005 命令被接受 0004 命令错误
- public static Boolean comStop(YgslClient ygslClient){
- try{
- String synr_str = "002000030050 ";
- ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
- return true;
- }catch (Exception e){
- e.printStackTrace();
- return false;
- }
- }
- // 使能工具 回复 0004/0005
- public static Boolean enableTool(YgslClient ygslClient){
- try{
- String synr_str = "00200043000 ";
- ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
- return true;
- }catch (Exception e){
- e.printStackTrace();
- return false;
- }
- }
- // 禁用工具 回复 0004/0005
- public static Boolean disableTool(YgslClient ygslClient){
- try{
- String synr_str = "00200042000 ";
- ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
- return true;
- }catch (Exception e){
- e.printStackTrace();
- return false;
- }
- }
- //上次拧紧结果数据订阅 0061 拧紧数据上报
- public static Boolean lastTighteningResultSubscribe(YgslClient ygslClient){
- try{
- String synr_str = "002000600010 ";
- System.out.println("lastTighteningResultSubscribe:"+synr_str);
- System.out.println(stringToHex(synr_str));
- ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
- return true;
- }catch (Exception e){
- e.printStackTrace();
- return false;
- }
- }
- // 接收到订阅拧紧数据后回复
- public static Boolean lastTighteningResultDataAcknowledge(YgslClient ygslClient){
- try{
- String synr_str = "0020006200010 ";
- System.out.println("接收到订阅拧紧数据后回复");
- System.out.println("lastTighteningResultDataAcknowledge:"+synr_str);
- System.out.println(stringToHex(synr_str));
- ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
- return true;
- }catch (Exception e){
- e.printStackTrace();
- return false;
- }
- }
- // 设置setId 对应回复 0010 pset
- public static Boolean pSet(YgslClient ygslClient){
- try{
- String synr_str = "002000100010 ";
- System.out.println("pSet:"+synr_str);
- ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
- return true;
- }catch (Exception e){
- e.printStackTrace();
- return false;
- }
- }
- // 选择 setId 0018 pset
- public static Boolean selectSet(YgslClient ygslClient){
- try{
- String synr_str = "002300180010 001";
- System.out.println("selectSet:"+synr_str);
- ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
- return true;
- }catch (Exception e){
- e.printStackTrace();
- return false;
- }
- }
- public static Boolean jobIdUpload(YgslClient ygslClient){
- try{
- String synr_str = "002000300010 ";
- System.out.println("jobIdUpload:"+synr_str);
- ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
- return true;
- }catch (Exception e){
- e.printStackTrace();
- return false;
- }
- }
- public static Boolean selectJob(YgslClient ygslClient){
- try{
- String synr_str = "002000380010 01";
- System.out.println("selectJob:"+synr_str);
- ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
- return true;
- }catch (Exception e){
- e.printStackTrace();
- return false;
- }
- }
- public static Boolean restartJob(YgslClient ygslClient){
- try{
- String synr_str = "002000390010 01";
- System.out.println("restartJob:"+synr_str);
- ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
- return true;
- }catch (Exception e){
- e.printStackTrace();
- return false;
- }
- }
- public static String stringToHex(String input) {
- StringBuilder hexString = new StringBuilder();
- for (char ch : input.toCharArray()) {
- hexString.append(Integer.toHexString((int) ch));
- }
- return hexString.toString();
- }
- /**
- * 将字符串消息转为ByteBuf
- * 16进制字符串转二进制
- * @param hex 字符串消息
- */
- public static ByteBuf getSendByteBuf(String hex) {
- hex = hex + "00";
- byte[] bytes = new byte[hex.length() / 2];
- for (int i = 0; i < hex.length(); i += 2) {
- int value = Integer.parseInt(hex.substring(i, i + 2), 16);
- bytes[i/2] = (byte)(value & 0xFF);
- }
- return Unpooled.wrappedBuffer(bytes);
- }
- }
|