package com.qianwen.mdc.service.scanet;
|
|
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.s7connector.exception.S7Exception;
|
import com.qianwen.mdc.utils.MdcException;
|
|
import java.io.IOException;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
public class ScanetClient {
|
private String ip;
|
private S7Connector connector = null;
|
|
private S7Serializer serializer = null;
|
|
private int count = 0;
|
|
private boolean connected = false;
|
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
|
public void connectServer() {
|
if (null == this.connector)
|
try {
|
this.connector = S7ConnectorFactory.buildTCPConnector().withHost(this.ip).build();
|
this.serializer = S7SerializerFactory.buildSerializer(this.connector);
|
if (null != this.connector) {
|
this.count = 0;
|
this.connected = true;
|
} else {
|
this.connected = false;
|
throw new MdcException();
|
}
|
} catch (Exception e) {
|
this.connected = false;
|
throw new MdcException();
|
}
|
}
|
|
public void disconnectServer() {
|
if (this.serializer != null)
|
try {
|
this.connector.close();
|
this.connector = null;
|
this.serializer = null;
|
} catch (IOException e) {
|
throw new MdcException();
|
}
|
this.count = 0;
|
this.connected = false;
|
}
|
|
public void reconnectServer() {
|
try {
|
disconnectServer();
|
connectServer();
|
} catch (Exception exception) {
|
}
|
}
|
|
public void checkConnection() {
|
int dbNum = 21;
|
int byteOffset = 35;
|
try {
|
byte[] read = this.connector.read(DaveArea.DB, dbNum, 50, byteOffset);
|
int i;
|
for (i = 1; i < read.length; i++) {
|
if (read[i] != 0) {
|
this.connected = true;
|
break;
|
}
|
}
|
if (i == read.length)
|
this.connected = false;
|
if (!this.connected) {
|
reconnectServer();
|
throw new MdcException();
|
}
|
} catch (S7Exception e) {
|
this.connected = false;
|
reconnectServer();
|
} catch (Exception e) {
|
if (++this.count >= 3)
|
this.connected = false;
|
throw new MdcException();
|
}
|
}
|
|
public int readOpMode() {
|
int dbNum = 11;
|
int byteOffset1 = 6;
|
int byteOffset2 = 7;
|
byte autoMask = 1;
|
byte mdaMask = 2;
|
byte jogMask = 4;
|
byte refMask = 4;
|
int ret = 9000;
|
try {
|
PlcByteValue opModeValue1 = (PlcByteValue) this.serializer.dispense(PlcByteValue.class, dbNum, byteOffset1);
|
boolean autoValue = ((opModeValue1.value & autoMask) != 0);
|
boolean mdaValue = ((opModeValue1.value & mdaMask) != 0);
|
boolean jogValue = ((opModeValue1.value & jogMask) != 0);
|
PlcByteValue opModeValue2 = (PlcByteValue) this.serializer.dispense(PlcByteValue.class, dbNum, byteOffset2);
|
boolean refValue = ((opModeValue2.value & refMask) != 0);
|
ret = autoValue ? 200 : (mdaValue ? 100 : (jogValue ? 0 : (refValue ? 300 : 9000)));
|
this.count = 0;
|
this.connected = true;
|
} catch (S7Exception e) {
|
this.connected = false;
|
reconnectServer();
|
} catch (Exception e) {
|
if (++this.count >= 3)
|
this.connected = false;
|
throw new MdcException();
|
}
|
return ret;
|
}
|
|
public int readNCState() {
|
int dbNum = 21;
|
int byteOffset = 35;
|
byte ncStartMask = 1;
|
byte ncStopMask = 4;
|
int ret = 9000;
|
try {
|
PlcByteValue ncValue = (PlcByteValue) this.serializer.dispense(PlcByteValue.class, dbNum, byteOffset);
|
boolean ncStartValue = ((ncValue.value & ncStartMask) != 0);
|
boolean ncStopValue = ((ncValue.value & ncStopMask) != 0);
|
ret = ncStartValue ? 0 : (ncStopValue ? 100 : 8000);
|
this.count = 0;
|
this.connected = true;
|
} catch (S7Exception e) {
|
this.connected = false;
|
reconnectServer();
|
} catch (Exception e) {
|
if (++this.count >= 3)
|
this.connected = false;
|
throw new MdcException();
|
}
|
return ret;
|
}
|
|
public int readSpState() {
|
int dbNum = 34;
|
int byteOffset = 64;
|
byte forwardMask = 64;
|
byte reverseMask = Byte.MIN_VALUE;
|
int ret = 9000;
|
try {
|
PlcByteValue spStateValue = (PlcByteValue) this.serializer.dispense(PlcByteValue.class, dbNum, byteOffset);
|
boolean forwardValue = ((spStateValue.value & forwardMask) != 0);
|
boolean reverseValue = ((spStateValue.value & reverseMask) != 0);
|
ret = (forwardValue | reverseValue) ? 0 : 8000;
|
this.count = 0;
|
this.connected = true;
|
} catch (S7Exception e) {
|
this.connected = false;
|
reconnectServer();
|
} catch (Exception e) {
|
if (++this.count >= 3)
|
this.connected = false;
|
throw new MdcException();
|
}
|
return ret;
|
}
|
|
public int readSpRate() {
|
int dbNum = 34;
|
int byteOffset = 19;
|
int ret = 0;
|
try {
|
PlcByteValue spRateValue = (PlcByteValue) this.serializer.dispense(PlcByteValue.class, dbNum, byteOffset);
|
ret = spRateValue.value;
|
this.count = 0;
|
this.connected = true;
|
} catch (S7Exception e) {
|
this.connected = false;
|
reconnectServer();
|
} catch (Exception e) {
|
if (++this.count >= 3)
|
this.connected = false;
|
throw new MdcException();
|
}
|
return ret;
|
}
|
|
public int readSpSpeed() {
|
int dbNum = 34;
|
int byteOffset = 88;
|
int ret = 0;
|
try {
|
PlcByteValue spRateValue = (PlcByteValue) this.serializer.dispense(PlcByteValue.class, dbNum, byteOffset);
|
ret = spRateValue.value;
|
this.count = 0;
|
this.connected = true;
|
} catch (S7Exception e) {
|
this.connected = false;
|
reconnectServer();
|
} catch (Exception e) {
|
if (++this.count >= 3)
|
this.connected = false;
|
throw new MdcException();
|
}
|
return ret;
|
}
|
|
public int readFeedRate() {
|
int dbNum = 21;
|
int byteOffset = 4;
|
int ret = 0;
|
try {
|
PlcByteValue feedRateValue = (PlcByteValue) this.serializer.dispense(PlcByteValue.class, dbNum, byteOffset);
|
ret = feedRateValue.value;
|
this.count = 0;
|
this.connected = true;
|
} catch (S7Exception e) {
|
this.connected = false;
|
reconnectServer();
|
} catch (Exception e) {
|
if (++this.count >= 3)
|
this.connected = false;
|
throw new MdcException();
|
}
|
return ret;
|
}
|
|
public int readAlarmState() {
|
int dbNum = 2;
|
int[] userAlarmOffset = { 180, 181, 182, 183, 184, 195, 186, 187 };
|
int ret = 0;
|
try {
|
for (int i : userAlarmOffset) {
|
PlcByteValue alarmState = (PlcByteValue) this.serializer.dispense(PlcByteValue.class, dbNum, i);
|
if (alarmState.value != 0) {
|
ret = 1;
|
break;
|
}
|
}
|
this.count = 0;
|
this.connected = true;
|
} catch (S7Exception e) {
|
this.connected = false;
|
reconnectServer();
|
} catch (Exception e) {
|
if (++this.count >= 3)
|
this.connected = false;
|
throw new MdcException();
|
}
|
return ret;
|
}
|
|
public String readALARMState() {
|
return null;
|
}
|
|
public String readSPState() {
|
return null;
|
}
|
|
public String getIp() {
|
return this.ip;
|
}
|
|
public void setIp(String ip) {
|
this.ip = ip;
|
}
|
|
public boolean isConnected() {
|
return this.connected;
|
}
|
|
public void setConnected(boolean connected) {
|
this.connected = connected;
|
}
|
|
|
}
|