|
@@ -0,0 +1,275 @@
|
|
|
+package com.jpsoft.education.modules.common.utils.com;
|
|
|
+
|
|
|
+import java.math.BigInteger;
|
|
|
+import java.text.DecimalFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class ComUtil {
|
|
|
+ //油井A11-----modbus rtu协议解析
|
|
|
+ //读取到油压、套压、回压、井口温度、载荷这些数据
|
|
|
+ public static void read1(){
|
|
|
+ String str="01 03 14 3F 3C 26 E9 3F C1 00 00 00 00 1E B8 41 D7 CC CD 42 0B 00 00 C3 5F";
|
|
|
+ //0103143F3C26E93FC1000000001EB841D7CCCD420B0000C35F
|
|
|
+ str =str.replace(" ","");
|
|
|
+ if(str.startsWith("010314")){
|
|
|
+ String str1= str.substring(6, 14);
|
|
|
+ System.out.println(str1);
|
|
|
+ String str2= str.substring(14, 22);
|
|
|
+ System.out.println(str2);
|
|
|
+ String str3= str.substring(22, 30);
|
|
|
+ System.out.println(str3);
|
|
|
+ String str4= str.substring(30, 38);
|
|
|
+ System.out.println(str4);
|
|
|
+ String str5= str.substring(38, 46);
|
|
|
+ System.out.println(str5);
|
|
|
+ }
|
|
|
+ //System.out.println(str);
|
|
|
+ }
|
|
|
+ //读取第6项到第17项的数据
|
|
|
+ public static void read2(){
|
|
|
+ String str="01 03 30 41 E9 C0 00 41 E7 00 00 3D 00 28 00 43 65 08 00 43 66 70 00 43 65 35 88 48 35 8F 85 48 EE A8 88 40 83 CD F5 41 45 00 00 00 00 85 37 3E 99 00 00 C6 3A ";
|
|
|
+ //0103143F3C26E93FC1000000001EB841D7CCCD420B0000C35F
|
|
|
+ str =str.replace(" ","");
|
|
|
+ if(str.startsWith("010330")){
|
|
|
+ String str1= str.substring(6, 14);
|
|
|
+ System.out.println(str1);
|
|
|
+ String str2= str.substring(14, 22);
|
|
|
+ System.out.println(str2);
|
|
|
+ String str3= str.substring(22, 30);
|
|
|
+ System.out.println(str3);
|
|
|
+ String str4= str.substring(30, 38);
|
|
|
+ System.out.println(str4);
|
|
|
+ String str5= str.substring(38, 46);
|
|
|
+ System.out.println(str5);
|
|
|
+
|
|
|
+ String str6= str.substring(46, 54);
|
|
|
+ System.out.println(str6);
|
|
|
+ String str7= str.substring(54, 62);
|
|
|
+ System.out.println(str7);
|
|
|
+ String str8= str.substring(62, 70);
|
|
|
+ System.out.println(str8);
|
|
|
+ String str9= str.substring(70, 78);
|
|
|
+ System.out.println(str9);
|
|
|
+ String str10= str.substring(78, 86);
|
|
|
+ System.out.println(str10);
|
|
|
+
|
|
|
+ String str11= str.substring(86, 94);
|
|
|
+ System.out.println(str11);
|
|
|
+ String str12= str.substring(94, 102);
|
|
|
+ System.out.println(str12);
|
|
|
+ }
|
|
|
+ //System.out.println(str);
|
|
|
+ }
|
|
|
+//冲次和冲程
|
|
|
+ public static void read3(){
|
|
|
+ String str="01 03 08 40 9B 85 1F 40 3B 00 00 55 74";
|
|
|
+ //0103143F3C26E93FC1000000001EB841D7CCCD420B0000C35F
|
|
|
+ str =str.replace(" ","");
|
|
|
+ if(str.startsWith("010308")){
|
|
|
+ String str1= str.substring(6, 14);
|
|
|
+ System.out.println(str1);
|
|
|
+ String str2= str.substring(14, 22);
|
|
|
+ System.out.println(str2);
|
|
|
+
|
|
|
+ }
|
|
|
+ //System.out.println(str);
|
|
|
+ }
|
|
|
+ //长度100
|
|
|
+ public static void read4(){
|
|
|
+ String str="010364119B11C011D111EB1200120711D111EB12001207119B11C011D111EB1200120711D111EB12001207119B11C011D111EB1200120711D111EB12001207119B11C011D111EB1200120711D111EB12001207119B11C011D111EB1200120711D111EB12001207119B11C011D111EB1200120711D111EB12001207119B11C011D111EB1200120711D111EB12001207119B11C011D111EB1200120711D111EB12001207119B11C011D111EB1200120711D111EB12001207119B11C011D111EB1200120711D111EB12001207C35F";
|
|
|
+ str =str.replace(" ","");
|
|
|
+ if(str.startsWith("010364")&&str.endsWith("C35F")){
|
|
|
+ str= str.substring(6, str.length()-4);
|
|
|
+ System.out.println(str);
|
|
|
+ int allLength =str.length();
|
|
|
+ System.out.println(allLength);
|
|
|
+ int startStrPos = 0;
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ int k=0;
|
|
|
+ while (startStrPos < allLength) {
|
|
|
+ String strOrg="";
|
|
|
+ if(k==0){
|
|
|
+ strOrg= str.substring(0,4);
|
|
|
+ }else{
|
|
|
+ strOrg =str.substring(startStrPos,startStrPos+4);
|
|
|
+ }
|
|
|
+ k++;
|
|
|
+ startStrPos=startStrPos+4;
|
|
|
+ System.out.println(startStrPos);
|
|
|
+ list.add(strOrg);
|
|
|
+ }
|
|
|
+
|
|
|
+ int m=0;
|
|
|
+ for(String strD:list){
|
|
|
+ m++;
|
|
|
+ System.out.println(m+"---"+strD);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //长度50
|
|
|
+ public static void read5(){
|
|
|
+ String str="010332119B11C011D111EB1200120711D111EB12001207119B11C011D111EB1200120711D111EB12001207119B11C011D111EB1200120711D111EB12001207119B11C011D111EB1200120711D111EB12001207119B11C011D111EB1200120711D111EB12001207C35F";
|
|
|
+ str =str.replace(" ","");
|
|
|
+ if(str.startsWith("010332")&&str.endsWith("C35F")){
|
|
|
+ str= str.substring(6, str.length()-4);
|
|
|
+ System.out.println(str);
|
|
|
+ int allLength =str.length();
|
|
|
+ System.out.println(allLength);
|
|
|
+ int startStrPos = 0;
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ int k=0;
|
|
|
+ while (startStrPos < allLength) {
|
|
|
+ String strOrg="";
|
|
|
+ if(k==0){
|
|
|
+ strOrg= str.substring(0,4);
|
|
|
+ }else{
|
|
|
+ strOrg =str.substring(startStrPos,startStrPos+4);
|
|
|
+ }
|
|
|
+ k++;
|
|
|
+ startStrPos=startStrPos+4;
|
|
|
+ System.out.println(startStrPos);
|
|
|
+ list.add(strOrg);
|
|
|
+ }
|
|
|
+
|
|
|
+ int m=0;
|
|
|
+ for(String strD:list){
|
|
|
+ m++;
|
|
|
+ System.out.println(m+"---"+strD+"---"+Integer.parseInt(strD,16));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * int数组转以两个为一组,一次转换成一个个的浮点数,并打印
|
|
|
+ * @param arry
|
|
|
+ */
|
|
|
+ public static void convertFloat(int arry[]){
|
|
|
+ for (int i = 0; i < arry.length; i++) {
|
|
|
+ if (i == 1) {
|
|
|
+ byte bytes1[] = new byte[2];
|
|
|
+ //bytes1 = int2Bytes(arry[0], 2);
|
|
|
+ bytes1 = Int2Bytes_LE(arry[0]);
|
|
|
+
|
|
|
+ byte bytes2[] = new byte[2];
|
|
|
+ //bytes2 = int2Bytes(arry[1], 2);
|
|
|
+ bytes2 = Int2Bytes_LE(arry[1]);
|
|
|
+
|
|
|
+ byte bytes[] = new byte[4];
|
|
|
+ bytes[0]=bytes1[0];
|
|
|
+ bytes[1]=bytes1[1];
|
|
|
+ bytes[2]=bytes2[0];
|
|
|
+ bytes[3]=bytes2[1];
|
|
|
+ System.out.println(hex(bytes));
|
|
|
+ System.out.println(sfloat(hex(bytes)));
|
|
|
+ } else {
|
|
|
+ if ((i+1)%2==0) {
|
|
|
+ byte bytes1[] = new byte[2];
|
|
|
+ bytes1 = Int2Bytes_LE(arry[i-1]);
|
|
|
+ byte bytes2[] = new byte[2];
|
|
|
+ bytes2 = Int2Bytes_LE(arry[i]);
|
|
|
+
|
|
|
+ byte bytes[] = new byte[4];
|
|
|
+ bytes[0]=bytes1[0];
|
|
|
+ bytes[1]=bytes1[1];
|
|
|
+ bytes[2]=bytes2[0];
|
|
|
+ bytes[3]=bytes2[1];
|
|
|
+ System.out.println(hex(bytes));
|
|
|
+ System.out.println(sfloat(hex(bytes)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转小端,返回2个字节数组
|
|
|
+ * @param iValue
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static byte[] Int2Bytes_LE(int iValue){
|
|
|
+ byte[] rst = new byte[2];
|
|
|
+ // 先写int的最后一个字节
|
|
|
+ rst[0] = (byte)(iValue & 0xFF);
|
|
|
+ // int 倒数第二个字节
|
|
|
+ rst[1] = (byte)((iValue & 0xFF00) >> 8 );
|
|
|
+ return rst;
|
|
|
+ }
|
|
|
+ //转大端返回4个字节数组
|
|
|
+ public static byte[] intToByteBig(int n) {
|
|
|
+ byte[] b = new byte[4];
|
|
|
+ b[0] = (byte) (n >> 24 & 0xff);
|
|
|
+ b[1] = (byte) (n >> 16 & 0xff);
|
|
|
+ b[2] = (byte) (n >> 8 & 0xff);
|
|
|
+ b[3] = (byte) (n & 0xff);
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+ //转小端返回4个字节数组
|
|
|
+ public static byte[] intToByteLittle(int n) {
|
|
|
+ byte[] b = new byte[4];
|
|
|
+ b[0] = (byte) (n & 0xff);
|
|
|
+ b[1] = (byte) (n >> 8 & 0xff);
|
|
|
+ b[2] = (byte) (n >> 16 & 0xff);
|
|
|
+ b[3] = (byte) (n >> 24 & 0xff);
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static String sfloat(String str){
|
|
|
+ int count = str.length()/2;
|
|
|
+ String last = "";
|
|
|
+ for(int i = count-1;i>=0;i--){
|
|
|
+ String one = str.substring(i*2,(i+1)*2);
|
|
|
+ last+=one;
|
|
|
+ }
|
|
|
+ if(!"".equals(last)) {
|
|
|
+ Float f = Float.intBitsToFloat(new BigInteger(last, 16).intValue());
|
|
|
+ //将科学计算值转可以看懂的数值
|
|
|
+ DecimalFormat fnum = new DecimalFormat("##0.000000");
|
|
|
+ String dd = fnum.format(f);
|
|
|
+ return dd;
|
|
|
+ }
|
|
|
+ return "--";
|
|
|
+ }
|
|
|
+ //将一个int数转换成几个字节数组
|
|
|
+ public static byte[] int2Bytes(int value, int len) {
|
|
|
+ byte b[] = new byte[len];
|
|
|
+ for (int i = 0; i < len; i++) {
|
|
|
+ b[len - i - 1] = (byte) ((value >> 8 * i) & 0xff);
|
|
|
+ }
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String hex(byte[] bytes) {
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+ for (byte aByte : bytes) {
|
|
|
+ result.append(String.format("%02x", aByte));
|
|
|
+ }
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ //read5();
|
|
|
+ int arry[] = new int[14];
|
|
|
+ arry[0] = 58196;
|
|
|
+ arry[1] = 16763;
|
|
|
+ arry[2] = 2097;
|
|
|
+ arry[3] = 15532;
|
|
|
+ arry[4] = 524;
|
|
|
+ arry[5] = 16611;
|
|
|
+ arry[6] = 39846;
|
|
|
+ arry[7] = 15300;
|
|
|
+ arry[8] = 35127;
|
|
|
+ arry[9] = 16621;
|
|
|
+ arry[10] = 34079;
|
|
|
+ arry[11] = 16907;
|
|
|
+ arry[12] = 11633;
|
|
|
+ arry[13] = 17435;
|
|
|
+ //convertFloat(arry);
|
|
|
+ System.out.println(sfloat("1f850b42"));
|
|
|
+ System.out.println(sfloat("3F3C26E9"));
|
|
|
+ System.out.println(sfloat("3FC10000"));
|
|
|
+ System.out.println(sfloat("00001EB8"));
|
|
|
+ System.out.println(sfloat("41D7CCCD"));
|
|
|
+ System.out.println(sfloat("420B0000"));
|
|
|
+ }
|
|
|
+}
|