package com.mes.common;
|
|
import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
|
import com.mes.device.PlcParameterObject;
|
import com.mes.tools.InitUtil;
|
import com.mes.tools.S7control;
|
|
|
/**
|
* @Author : yanzhixin
|
* @Date: 2024/4/9 15:13
|
* @Description:
|
*/
|
public class S7objectCleaning extends Thread {
|
public S7control plccontrol; // PLC通讯类实例
|
private EPlcType plcType = EPlcType.S200_SMART; // 西门子PLC类型
|
private String ip = "200.200.200.193"; // plc ip地址
|
private int port = 102; // plc 端口号
|
|
|
public PlcParameterObject PlcMesObject;
|
private static volatile S7objectCleaning instance = null;
|
|
private S7objectCleaning() {
|
if (plccontrol == null) {
|
plccontrol = new S7control(plcType, ip, port, 0, 0);
|
String PlcLoadGlass = System.getProperty("user.dir") + "/JsonFile/PlcCleaning.json";
|
// String PlcLoadGlass=S7object.class.getResource("/JsonFile/PlcCacheGlass.json").getPath();
|
System.out.println("Load Glass File: " + PlcLoadGlass);
|
PlcMesObject = InitUtil.initword(PlcLoadGlass);
|
}
|
}
|
|
// 单例模式 获取类的唯一实例
|
public static S7objectCleaning getinstance() {
|
if (instance == null) {
|
synchronized (S7objectCleaning.class) {
|
if (instance == null) {
|
instance = new S7objectCleaning();
|
}
|
}
|
}
|
return instance;
|
}
|
|
@Override
|
public void run() {
|
while (this != null) {
|
try {
|
Thread.sleep(100);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
byte[] resultValues=new byte[PlcMesObject.getPlcAddressLength()];
|
int maxRead=1092;
|
int size=PlcMesObject.getPlcAddressLength()%maxRead==0?
|
(PlcMesObject.getPlcAddressLength()/maxRead):
|
(PlcMesObject.getPlcAddressLength()/maxRead+1);
|
for (int i = 0; i <size ; i++) {
|
int begin=i*maxRead;
|
int length=(i==size-1?PlcMesObject.getPlcAddressLength()-begin:maxRead);
|
//String s=PlcMesObject.getPlcAddressBegin().substring(PlcMesObject.getPlcAddressBegin().indexOf(".")+1);
|
int defaultStart=Integer.valueOf(PlcMesObject.getPlcAddressBegin().substring(PlcMesObject.getPlcAddressBegin().indexOf(".")+1));
|
|
String beginAddress=PlcMesObject.getPlcAddressBegin().substring(0, PlcMesObject.getPlcAddressBegin().indexOf("."))+"."+(begin+defaultStart);
|
byte[] getplcvlues = plccontrol.readByte(beginAddress, length);
|
System.arraycopy(getplcvlues,0,resultValues,begin,length);
|
}
|
if (resultValues != null) {
|
PlcMesObject.setPlcParameterList(resultValues);
|
}
|
}
|
}
|
}
|