package com.mes.common;
|
|
import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
|
import com.mes.device.PlcParameterInfo;
|
import com.mes.device.PlcParameterObject;
|
import com.mes.tools.InitUtil;
|
import com.mes.tools.S7control;
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.util.ArrayList;
|
import java.util.Collection;
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
|
|
/**
|
* @Author : yanzhixin
|
* @Date: 2024/4/9 15:13
|
* @Description:
|
*/
|
@Slf4j
|
public class S7objectMachine extends Thread {
|
public S7control plccontrol; // PLC通讯类实例
|
private EPlcType plcType = EPlcType.S1200; // 西门子PLC类型
|
private String ip = ""; // plc ip地址
|
private int port = 102; // plc 端口号
|
private String [] plcFileName = null; // json名称
|
public List<PlcParameterObject> plcMesObjectList=new ArrayList<>();
|
public boolean plcRequest;
|
public boolean mesConfirm;
|
|
public S7objectMachine(String ip, int port, String [] plcFileName, EPlcType plcType) {
|
initialize(ip, port, plcFileName, plcType);
|
if (plccontrol == null) {
|
plccontrol = new S7control(this.plcType, this.ip, this.port, 0, 0);
|
//String plcFileUrl = System.getProperty("user.dir") + "D:/HangZhouMes/JsonFile/"+this.plcFileName+".json";
|
// String plcFileUrl = "D:/mes/JsonFile/" + this.plcFileName + ".json";
|
for (String fileName:plcFileName){
|
String plcFileUrl = System.getProperty("user.dir") + "/JsonFile/" + fileName + ".json";
|
PlcParameterObject itemPlcMesObject =null;
|
//if (plcMesObjectList.size()==0){
|
itemPlcMesObject = InitUtil.initword(plcFileUrl);
|
//}
|
plcMesObjectList.add(itemPlcMesObject);
|
}
|
|
}
|
}
|
|
public void initialize(String ip, int port, String [] plcFileName, EPlcType plcType) {
|
this.ip = ip;
|
this.port = port;
|
this.plcFileName = plcFileName;
|
this.plcType = plcType;
|
}
|
|
/**
|
* 打印参数值
|
*/
|
public void consoleLogInfo() {
|
String logInfo = this.plcFileName + " ";
|
for (PlcParameterObject PlcMesObject : plcMesObjectList) {
|
LinkedHashMap<String, PlcParameterInfo> thisPlcParameterInfo = PlcMesObject.getPlcParameterMap();
|
for (String key : thisPlcParameterInfo.keySet()) {
|
logInfo += key + ":" + thisPlcParameterInfo.get(key).getValue() + ",";
|
}
|
log.info(logInfo);
|
}
|
|
}
|
|
@Override
|
public void run() {
|
while (this != null) {
|
try {
|
Thread.sleep(100);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
try {
|
if (plcMesObjectList != null) {
|
for (PlcParameterObject plcParameterObject : plcMesObjectList) {
|
if (plcParameterObject!= null) {
|
//byte[] resultValues=new byte[plcParameterObject.getPlcAddressLength()];
|
//resultValues[0]=1;
|
//log.info("plcMesObjectList:{},PLC地址:{},PLC长度:{}",plcMesObjectList,plcParameterObject.getPlcAddressBegin(),plcParameterObject.getPlcAddressLength());
|
byte[] resultValues = plccontrol.readByte(plcParameterObject.getPlcAddressBegin(), plcParameterObject.getPlcAddressLength());
|
//log.info("内容:{}",resultValues);
|
if (resultValues != null) {
|
plcParameterObject.setPlcParameterList(resultValues);
|
}
|
}
|
}
|
}
|
|
} catch (Exception e) {
|
log.info("异常:ip:{},port:{}",this.ip,this.port);
|
}
|
|
}
|
}
|
}
|