package steer.MelsecPLC;
import HslCommunication.BasicFramework.SoftBasic;
import HslCommunication.Core.Types.OperateResult;
import HslCommunication.Core.Types.OperateResultExOne;
import HslCommunication.Profinet.Melsec.MelsecMcAsciiNet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import net.sf.json.JSONObject;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
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;
/**
@date:2021年9月30日 三菱PLC
*/
@Controller
public class MelsecPLC {private static MelsecMcAsciiNet melsecMcNet = new MelsecMcAsciiNet();
private static final String HOST2 = "192.168.1.37";
private static S7Connector connector2;
private static S7Serializer serializer2;
JSONObject data = new JSONObject();
WebSocketServer ws = new WebSocketServer();//PLC连接
static {if (null == connector2) { try { melsecMcNet.setIpAddress("192.168.1.10"); melsecMcNet.setPort(Integer.parseInt("1280")); OperateResult connect = melsecMcNet.ConnectServer(); if (connect.IsSuccess) { System.out.println("Connect succeed --- PLC1连接成功"); } connector2 = S7ConnectorFactory.buildTCPConnector().withHost(HOST2). withRack(0).withSlot(0).withTimeout(3000).build(); serializer2 = S7SerializerFactory.buildSerializer(connector2); } catch (Exception e) { connector2 = null; System.out.println(e); System.out.println("plc1连接失败"); } }
}
@Scheduled(fixedDelay = 500)
public void onOpen() {try { //当扫码枪扫到的信息包含LGW时,将VIN发送到前端 //前端收到VIN信息后会回显VIN,并开始执行流程 if (UsbUtils.usbMsg.contains("LGW")) { data.put("#VIN", UsbUtils.usbMsg); ws.sendInfo(data.toString()); } //当扫码枪扫到的信息包含CC时,将CC号 发送到前端 if (UsbUtils.usbMsg.contains("CC")) { data.put("#barcode", UsbUtils.usbMsg); ws.sendInfo(data.toString()); } UsbUtils.usbMsg = "NULL"; data.put("userless", "测试手动"); boolean flag = true; // 读取32位数据 data1为 result结果 OperateResultExOne<Integer> data_1 = melsecMcNet.ReadInt32("D700"); System.out.println("data_1:::" + data_1); Date creatDate = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("时间:" + sdf.format(creatDate)); String result1 = SoftBasic.ArrayFormat(melsecMcNet.ReadInt32("D700").Content); System.out.println("result1:::" + result1); int result2 = Integer.parseInt(result1.substring(1, result1.length() - 1)); // 读取后转化为二进制,并以0补全16位 String result3 = String.format("%16s", Integer.toBinaryString(result2)).replace(" ", "0"); //将result3倒序 String result = ""; char[] charArray = result3.toCharArray(); for (int i = charArray.length - 1; i >= 0; i--) { result += charArray[i]; } /* * 读取实际位置防错:设备都在原位才可进行点击开始按钮标定的操作 */ Thread.sleep(100);//========= In_R_ACCY accy = serializer2.dispense(In_R_ACCY.class, 101, 0); In_R_CRZ crz = serializer2.dispense(In_R_CRZ.class, 101, 0); In_R_CRL cr1 = serializer2.dispense(In_R_CRL.class, 101, 0); In_R_CRR cr2 = serializer2.dispense(In_R_CRR.class, 101, 0); In_B_ACCY accy1 = serializer2.dispense(In_B_ACCY.class, 101, 0); In_B_ACCZ accz1 = serializer2.dispense(In_B_ACCZ.class, 101, 0); In_B_CRZ crz1 = serializer2.dispense(In_B_CRZ.class, 101, 0); In_B_CRL cry1 = serializer2.dispense(In_B_CRL.class, 101, 0); In_B_CRR cry2 = serializer2.dispense(In_B_CRR.class, 101, 0); InplcSystem control2 = serializer2.dispense(InplcSystem.class, 101, 0); int accyNow = accy.DBW4_0;//ACC-Y实际位置 int crzNow = crz.DBW18_0;//CR-Z实际位置 int cry1Now = cr1.DBW22_0;//CR-Y1实际位置 int cry2Now = cr2.DBW28_0;//CR-Y2实际位置 //设备是否在原位置 判定 if (accyNow >= -10 && accyNow <= 10 && crzNow >= -10 && crzNow <= 10 && cry1Now >= -10 && cry1Now <= 10 && cry2Now >= -10 && cry2Now <= 10) {
// System.out.println("设备均在原位置 可以按开始按钮进行标定!");
//开始 信号
if (result.substring(3, 4).equals("1") && flag) {
flag = false;
data.put("check-start", 1);
}
if (!result.substring(3, 4).equals("1")) {
flag = true;
}
}
//例:读取到的一次数据(手动倒置之后)1010000011100000
data.put("#checking", result.substring(3, 4).equals("1") ? 1 : 0);//开始测试,测试中
data.put("#PLC-signal", result.substring(0, 1).equals("1") ? 1 : 0); // PLC 心跳1
data.put("#PLC-signal2", control2.DBX0_0 ? 1 : 0); // PLC 心跳2
data.put("#auto-mode", result.substring(2, 3).equals("0") ? 1 : 0); // 自/手动模式
data.put("#OBD-connection", result.substring(4, 5).equals("1") ? 1 : 0); // OBD连接
data.put("#LDW-moving", result.substring(9, 10).equals("0") ? 1 : 0); // LDW移动中
data.put("#RSDS-moving", result.substring(10, 11).equals("0") ? 1 : 0); // rsds移动中
data.put("#ACCY-error", accy1.DBX2_7 ? 1 : 0); //ACC-Y驱动器故障
data.put("#ACCZ-error", accz1.DBX8_7 ? 1 : 0); //ACC-Z驱动器故障
data.put("#CRZ-error", crz1.DBX14_7 ? 1 : 0); //CR-Z驱动器故障
data.put("#CRY1-error", cry1.DBX20_7 ? 1 : 0); //CR-Y1驱动器故障
data.put("#CRY2-error", cry2.DBX26_7 ? 1 : 0); //CR-Y2驱动器故障
if (accy1.DBX2_7 || accz1.DBX8_7 || crz1.DBX14_7 || cry1.DBX20_7 || cry2.DBX26_7) {
data.put("#device-error", "设备故障");
} else {
data.put("#device-error", "设备正常");
}
// //PLC连接信息监控显示
// if (result.charAt(0) != '1') {
// data.put("#device-error", "PLC1中断故障");
// }
ws.sendInfo(data.toString());
data.clear();
} catch (Exception e) {
e.printStackTrace();
System.out.println("=======断开后错误信息输出 e:" + e);
//重连 PLC
try {
melsecMcNet.ConnectClose();
Thread.sleep(1000);//=========
melsecMcNet.setIpAddress("192.168.1.10");
melsecMcNet.setPort(Integer.parseInt("1280"));
OperateResult connect = melsecMcNet.ConnectServer();
if (connect.IsSuccess) {
System.out.println("===三菱PLC连接成功=重连==");
}
System.out.println("===三菱PLC连接=重连==");
connector2 = S7ConnectorFactory.buildTCPConnector().withHost(HOST2).
withRack(0).withSlot(0).withTimeout(3000).build();
serializer2 = S7SerializerFactory.buildSerializer(connector2);
System.out.println("===PLC2连接=重连==");
} catch (Exception ex) {
data.put("#device-error", "PLC1中断故障");
ws.sendInfo(data.toString());
connector2 = null;
}
}
}
}
}