Skip to content

Commit

Permalink
Add slf4j as dependency, add option to read other areas than DB areas
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrinch committed Oct 24, 2017
1 parent 8c0523f commit 86f794f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,10 @@
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.8.0-beta0</version>
</dependency>
</dependencies>
</project>
55 changes: 48 additions & 7 deletions src/si/trina/moka7/live/PLC.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,26 @@
import java.util.Timer;
import java.util.TimerTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sourceforge.snap7.moka7.S7;
import com.sourceforge.snap7.moka7.S7Client;

public class PLC implements Runnable {

final Logger logger = LoggerFactory.getLogger(PLC.class);

public ArrayList<PLCListener> listeners;
public Object PLCSyncObj;

private int plcToPcDb;
private int pcToPlcDb;

private int rack = 0;
private int slot = 1;
private int plcToPcAreaType = S7.S7AreaDB; // optionally read merker, eingang, ausgang area
private int pcToPlcAreaType = S7.S7AreaDB;

private long plcToPcLiveBit;
private long pcToPlcLiveBit;
private boolean plcToPcLiveBitState;
Expand Down Expand Up @@ -58,6 +67,37 @@ public PLC(String name,String ip,byte[] plcToPc,byte[] pcToPlc,int plcToPcDb,int
this.listeners = new ArrayList<PLCListener>();
}

public PLC(String name,
String ip,
int plcToPcLength,
int pcToPlcLength,
int plcToPcDb,
int pcToPlcDb,
double[] booleans,
int rack,
int slot,
int plcToPcAreaType,
int pcToPlcAreaType) {
this.plcToPc = new byte[plcToPcLength];
this.pcToPlc = new byte[pcToPlcLength];
this.PLCName = name;
this.PLCIp = ip;
this.moka = new S7Client();
this.moka.SetConnectionType(S7.OP);
this.plcToPcDb = plcToPcDb;
this.pcToPlcDb = pcToPlcDb;
this.boolBitChange = new HashMap<Double, Boolean>();
this.booleans = booleans;
this.pcToPlcLock = new Object();
this.plcToPcLock = new Object();
this.PLCSyncObj = new Object();
this.listeners = new ArrayList<PLCListener>();
this.rack = rack;
this.slot = slot;
this.pcToPlcAreaType = pcToPlcAreaType;
this.plcToPcAreaType = plcToPcAreaType;
}

public void processPLCEvents() {
this.getBoolChange();
}
Expand Down Expand Up @@ -124,10 +164,10 @@ public PLCStatus getStatus() {
public void refreshPLCStatus() {
// Update incoming
synchronized (this.plcToPcLock) {
this.moka.ReadArea(S7.S7AreaDB, this.plcToPcDb, 0, this.plcToPc.length, this.plcToPc);
this.moka.ReadArea(this.plcToPcAreaType, this.plcToPcDb, 0, this.plcToPc.length, this.plcToPc);
}
synchronized (this.pcToPlcLock) {
this.moka.WriteArea(S7.S7AreaDB, this.pcToPlcDb, 0, this.pcToPlc.length, this.pcToPlc);
this.moka.WriteArea(this.pcToPlcAreaType, this.pcToPlcDb, 0, this.pcToPlc.length, this.pcToPlc);
}
this.processPLCEvents();
}
Expand Down Expand Up @@ -449,13 +489,14 @@ public void run() {
try {
if (this.moka.Connected == false) {
this.connected = false;
this.moka.ConnectTo(this.PLCIp, 0, 1);
int error = this.moka.ConnectTo(this.PLCIp, this.rack, this.slot);
if (error > 0) {
logger.error(S7Client.ErrorText(error));
}
} else {
this.connected = true;
if (this.firstConnect == true) {
System.out.println("Connected to PLC: " + this.PLCIp);

this.moka.ReadArea(S7.S7AreaDB, this.pcToPlcDb, 0, this.pcToPlc.length, this.pcToPlc);
this.moka.ReadArea(this.pcToPlcAreaType, this.pcToPlcDb, 0, this.pcToPlc.length, this.pcToPlc);
this.pcToPlcLiveBit = System.nanoTime();
this.plcToPcLiveBit = System.nanoTime();
this.plcToPcLiveBitState = this.getBool(true, this.liveBitAddress, this.liveBitPosition);
Expand Down

0 comments on commit 86f794f

Please sign in to comment.