Skip to content

Commit

Permalink
Added S7-200 support
Browse files Browse the repository at this point in the history
  • Loading branch information
docbender committed Jan 29, 2019
1 parent 5a1343a commit 62763aa
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ public class SimaticActivator implements BundleActivator {

private static Logger logger = LoggerFactory.getLogger(SimaticActivator.class);

private final String VERSION = "1.2.0";

@Override
public void start(BundleContext context) throws Exception {
logger.debug("Simatic binding has been started.");
logger.debug("Simatic binding has been started (v.{}).", VERSION);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void activate(final BundleContext bundleContext, final Map<String, Object

Pattern rgxPLCKey = Pattern.compile("^plc\\d*$");
Pattern rgxPLCValue = Pattern
.compile("^((\\d{1,3}[.]\\d{1,3}[.]\\d{1,3}[.]\\d{1,3})[:]([0-2])[.](\\d{1,2})(:(OP|PG|S7))?)$");
.compile("^((\\d{1,3}[.]\\d{1,3}[.]\\d{1,3}[.]\\d{1,3})[:]([0-2])[.](\\d{1,2})(:(OP|PG|S7|200))?)$");

for (Map.Entry<String, Object> item : configuration.entrySet()) {
// port
Expand All @@ -111,7 +111,8 @@ public void activate(final BundleContext bundleContext, final Map<String, Object

if (!matcher.matches()) {
logger.error("{}: Wrong PLC configuration: {}", item.getKey(), plcString);
logger.info("PLC configuration example: plc=192.168.1.5:0.15 or plc1=192.168.1.5:0.1:OP");
logger.info(
"PLC configuration example: plc=192.168.1.5:0.15 or plc1=192.168.1.5:0.1:OP or plc1=192.168.1.5:0.1:200");
logger.debug("setProperlyConfigured: false");
setProperlyConfigured(false);
return;
Expand All @@ -121,11 +122,15 @@ public void activate(final BundleContext bundleContext, final Map<String, Object
devices.put(item.getKey(), new SimaticTCP(item.getKey(), matcher.group(2),
Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4))));
} else {
devices.put(item.getKey(),
new SimaticTCP(item.getKey(), matcher.group(2), Integer.parseInt(matcher.group(3)),
Integer.parseInt(matcher.group(4)), matcher.group(6)));
if (matcher.group(6).equals("200")) {
devices.put(item.getKey(), new SimaticTCP200(item.getKey(), matcher.group(2),
Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4))));
} else {
devices.put(item.getKey(),
new SimaticTCP(item.getKey(), matcher.group(2), Integer.parseInt(matcher.group(3)),
Integer.parseInt(matcher.group(4)), matcher.group(6)));
}
}

} else {
logger.error("Blank port configuration");
logger.debug("setProperlyConfigured: false");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public class SimaticTCP extends SimaticGenericDevice {
private static final Logger logger = LoggerFactory.getLogger(SimaticTCP.class);

/** address */
private String plcAddress = "";
protected String plcAddress = "";
/** rack/slot */
private final int rack, slot, communicationType;
protected final int rack, slot, communicationType;

Socket sock;
PLCinterface di;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* Copyright (c) 2010-2019, openHAB.org and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.simatic.internal;

import java.io.IOException;
import java.net.Socket;

import org.openhab.binding.simatic.internal.SimaticPortState.PortStates;
import org.openhab.binding.simatic.libnodave.Nodave;
import org.openhab.binding.simatic.libnodave.PLCinterface;
import org.openhab.binding.simatic.libnodave.TCP243Connection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* IP CP243 device class
*
* @author Vita Tucek
* @since 1.9.0
*/
public class SimaticTCP200 extends SimaticTCP {

private static final Logger logger = LoggerFactory.getLogger(SimaticTCP.class);

/**
* Constructor
*
* @param deviceName
* @param ip
* @param rack
* @param slot
*/
public SimaticTCP200(String deviceName, String ip, int rack, int slot) {
super(deviceName, ip, rack, slot);
// TODO Auto-generated constructor stub
}

/**
* Open socket
*
* @see org.openhab.binding.simplebinary.internal.SimaticIDevice#open()
*/
@Override
public Boolean open() {
if (logger.isDebugEnabled()) {
logger.debug("{} - open() - connecting CP243", this.toString());
}

portState.setState(PortStates.CLOSED);
// reset connected state
connected = false;

// open socket
try {
sock = new Socket(this.plcAddress, 102);
} catch (IOException e) {
logger.error("{} - create socket error: {}", this.toString(), e.getMessage());
return false;
}

if (sock == null) {
logger.error("{} - socket was not created (null returned)", this.toString());
return false;
}

try {
oStream = sock.getOutputStream();
} catch (IOException e) {
logger.error("{} - getOutputStream error: {}", this.toString(), e.getMessage());
return false;
}
try {
iStream = sock.getInputStream();
} catch (IOException e) {
logger.error("{} - getInputStream error: {}", this.toString(), e.getMessage());
return false;
}
di = new PLCinterface(oStream, iStream, "IF1", 0, Nodave.PROTOCOL_ISOTCP);

dc = new TCP243Connection(di, rack, slot);

try {
if (dc.connectPLC() == 0) {
if (logger.isInfoEnabled()) {
logger.info("{} - connected", this.toString());
}
portState.setState(PortStates.LISTENING);
tryReconnect.set(false);
connected = true;
} else {
logger.error("{} - cannot connect to PLC", this.toString());

return false;
}
} catch (IOException ex) {
logger.error("{} - cannot connect to PLC due: {}", this.toString(), ex.getMessage());

return false;
}

return true;
}
}

0 comments on commit 62763aa

Please sign in to comment.