|
|
@@ -0,0 +1,292 @@
|
|
|
+package com.jeesite.modules;
|
|
|
+
|
|
|
+import com.github.s7connector.api.DaveArea;
|
|
|
+import com.github.s7connector.api.S7Connector;
|
|
|
+import com.github.s7connector.api.S7Serializer;
|
|
|
+import com.github.s7connector.api.factory.S7ConnectorFactory;
|
|
|
+import com.github.s7connector.api.factory.S7SerializerFactory;
|
|
|
+import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
|
|
|
+import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
|
|
|
+
|
|
|
+import java.math.BigInteger;
|
|
|
+
|
|
|
+public class S7 {
|
|
|
+ public static void main(String[] args) {
|
|
|
+// 192.168.0.200
|
|
|
+ S7PLC s7PLC = new S7PLC(EPlcType.SINUMERIK_828D, "192.168.1.7");
|
|
|
+ s7PLC.readStatus();
|
|
|
+ int workMode = s7PLC.readWorkMode();
|
|
|
+// S7PLC s7PLC = new S7PLC(EPlcType.S1200, "192.168.0.200");
|
|
|
+// s7PLC.writeString("DB1.400","000020015308-0100101425042500137");
|
|
|
+// System.out.println("单枪-条码:"+s7PLC.readString("DB1.400.0",40));
|
|
|
+// System.out.println("单枪-产品:"+s7PLC.readInt16("DB1.440.0"));
|
|
|
+// System.out.println("单枪-拉力:"+s7PLC.readInt32("DB1.144.0"));
|
|
|
+// System.out.println("单枪-位移:"+s7PLC.readFloat32("DB1.148.0"));
|
|
|
+// System.out.println("单枪-结果:"+s7PLC.readString("DB1.450.0",2));
|
|
|
+// System.out.println("单枪-预设值:"+s7PLC.readInt32("DB1.38.0"));
|
|
|
+// System.out.println("单枪-实际值:"+s7PLC.readInt32("DB1.184.0"));
|
|
|
+// System.out.println("单枪-OK数:"+s7PLC.readInt32("DB1.176.0"));
|
|
|
+// System.out.println("单枪-NG数:"+s7PLC.readInt32("DB1.180.0"));
|
|
|
+// System.out.println("单枪-基准拉力:"+s7PLC.readInt16("DB1.442.0"));
|
|
|
+// System.out.println("单枪-基准位移:"+s7PLC.readInt32("DB1.444.0"));
|
|
|
+// System.out.println("单枪-拉力上限:"+s7PLC.readInt32("DB1.160.0"));
|
|
|
+// System.out.println("单枪-拉力下限:"+s7PLC.readInt32("DB1.164.0"));
|
|
|
+// System.out.println("单枪-位移上限:"+s7PLC.readInt32("DB1.168.0"));
|
|
|
+// System.out.println("单枪-位移下限:"+s7PLC.readInt32("DB1.172.0"));
|
|
|
+// System.out.println("单枪-拉铆完成信号:"+s7PLC.readBoolean("DB1.314.5"));
|
|
|
+// System.out.println("单枪-MES屏蔽:"+s7PLC.readBoolean("DB5.0.1"));
|
|
|
+// System.out.println("单枪-MES启用:"+s7PLC.readBoolean("DB5.0.0"));
|
|
|
+// s7PLC
|
|
|
+// s7PLC.writeBoolean("DB1.2.2",true);
|
|
|
+// s7PLC.writeBoolean("DB1.2.3",false);
|
|
|
+ System.out.println("A枪-强制使用:"+s7PLC.readBoolean("DB1.2.3"));
|
|
|
+ System.out.println("A枪-报警断气:"+s7PLC.readBoolean("DB1.1.0"));
|
|
|
+ System.out.println("A枪-扫码功能:"+s7PLC.readBoolean("DB1.2.2"));
|
|
|
+ System.out.println("A枪-授权运行:"+s7PLC.readBoolean("DB1.3.0"));
|
|
|
+ System.out.println("A枪-SV(预设)数量:"+s7PLC.readInt32("DB1.38.0"));
|
|
|
+ System.out.println("A枪-PV(实际)数量:"+s7PLC.readInt32("DB1.184.0"));
|
|
|
+ System.out.println("A枪-OK数量:"+s7PLC.readInt32("DB1.176.0"));
|
|
|
+ System.out.println("A枪-NG数量:"+s7PLC.readInt32("DB1.180.0"));
|
|
|
+ System.out.println("A枪-产品选择:"+s7PLC.readInt16("DB1.54.0"));
|
|
|
+ System.out.println("A枪-最大拉力:"+s7PLC.readInt32("DB1.144.0"));
|
|
|
+ System.out.println("A枪-最大位移:"+s7PLC.readFloat32("DB1.148.0"));
|
|
|
+ System.out.println("A枪-基准拉力:"+s7PLC.readInt32("DB1.152.0"));
|
|
|
+ System.out.println("A枪-基准位移:"+s7PLC.readFloat32("DB1.156.0"));
|
|
|
+// System.out.println("A枪-拉力上限:"+s7PLC.readInt32("DB1.160.0"));
|
|
|
+// System.out.println("A枪-拉力下限:"+s7PLC.readInt32("DB1.164.0"));
|
|
|
+// System.out.println("A枪-位移上限:"+s7PLC.readFloat32("DB1.168.0"));
|
|
|
+// System.out.println("A枪-位移下限:"+s7PLC.readFloat32("DB1.172.0"));
|
|
|
+ System.out.println("A枪-拉力上限:"+s7PLC.readInt32("DB1.22.0"));
|
|
|
+ System.out.println("A枪-拉力下限:"+s7PLC.readInt32("DB1.26.0"));
|
|
|
+ System.out.println("A枪-位移上限:"+s7PLC.readFloat32("DB1.30.0"));
|
|
|
+ System.out.println("A枪-位移下限:"+s7PLC.readFloat32("DB1.34.0"));
|
|
|
+ System.out.println("A枪-拉力OK:"+s7PLC.readBoolean("DB1.314.0"));
|
|
|
+ System.out.println("A枪-拉力NG:"+s7PLC.readBoolean("DB1.314.1"));
|
|
|
+ System.out.println("A枪-位移OK:"+s7PLC.readBoolean("DB1.314.2"));
|
|
|
+ System.out.println("A枪-位移NG:"+s7PLC.readBoolean("DB1.314.3"));
|
|
|
+ System.out.println("A枪-拉铆完成信号:"+s7PLC.readBoolean("DB1.314.5"));
|
|
|
+ System.out.println("A枪-判断结果:"+s7PLC.readString("DB1.398.0",2));
|
|
|
+ System.out.println("A枪-实时条码:"+s7PLC.readString("DB1.400.0",50));
|
|
|
+
|
|
|
+ System.out.println("A枪-枪号:"+s7PLC.readInt16("DB1.450.0"));
|
|
|
+ System.out.println("A枪-产品型号:"+s7PLC.readInt16("DB1.452.0"));
|
|
|
+ System.out.println("A枪-基准拉力:"+s7PLC.readInt16("DB1.454.0"));
|
|
|
+ System.out.println("A枪-基准位移:"+s7PLC.readInt16("DB1.456.0"));
|
|
|
+ System.out.println("A枪-最大拉力:"+s7PLC.readInt16("DB1.458.0"));
|
|
|
+ System.out.println("A枪-最大位移:"+s7PLC.readInt16("DB1.460.0"));
|
|
|
+ System.out.println("A枪-结果判断:"+s7PLC.readString("DB1.462.0",2));
|
|
|
+ System.out.println("A枪-当前数量:"+s7PLC.readInt16("DB1.464.4"));
|
|
|
+ System.out.println("A枪-总铆接数:"+s7PLC.readString("DB1.466.0",10));
|
|
|
+ System.out.println("A枪-条码信息:"+s7PLC.readString("DB1.476.0",50));
|
|
|
+
|
|
|
+
|
|
|
+ System.out.println("B枪-扫码功能:"+s7PLC.readBoolean("DB1.532.2"));
|
|
|
+// System.out.println("B枪-授权运行:"+s7PLC.readBoolean("DB1.3.0"));
|
|
|
+ System.out.println("B枪-SV(预设)数量:"+s7PLC.readInt32("DB1.568.0"));
|
|
|
+ System.out.println("B枪-PV(实际)数量:"+s7PLC.readInt32("DB1.714.0"));
|
|
|
+ System.out.println("B枪-OK数量:"+s7PLC.readInt32("DB1.706.0"));
|
|
|
+ System.out.println("B枪-NG数量:"+s7PLC.readInt32("DB1.710.0"));
|
|
|
+ System.out.println("B枪-产品选择:"+s7PLC.readInt16("DB1.584.0"));
|
|
|
+ System.out.println("B枪-最大拉力:"+s7PLC.readInt32("DB1.674.0"));
|
|
|
+ System.out.println("B枪-最大位移:"+s7PLC.readFloat32("DB1.678.0"));
|
|
|
+ System.out.println("B枪-基准拉力:"+s7PLC.readInt32("DB1.682.0"));
|
|
|
+ System.out.println("B枪-基准位移:"+s7PLC.readFloat32("DB1.686.0"));
|
|
|
+// System.out.println("B枪-拉力上限:"+s7PLC.readInt32("DB1.690.0"));
|
|
|
+// System.out.println("B枪-拉力下限:"+s7PLC.readInt32("DB1.694.0"));
|
|
|
+// System.out.println("B枪-位移上限:"+s7PLC.readFloat32("DB1.698.0"));
|
|
|
+// System.out.println("B枪-位移下限:"+s7PLC.readFloat32("DB1.702.0"));
|
|
|
+
|
|
|
+ System.out.println("B枪-拉力上限:"+s7PLC.readInt32("DB1.552.0"));
|
|
|
+ System.out.println("B枪-拉力下限:"+s7PLC.readInt32("DB1.556.0"));
|
|
|
+ System.out.println("B枪-位移上限:"+s7PLC.readFloat32("DB1.560.0"));
|
|
|
+ System.out.println("B枪-位移下限:"+s7PLC.readFloat32("DB1.564.0"));
|
|
|
+
|
|
|
+ System.out.println("B枪-拉力OK:"+s7PLC.readBoolean("DB1.844.0"));
|
|
|
+ System.out.println("B枪-拉力NG:"+s7PLC.readBoolean("DB1.844.1"));
|
|
|
+ System.out.println("B枪-位移OK:"+s7PLC.readBoolean("DB1.844.2"));
|
|
|
+ System.out.println("B枪-位移NG:"+s7PLC.readBoolean("DB1.844.3"));
|
|
|
+ System.out.println("B枪-拉铆完成信号:"+s7PLC.readBoolean("DB1.844.5"));
|
|
|
+ System.out.println("B枪-最大拉力:"+s7PLC.readInt32("DB1.846"));
|
|
|
+ System.out.println("B枪-最大位移:"+s7PLC.readFloat32("DB1.850"));
|
|
|
+ System.out.println("B枪-判断结果:"+s7PLC.readString("DB1.992.0",2));
|
|
|
+ System.out.println("B枪-实时条码:"+s7PLC.readString("DB1.930.0",50));
|
|
|
+
|
|
|
+ System.out.println("B枪-枪号:"+s7PLC.readInt16("DB1.980.0"));
|
|
|
+ System.out.println("B枪-产品型号:"+s7PLC.readInt16("DB1.982.0"));
|
|
|
+ System.out.println("B枪-基准拉力:"+s7PLC.readInt16("DB1.984.0"));
|
|
|
+ System.out.println("B枪-基准位移:"+s7PLC.readInt16("DB1.986.0"));
|
|
|
+ System.out.println("B枪-最大拉力:"+s7PLC.readInt16("DB1.988.0"));
|
|
|
+ System.out.println("B枪-最大位移:"+s7PLC.readInt16("DB1.990.0"));
|
|
|
+ System.out.println("B枪-结果判断:"+s7PLC.readString("DB1.992.0",2));
|
|
|
+ System.out.println("B枪-当前数量:"+s7PLC.readInt16("DB1.994.4"));
|
|
|
+ System.out.println("B枪-总铆接数:"+s7PLC.readString("DB1.996.0",10));
|
|
|
+ System.out.println("B枪-条码信息:"+s7PLC.readString("DB1.1006.0",50));
|
|
|
+
|
|
|
+// String programName = s7PLC.readProgramName();;
|
|
|
+// System.out.println("programName:"+programName);
|
|
|
+////// String programName = s7PLC.readProgramName();;
|
|
|
+// System.out.println("programName:"+s7PLC.readStatus());
|
|
|
+// // S7PLC s7PLC = new S7PLC(EPlcType.S1200, "192.168.0.41");
|
|
|
+// S7PLC s7PLC = new S7PLC(EPlcType.S1200, "192.168.7.100");
|
|
|
+// Short dya = s7PLC.readInt16("DB1010.20");
|
|
|
+// Short dla = s7PLC.readInt16("DB1.22");
|
|
|
+// Short ssa = s7PLC.readInt16("DB1.24");
|
|
|
+// Short cwa = s7PLC.readInt16("DB1.26");
|
|
|
+// Short joba = s7PLC.readInt16("DB1.28");
|
|
|
+
|
|
|
+// System.err.println(s7PLC.readFloat32("DB1010.2")+"");
|
|
|
+// System.err.println(s7PLC.readFloat32("DB1010.8")+"");
|
|
|
+// System.err.println(s7PLC.readFloat32("DB1010.14")+"");
|
|
|
+// System.err.println(s7PLC.readInt16("DB1010.18")+"");
|
|
|
+// System.err.println(s7PLC.readInt16("DB1010.20")+"");
|
|
|
+//
|
|
|
+// System.err.println(s7PLC.readFloat32("DB1010.26")+"");
|
|
|
+// System.err.println(s7PLC.readFloat32("DB1010.34")+"");
|
|
|
+// System.err.println(s7PLC.readFloat32("DB1010.42")+"");
|
|
|
+// System.err.println(s7PLC.readInt16("DB1010.46")+"");
|
|
|
+// System.err.println(s7PLC.readInt16("DB1010.48")+"");
|
|
|
+//
|
|
|
+// System.err.println(s7PLC.readFloat32("DB1010.54")+"");
|
|
|
+// System.err.println(s7PLC.readFloat32("DB1010.62")+"");
|
|
|
+// System.err.println(s7PLC.readFloat32("DB1010.70")+"");
|
|
|
+// System.err.println(s7PLC.readInt16("DB1010.74")+"");
|
|
|
+// System.err.println(s7PLC.readInt16("DB1010.76")+"");
|
|
|
+
|
|
|
+// Float dyb = s7PLC.readFloat32("DB2000.50");
|
|
|
+// Float dlb = s7PLC.readFloat32("DB2000.54");
|
|
|
+// Float ssb = s7PLC.readFloat32("DB2000.58");
|
|
|
+// Short cwb = s7PLC.readInt16("DB2000.62");
|
|
|
+// Short jobb = s7PLC.readInt16("DB2000.64");
|
|
|
+
|
|
|
+// Short dyb = s7PLC.readInt16("DB1.30");
|
|
|
+// Short dlb = s7PLC.readInt16("DB1.32");
|
|
|
+// Short ssb = s7PLC.readInt16("DB1.34");
|
|
|
+// Short cwb = s7PLC.readInt16("DB1.36");
|
|
|
+// Short jobb = s7PLC.readInt16("DB1.38");
|
|
|
+// System.err.println(dyb+"");
|
|
|
+// System.err.println(dlb+"");
|
|
|
+// System.err.println(ssb+"");
|
|
|
+// System.err.println(cwb+"");
|
|
|
+// System.err.println(jobb+"");
|
|
|
+
|
|
|
+
|
|
|
+// S7PLC s7PLC = new S7PLC(EPlcType.S200_SMART,"192.168.2.1");
|
|
|
+// s7PLC.writeBoolean("V800.1",true);
|
|
|
+//// s7PLC.writeBoolean("V801.0",true);
|
|
|
+//// s7PLC.writeBoolean("V800.0",true);
|
|
|
+//
|
|
|
+// System.out.println(s7PLC.readBoolean("V800.0"));
|
|
|
+// System.out.println(s7PLC.readBoolean("V801.0"));
|
|
|
+// System.out.println(s7PLC.readBoolean("V800.1"));
|
|
|
+
|
|
|
+ // Float weight = Float.valueOf(" ");
|
|
|
+
|
|
|
+// System.out.println(Float.valueOf(" "));
|
|
|
+// S7PLC s7PLC = new S7PLC(EPlcType.SINUMERIK_828D, "192.168.0.7");
|
|
|
+
|
|
|
+ //System.out.println(s7PLC.readFloat32("DB9050.12"));
|
|
|
+ //System.out.println(s7PLC.readFloat32("DB9050.14"));
|
|
|
+
|
|
|
+
|
|
|
+// System.out.println(new BigInteger(s7PLC.readByte("DB9050",4)).floatValue());
|
|
|
+
|
|
|
+
|
|
|
+// S7Connector s7Connector = null;
|
|
|
+// try {
|
|
|
+// if (s7Connector == null) {
|
|
|
+//
|
|
|
+// //PLC地址
|
|
|
+// String ipAddress = "192.168.7.100";
|
|
|
+// //默认端口
|
|
|
+//// int port = 102;
|
|
|
+// int rack = 0;
|
|
|
+// int slot = 3;
|
|
|
+// int timeout = 10000;
|
|
|
+// s7Connector =
|
|
|
+// S7ConnectorFactory
|
|
|
+// .buildTCPConnector()
|
|
|
+// .withHost(ipAddress)
|
|
|
+//// .withPort(port) //optional
|
|
|
+//// .withRack(rack) //optional
|
|
|
+//// .withSlot(slot) //optional
|
|
|
+// .withTimeout(timeout) //连接超时时间
|
|
|
+// .build();
|
|
|
+// S7Serializer s7Serializer2L = S7SerializerFactory.buildSerializer(s7Connector);
|
|
|
+//
|
|
|
+//
|
|
|
+//
|
|
|
+//
|
|
|
+//// String binaryString = "00000001";
|
|
|
+//// int length1 = binaryString.length();
|
|
|
+//// byte[] byteArray = new byte[length1 / 8];
|
|
|
+//// for (int i = 0; i < byteArray.length; i++) {
|
|
|
+//// String byteString = binaryString.substring(i * 8, (i + 1) * 8);
|
|
|
+//// byteArray[i] = (byte) Integer.parseInt(byteString, 2);
|
|
|
+//// }
|
|
|
+//
|
|
|
+// byte[] bytes2 = new byte[1];
|
|
|
+// bytes2[0] = 0x01;
|
|
|
+// s7Connector.write(DaveArea.DB,9052,1,bytes2);
|
|
|
+////
|
|
|
+//// System.out.println("DB9031.DBW60:"+new BigInteger(xtob(s7Connector.read(DaveArea.DB, 1010, 2, 0))).intValue());
|
|
|
+//
|
|
|
+//
|
|
|
+// System.out.println("DB9050.DBD0:"+new BigInteger(xtob(s7Connector.read(DaveArea.DB, 9050, 4, 0))).floatValue());
|
|
|
+// System.out.println("DB9050.DBD4:"+new BigInteger(xtob(s7Connector.read(DaveArea.DB, 9050, 4, 4))).intValue());
|
|
|
+// System.out.println("DB9050.DBD8:"+new BigInteger(xtob(s7Connector.read(DaveArea.DB, 9050, 4, 8))).intValue());
|
|
|
+// System.out.println("DB9050.DBD12:"+new BigInteger(xtob(s7Connector.read(DaveArea.DB, 9050, 4, 12))).intValue());
|
|
|
+// System.out.println("DB9050.DBD16:"+new BigInteger(xtob(s7Connector.read(DaveArea.DB, 9050, 4, 16))).intValue());
|
|
|
+//
|
|
|
+//
|
|
|
+// System.out.println("DB9052.DBB1:"+new BigInteger(xtob(s7Connector.read(DaveArea.DB, 9052, 1, 1))).intValue());
|
|
|
+//
|
|
|
+// byte[] bytes3 = s7Connector.read(DaveArea.DB, 9052, 1, 1);
|
|
|
+// System.out.println("DB9052.DBB1:"+byteToBinaryString(bytes3));
|
|
|
+//
|
|
|
+//
|
|
|
+// byte[] bytes = s7Connector.read(DaveArea.DB, 9052, 1, 0);
|
|
|
+// System.out.println("DB9052.DBX0:"+byteToBinaryString(bytes));
|
|
|
+//
|
|
|
+// System.out.println("DB9052.DBX0:"+getBit(bytes[0],0));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// }
|
|
|
+// }catch (Exception e){
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String byteToBinaryString(byte[] byteArray) {
|
|
|
+ StringBuilder binaryString = new StringBuilder();
|
|
|
+ for (byte b : byteArray) {
|
|
|
+ binaryString.append(String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'));
|
|
|
+ }
|
|
|
+ return binaryString.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static byte[] xtob( byte[] bytes){
|
|
|
+ // 创建新的byte数组并复制原始数据
|
|
|
+ byte[] reversedBytes = new byte[bytes.length];
|
|
|
+ System.arraycopy(bytes, 0, reversedBytes, 0, bytes.length);
|
|
|
+
|
|
|
+ // 反转byte数组元素的位置
|
|
|
+ for (int i = 0; i < bytes.length / 2; i++) {
|
|
|
+ byte temp = reversedBytes[i];
|
|
|
+ reversedBytes[i] = reversedBytes[reversedBytes.length - 1 - i];
|
|
|
+ reversedBytes[reversedBytes.length - 1 - i] = temp;
|
|
|
+ }
|
|
|
+ return reversedBytes;
|
|
|
+ }
|
|
|
+}
|