| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- package com.jeesite.modules;
- import com.alibaba.fastjson.JSON;
- //import com.github.xingshuangs.iot.common.buff.EByteBuffFormat;
- import com.github.xingshuangs.iot.protocol.common.buff.EByteBuffFormat;
- import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.charset.Charset;
- import java.util.List;
- public class Modbus {
- public static void main(String[] args) {
- // ModbusTcp modbusTcp = new ModbusTcp(2, "192.168.1.211",8000);
- // List<Boolean> list = modbusTcp.readCoil(26,1);
- // System.err.println("6002:"+list.toString());
- ModbusTcp modbusTcp = new ModbusTcp(1,"192.168.1.91");
- modbusTcp.writeInt16(8,(short)0);
- Short value = modbusTcp.readInt16(8);
- System.out.println(value);
- // ModbusTcp modbusTcp = new ModbusTcp(1,"192.168.88.99");
- // modbusTcp.writeInt32(15000*2,0, EByteBuffFormat.DC_BA);
- //
- // Integer ret3 = modbusTcp.readInt32(15000*2,EByteBuffFormat.DC_BA);
- // System.out.println(ret3);
- // Integer ret33 = modbusTcp.readInt32(15001*2,EByteBuffFormat.DC_BA);
- // System.out.println(ret33);
- // Integer ret333 = modbusTcp.readInt32(15004*2,EByteBuffFormat.DC_BA);
- // System.out.println(ret333);
- // Integer ret = modbusTcp.readInt32(58*2, EByteBuffFormat.DC_BA);
- // modbusTcp.writeInt32(6002*2,1, EByteBuffFormat.DC_BA);
- // modbusTcp.writeInt32(58*2, 0);
- // Integer ret = modbusTcp.readInt32(6000*2, EByteBuffFormat.DC_BA);
- // System.err.println("6001:"+ret);
- //
- // Integer ret2 = modbusTcp.readInt32(6002*2, EByteBuffFormat.DC_BA);
- // System.err.println("6002:"+ret2);
- // ModbusTcp plc = new ModbusTcp(1,"192.168.20.21");
- //// System.out.println("plc:"+plc.readCoil(99,1));
- ////
- // int val = plc.readInt32(99, EByteBuffFormat.DC_BA);
- ////
- //
- // System.out.println("转换后的整数:" + val);
- //// int val = plc.readInt32(100, EByteBuffFormat.AB_CD);
- //
- // String binaryString = String.format("%16s", Integer.toBinaryString(val & 0xFF)).replace(' ', '0');
- // System.out.println("Binary string: " + binaryString);
- // System.out.println("0: " + getCharByPosition(binaryString,0));
- // System.out.println("1: " + getCharByPosition(binaryString,1));
- // System.out.println("2: " + getCharByPosition(binaryString,2));
- // System.out.println("3: " + getCharByPosition(binaryString,3));
- // System.out.println("4: " + getCharByPosition(binaryString,4));
- // System.out.println("5: " + getCharByPosition(binaryString,5));
- // System.out.println("6: " + getCharByPosition(binaryString,6));
- // System.out.println("7: " + getCharByPosition(binaryString,7));
- //
- // int value = Integer.parseInt(binaryString, 2);
- // System.out.println("转换后的整数:" + value);
- //
- // String newBinaryString = setCharByPosition(binaryString,0,'0');
- // System.out.println("newBinaryString: " + newBinaryString);
- //
- // int value2 = Integer.parseInt(newBinaryString, 2);
- // System.out.println("转换后的整数2:" + value2);
- //
- //// plc.writeInt32(99,value2, EByteBuffFormat.DC_BA);
- //
- // System.out.println("D100.0: " + getCharByPosition(binaryString, 0));
- // System.out.println("D100.1: " + getCharByPosition(binaryString, 1));
- // System.out.println("D100.2: " + getCharByPosition(binaryString, 2));
- // System.out.println("D100.3: " + getCharByPosition(binaryString, 3));
- // System.out.println("D100.4: " + getCharByPosition(binaryString, 4));
- // System.out.println("D100.5: " + getCharByPosition(binaryString, 5));
- // System.out.println("D100.6: " + getCharByPosition(binaryString, 6));
- // System.out.println("D100.7: " + getCharByPosition(binaryString, 7));
- // double ww = Double.valueOf("1.1");
- // System.out.println("ww:"+ww);
- // ModbusTcp plc = new ModbusTcp(1,"192.168.1.211",8000);
- //
- // plc.writeCoil(8217, true);
- //
- // System.out.println("plc:"+plc.readCoil(200,1));
- //
- // int D500 = plc.readInt32(102);//500=>100
- // int D208 = plc.readInt32(208);
- //
- // System.out.println("D500:"+D500);
- //
- // System.out.println("D208:"+D208);
- // ModbusTcp plc = new ModbusTcp("192.168.1.18");
- // 单枪
- // plc.writeCoil(3328,true);
- // plc.writeCoil(3079,false);
- // plc.writeCoil(3328,true); // 必须选关掉关机,开机才起左右
- // plc.writeCoil(3128,true);
- // plc.writeCoil(3128,false);
- // plc.writeInt16(1128,(short) 3);
- // plc.writeInt16(1136,(short) 0);
- // plc.writeInt16(1138,(short) 0);
- // plc.writeCoil(3128,true);
- // // 设置模式 1=标记铆接模式
- // plc.writeInt16(1092,(short) 1);
- // // 设置任务
- // plc.writeInt16(1128,(short) 80);
- // plc.writeInt16(1136,(short) 2);
- // plc.writeInt16(1138,(short) 1);
- //
- // plc.writeCoil(3128,true);
- //
- //
- // System.out.println("任务数量:"+plc.readInt16(1128)); // 1129 任务数量
- // System.out.println("已铆接数量:"+plc.readInt16(1136)); // 1137 任务数量
- // System.out.println("合格数量:"+plc.readInt16(1138)); // 1139 合格数量
- //
- // System.out.println("任务数量2:"+plc.readInt16(1126)); // 1129 任务数量
- // System.out.println("已铆接数量2:"+plc.readInt16(1144)); // 1137 任务数量
- // System.out.println("合格数量2:"+plc.readInt16(1146)); // 1139 合格数量
- // System.out.println("合格数量2:"+plc.readInt16(1160)); // 1139 合格数量
- // System.out.println("合格数量2:"+plc.readInt16(1162)); // 1139 合格数量
- ////
- // System.out.println("控制模式:"+plc.readInt16(1090)); // 1091 控制模式
- // System.out.println("系统运行中:"+plc.readCoil(3128,1)); // 3129 系统运行中
- // System.out.println("结束暂停:"+plc.readCoil(3132,1)); // 3133 系统运行中
- // System.out.println("远程开机:"+plc.readCoil(3328,1)); // 3329 远程开机
- // System.out.println("远程关机:"+plc.readCoil(3079,1)); // 3080 远程关机
- // System.out.println("作业模式:"+plc.readInt16(1092)); // 1093 作业模式 1=标记铆接模式
- // System.out.println("最大拉力:"+plc.readInt16(1064)); // 1065 最大拉力
- // System.out.println("对应位移:"+plc.readInt16(1065)); // 1066 对应位移
- // System.out.println("任务数量:"+plc.readInt16(1128)); // 1129 任务数量
- // System.out.println("已铆接数量:"+plc.readInt16(1136)); // 1137 任务数量
- // System.out.println("合格数量:"+plc.readInt16(1138)); // 1139 合格数量
- // System.out.println("质量状态:"+plc.readInt16(1072)); // 1073 质量状态
- // System.out.println("F-out:"+plc.readInt16(1064));
- // System.out.println("S-out:"+plc.readInt16(1065));
- // System.out.println("F-min:"+plc.readInt16(1068));
- // System.out.println("S-min:"+plc.readInt16(1070));
- // System.out.println("F-max:"+plc.readInt16(1069));
- // System.out.println("S-max:"+plc.readInt16(1071));
- // System.out.println("OK:"+plc.readCoil(3101,1));
- // System.out.println("NG:"+plc.readCoil(3102,1));
- // System.out.println("工件完成:"+plc.readCoil(3155,1));
- // System.out.println("暂停:"+plc.readCoil(3156,1));
- }
- public static int getCharByPosition(String binaryString, int position) {
- //// 想要获取的位的位置(从右到左,从0开始计数) position
- // 获取指定位置的字符并转换为数字
- char bit = binaryString.charAt(15 - position); // 调整位置以符合从右到左的计数方式
- int value = (int) Character.getNumericValue(bit);
- return value;
- }
- public static String setCharByPosition(String binaryString, int position, char value) {
- //// 想要获取的位的位置(从右到左,从0开始计数) position
- StringBuilder sb = new StringBuilder(binaryString);
- sb.setCharAt(15 - position, value);
- return sb.toString();
- }
- // 大端排序转字符串
- public static String bytesToBigEndianString(byte[] bytes) {
- // 使用ByteBuffer创建一个大端缓冲区
- ByteBuffer buffer = ByteBuffer.allocate(bytes.length).order(ByteOrder.BIG_ENDIAN);
- // 将byte数组复制到ByteBuffer中
- buffer.put(bytes);
- // 重设缓冲区位置
- buffer.flip();
- // 根据需要转换为不同类型,这里转换为字符串
- String result = "";
- for (int i = 0; i < bytes.length; i++) {
- result += String.format("%02X", buffer.get()); // 以16进制字符串的形式拼接
- }
- return result;
- }
- public static int getBit(byte b, int n) {
- // 将byte转换为二进制字符串
- String binaryString = Integer.toBinaryString(b & 0xFF);
- // 补足位数到8位
- binaryString = String.format("%8s", binaryString).replace(' ', '0');
- // 获取第n位的字符,然后转换为整数
- return binaryString.charAt(n) == '1' ? 1 : 0;
- }
- }
|