Skip to content

Commit

Permalink
When creating the data queue the PDU size is taken into account
Browse files Browse the repository at this point in the history
  • Loading branch information
docbender committed Nov 26, 2019
1 parent 62763aa commit bece24a
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion org.openhab.binding.simatic/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Bundle-License: http://www.eclipse.org/legal/epl-v10.html
Bundle-Name: openHAB Simatic Binding
Bundle-SymbolicName: org.openhab.binding.simatic
Bundle-Vendor: openHAB.org
Bundle-Version: 1.9.0.qualifier
Bundle-Version: 1.14.0.qualifier
Bundle-ManifestVersion: 2
Bundle-Description: This is the Simatic binding of the open Home Aut
omation Bus (openHAB)
Expand Down
2 changes: 1 addition & 1 deletion org.openhab.binding.simatic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.openhab.bundles</groupId>
<artifactId>binding</artifactId>
<version>1.9.0-SNAPSHOT</version>
<version>1.14.0-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* like querying a Website/Device.
*
* @author VitaTucek
* @since 1.9.0
* @since 1.14.0
*/
public class SimaticBinding extends AbstractActiveBinding<SimaticBindingProvider> {

Expand Down Expand Up @@ -145,7 +145,6 @@ public void activate(final BundleContext bundleContext, final Map<String, Object

for (Map.Entry<String, SimaticGenericDevice> item : devices.entrySet()) {
item.getValue().setBindingData(eventPublisher, items, infoItems);
item.getValue().prepareData();
item.getValue().open();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
* Generic device class
*
* @author Vita Tucek
* @since 1.9.0
* @since 1.14.0
*/
public class SimaticGenericDevice implements SimaticIDevice {
private static final Logger logger = LoggerFactory.getLogger(SimaticGenericDevice.class);
Expand Down Expand Up @@ -81,6 +81,9 @@ public class SimaticGenericDevice implements SimaticIDevice {
protected SimaticReadQueue readAreasList = new SimaticReadQueue();
/** try reconnect flag when read/write function failure **/
protected AtomicBoolean tryReconnect = new AtomicBoolean(false);
/** PDU size **/
protected int pduSize = 0;


public enum ProcessDataResult {
OK,
Expand Down Expand Up @@ -229,7 +232,7 @@ public void reconnectWithDelaying() {
@Override
public void sendData(String itemName, Command command, SimaticBindingConfig config) {

sendData(SimaticWriteDataArea.create(command, config));
sendData(SimaticWriteDataArea.create(command, config, pduSize));
}

/**
Expand Down Expand Up @@ -405,7 +408,7 @@ public void prepareData() {
}

if (readDataArea == null || readDataArea.isItemOutOfRange(item.getValue().getAddress())) {
readDataArea = new SimaticReadDataArea(item.getValue());
readDataArea = new SimaticReadDataArea(item.getValue(),pduSize);
readAreasList.put(readDataArea);
} else {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
* Read / write area interface
*
* @author Vita Tucek
* @since 1.9.0
* @since 1.14.0
*/
public interface SimaticIReadWriteDataArea {
/** Maximum bytes transfered in one data frame **/
public static final int MAX_DATA_LENGTH = 192;
public static final int MAX_PDU480_DATA_LENGTH = 462;

/**
* Return PLC area type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Read / write area class
*
* @author Vita Tucek
* @since 1.9.0
* @since 1.14.0
*/
public class SimaticReadDataArea implements SimaticIReadWriteDataArea {
/** Maximum space between two useful data block **/
Expand All @@ -26,12 +26,17 @@ public class SimaticReadDataArea implements SimaticIReadWriteDataArea {
LinkedList<SimaticBindingConfig> items = new LinkedList<SimaticBindingConfig>();
final SimaticPLCAddress startAddress;
int areaLength = 0;
/** data limit PDU size depending **/
int dataLimit = MAX_DATA_LENGTH;

public SimaticReadDataArea(SimaticBindingConfig firstItem) {
public SimaticReadDataArea(SimaticBindingConfig firstItem, int pduSize) {
startAddress = firstItem.getAddress();
items.add(firstItem);

areaLength = startAddress.getDataLength();
if(pduSize >= 480) {
dataLimit = MAX_PDU480_DATA_LENGTH;
}
}

@Override
Expand Down Expand Up @@ -117,7 +122,7 @@ public boolean isItemOutOfRange(SimaticPLCAddress itemAddress) {
return itemAddress.getArea() != this.getArea()
|| (this.getArea() == SimaticPLCAreaTypes.DB && !startAddress.DBNum.equals(itemAddress.DBNum))
|| (itemAddress.addressByte + itemAddress.getDataLength()
- this.startAddress.addressByte > MAX_DATA_LENGTH)
- this.startAddress.addressByte > dataLimit)
|| (itemAddress.addressByte
- (this.startAddress.addressByte + this.getAddressSpaceLength()) > GAP_LIMIT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* IP device class
*
* @author Vita Tucek
* @since 1.9.0
* @since 1.14.0
*/
public class SimaticTCP extends SimaticGenericDevice {

Expand Down Expand Up @@ -163,10 +163,13 @@ public Boolean open() {
try {
if (dc.connectPLC() == 0) {
if (logger.isInfoEnabled()) {
logger.info("{} - connected", this.toString());
logger.info("{} - connected. Max PDU size = {}", this.toString(), dc.maxPDUlength);
}
pduSize = dc.maxPDUlength;
portState.setState(PortStates.LISTENING);
tryReconnect.set(false);
//prepare data after PDU is negotiated
prepareData();
connected = true;
} else {
logger.error("{} - cannot connect to PLC", this.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@
* Class holding single write data request
*
* @author Vita Tucek
* @since 1.9.0
* @since 1.14.0
*/
public class SimaticWriteDataArea implements SimaticIReadWriteDataArea {

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

static int INCREASE_STEP = 5;
/** data limit PDU size depending **/
int dataLimit = MAX_DATA_LENGTH;

public static SimaticWriteDataArea create(Command command, SimaticBindingConfig config) {
public static SimaticWriteDataArea create(Command command, SimaticBindingConfig config, int pduSize) {
if (logger.isDebugEnabled()) {
logger.debug("create(): item:" + config.getName() + "|datatype:" + config.getDataType());
}
Expand Down Expand Up @@ -342,7 +344,7 @@ public static SimaticWriteDataArea create(Command command, SimaticBindingConfig
return null;
}

return new SimaticWriteDataArea(config.getAddress(), data);
return new SimaticWriteDataArea(config.getAddress(), data, pduSize);

}

Expand All @@ -355,9 +357,12 @@ public static SimaticWriteDataArea create(Command command, SimaticBindingConfig
* @param itemData
* Raw data
*/
public SimaticWriteDataArea(SimaticPLCAddress address, byte[] itemData) {
public SimaticWriteDataArea(SimaticPLCAddress address, byte[] itemData, int pduSize) {
this.address = address;
this.itemData = itemData;
if(pduSize >= 480) {
dataLimit = MAX_PDU480_DATA_LENGTH;
}
}

/**
Expand Down Expand Up @@ -474,9 +479,9 @@ public boolean isItemOutOfRange(SimaticPLCAddress itemAddress) {
|| this.address.getSimaticDataType() == SimaticPLCDataTypes.BIT
|| (itemAddress.addressByte > (this.address.addressByte + this.address.getDataLength()))
|| ((itemAddress.addressByte + itemAddress.getDataLength()) < this.address.addressByte)
|| (itemAddress.addressByte + itemAddress.getDataLength() - this.address.addressByte > MAX_DATA_LENGTH)
|| (itemAddress.addressByte + itemAddress.getDataLength() - this.address.addressByte > dataLimit)
|| (this.address.addressByte + this.getAddressSpaceLength()
- itemAddress.addressByte > MAX_DATA_LENGTH);
- itemAddress.addressByte > dataLimit);
}

public void insert(SimaticWriteDataArea data) {
Expand Down

0 comments on commit bece24a

Please sign in to comment.