/** * ***************************** 获取需要的协议信息,封装成MyDParam * * @param markValue * @return */ private List<MyDParam> getDParams(List<DParam> dParams) { List<MyDParam> list = new ArrayList<MyDParam>(); if (dParams.size() > 0) { for (int j = 0; j < dParams.size(); j++) { MyDParam myDParam = new MyDParam(); DParam dParam = dParams.get(j); myDParam.setdParam(dParam); Map<Integer, DparamMapValue> map = stringTransformMap(dParam.getDparamIsMap(), dParam.getDparamMapDatas()); myDParam.setDmv(map); list.add(myDParam); } } return list; }
/** * ******************** 获得协议参数 * * @param datas * @param dParam * @return */ public float getParam(byte[] datas, MyDParam myDParam) { DParam dParam = myDParam.getdParam(); int byteBegin = dParam.getDparamByteBegin(); int byteLength = dParam.getDparamByteLength(); int isByte = dParam.getDparamIsByte(); float factory = dParam.getDparamFactor(); // 参数分辨率(倍数) float offset = dParam.getDparamOffset(); // 参数偏移值 String byteDatas = dParam.getDparamByteDatas(); float param = 0; if ((byteBegin + byteLength) <= datas.length) { byte[] dataArray = cutDatas(datas, byteBegin, byteLength); // 截取datas if (isByte == 0) { float tmpFloat = 0; if (dParam.getDparamHighAndLow() == 1) { // 大端法解析 tmpFloat = byteTransformFloat_big(dataArray); } else { // 小端法解析 tmpFloat = byteTransformFloat_small(dataArray); } if (tmpFloat == 0) { param = 0; } else { param = Math.round((tmpFloat * factory + offset) * 10) / 10; } } else { String[] bytedataArray = byteDatas.split(":"); if (bytedataArray.length >= 4) { int bytelen = Integer.parseInt(bytedataArray[0]); int bytebeginbit = Integer.parseInt(bytedataArray[1]); int byteendbit = Integer.parseInt(bytedataArray[2]) + bytebeginbit; int bytefactor = Integer.parseInt(bytedataArray[3]); int tmpInt = 0; for (int i = Math.min(dataArray.length, bytelen) - 1; i >= 0; --i) { tmpInt |= dataArray[i]; tmpInt = tmpInt << 4 * i; } String tmpString = toBinaryString(tmpInt); if (tmpString.length() - byteendbit < 0 || tmpString.length() - bytebeginbit < 0) { return 0; } tmpString = tmpString.substring( tmpString.length() - byteendbit, tmpString.length() - bytebeginbit); param = (float) (new BigInteger(tmpString, 2).intValue() * bytefactor); } } } else { param = 0; } return param; }