| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- package com.jeesite.modules.utils;
- import com.jeesite.common.lang.StringUtils;
- // 固定格式报文各参数获取方法
- // 协议 v2(2026-06-29 修订):oprno 字段从 6 字符扩到 8 字符,后续字段偏移整体 +2
- public class ProtocolParam {
- public static Integer fixedLength = 126; // 固定长度(v1=124,v2 扩 oprno 6→8 后 +2)
- // 获取消息类型 所有报文都可使用
- public static String getMsgType(String msg){
- System.out.print(msg);
- if(msg.length() < 16){
- return "";
- }
- return msg.substring(12,16);
- }
- // 获取工位号(8 字符,原 6 字符)
- public static String getOprno(String msg){
- if(msg.length() < 26){
- return "";
- }
- return msg.substring(18,26);
- }
- // 获取工艺号
- public static String getCraft(String msg){
- if(msg.length() < 34){
- return "";
- }
- return msg.substring(28,34);
- }
- // 获取类型
- public static String getLx(String msg){
- if(msg.length() < 38){
- return "";
- }
- return msg.substring(36,38);
- }
- // 获取返回类型
- public static String getReturnLx(String msg){
- if(msg.length() < 38){
- return "RS";
- }
- String lx = msg.substring(36,38);
- return StringUtils.isEmpty(lx.trim())?"RS":lx;
- }
- // 获取产线编号
- public static String getLineSn(String msg){
- if(msg.length() < 40){
- return "";
- }
- return msg.substring(38,40);
- }
- // 获取镭雕码或设备报警故障代码
- public static String getSn(String msg){
- if(msg.length() < 76){
- return "";
- }
- return msg.substring(40,76);
- }
- // 获取结果
- public static String getResult(String msg){
- if(msg.length() < 80){
- return "";
- }
- return msg.substring(78,80);
- }
- // 获取日期
- public static String getDay(String msg){
- if(msg.length() < 92){
- return "";
- }
- return msg.substring(82,92);
- }
- // 获取时间
- public static String getTime(String msg){
- if(msg.length() < 102){
- return "";
- }
- return msg.substring(94,102);
- }
- // 获取用户
- public static String getUserCode(String msg){
- if(msg.length() < 124){
- return "";
- }
- return msg.substring(104,124).trim();
- }
- // 获取参数个数
- public static Integer getParamNums(String msg){
- if(msg.length() < fixedLength){
- return 0;
- }
- String nn = msg.substring(124,fixedLength).trim();
- if(StringUtils.isEmpty(nn)){
- return 0;
- }
- return Integer.parseInt(nn);
- }
- // 获取参数的总长度
- public static Integer getParamLenth(String msg){
- if(msg.length() < fixedLength){
- return 0;
- }
- Integer length = getParamNums(msg);
- if(length == 0){
- return 0;
- }
- String msgType = getMsgType(msg);
- // String cate = getProductCate(msg);
- // String oprno = getOprno(msg);
- Integer res = 0;
- if(msgType.equals("MBDW") || msgType.equals("MJBW") || msgType.equals("VLDW") || msgType.equals("AQDW") || msgType.equals("MCJW")||msgType.equals("MKSW")){ // 绑定报文,截取36个字符
- if(msg.length() >= fixedLength + length*36){
- res = length*36;
- }
- }else if(msgType.equals("MSBW")){ // 设备状态,9*26=234
- if(msg.length() >= fixedLength + length*4){
- res = length*4;
- }
- }else{
- if(msg.length() >= fixedLength + length*12){
- res = length*12;
- }
- }
- return res;
- }
- // 获取参数 num从1开始
- public static String getParam(String msg,Integer num){
- if(msg.length() < fixedLength){
- return "";
- }
- Integer length = getParamNums(msg);
- if(length == 0){
- return "";
- }
- if(num > length){
- return "";
- }
- String msgType = getMsgType(msg);
- String res = "";
- if(msgType.equals("MBDW")||msgType.equals("MJBW")||msgType.equals("VLDW")||msgType.equals("AQDW")||msgType.equals("MCJW")||msgType.equals("MKSW")){ // 绑定报文,截取36个字符
- if(msg.length() >= fixedLength + length*36){
- res = msg.substring(fixedLength+(num-1)*36,fixedLength + num*36);
- }
- }else if(msgType.equals("MSBW")){ // 设备状态,9*26=234
- if(msg.length() >= fixedLength + length*4){
- // res = msg.substring(fixedLength,fixedLength + 234);
- res = msg.substring(fixedLength+(num-1)*4,fixedLength + num*4);
- }
- }else{
- if(msg.length() >= fixedLength + length*12){
- res = msg.substring(fixedLength+(num-1)*12,fixedLength + num*12);
- }
- }
- return res;
- }
- public static String getParamResult(String param){
- if(param.length() >= 1){
- String ret = param.substring(0,1).trim();
- if(ret.equals("Y")){
- return "OK";
- }
- }
- return "NG";
- }
- public static String getParamVal(String param){
- if(param.length() > 1){
- return param.substring(1,param.length()).trim();
- }
- return "";
- }
- }
|