Skip to content

Commit

Permalink
Adds COUNTER type for read/write. (#1850)
Browse files Browse the repository at this point in the history
Co-authored-by: César García <[email protected]>
  • Loading branch information
glcj and ceos01 authored Nov 1, 2024
1 parent 6a68573 commit 6037919
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -802,9 +802,10 @@ public enum KnxManufacturer {
M_XIAMEN_LEELEN_TECHNOLOGY_CO__LTD_(
(int) 689, (int) 747, (String) "Xiamen Leelen Technology Co.,Ltd."),
M_LEDNX((int) 690, (int) 748, (String) "LedNX"),
M_ABB___RESERVED((int) 691, (int) 43954, (String) "ABB - reserved"),
M_EBELONG((int) 691, (int) 749, (String) "ebelong"),
M_ABB___RESERVED((int) 692, (int) 43954, (String) "ABB - reserved"),
M_BUSCH_JAEGER_ELEKTRO___RESERVED(
(int) 692, (int) 43959, (String) "Busch-Jaeger Elektro - reserved");
(int) 693, (int) 43959, (String) "Busch-Jaeger Elektro - reserved");
private static final Map<Integer, KnxManufacturer> map;

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,19 @@ public enum TransportSize {
DataTransportSize.BYTE_WORD_DWORD,
(String) "IEC61131_ULINT",
TransportSize.INT),
COUNTER(
(short) 0x1C,
(boolean) true,
(boolean) false,
(short) 0x1C,
(short) 2,
(boolean) true,
(boolean) true,
(short) 'X',
(boolean) true,
DataTransportSize.OCTET_STRING,
(String) "IEC61131_INT",
null),
REAL(
(short) 0x0E,
(boolean) true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,22 @@ public S7Tag(TransportSize dataType, MemoryArea memoryArea,
this.dataType = dataType;
this.memoryArea = memoryArea;
this.blockNumber = blockNumber;
this.byteOffset = byteOffset;
this.bitOffset = bitOffset;
this.numElements = numElements;

//TODO: Should this address conversion be done in the mspec?
switch (dataType) {
case COUNTER: {
this.bitOffset = (byte) ((byteOffset) & 0x0007);
this.byteOffset = (byteOffset >> 3);
break;
}
default :{
this.byteOffset = byteOffset;
this.bitOffset = bitOffset;
}

}

}

@Override
Expand All @@ -97,6 +110,8 @@ public PlcValueType getPlcValueType() {
return PlcValueType.DATE_AND_LTIME;
case "DTL":
return PlcValueType.DATE_AND_LTIME;
case "COUNTER":
return PlcValueType.WORD;
default:
return PlcValueType.valueOf(dataType.name());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@ public class DatatypesTest {

public static void main(String[] args) throws Exception {
//try (PlcConnection connection = new DefaultPlcDriverManager().getConnection("s7://192.168.24.83")) {
try (PlcConnection connection = new DefaultPlcDriverManager().getConnection("s7://192.168.23.30")) {
//
String URL1 = "s7://192.168.23.30";

//
String URL2 = "s7://192.168.0.47?remote-rack=0&"
+ "remote-slot=3&"
+ "controller-type=S7_400&read-timeout=8&"
+ "ping=false&ping-time=2&retry-time=3";


try (PlcConnection connection = new DefaultPlcDriverManager().getConnection(URL1)) {
final PlcReadRequest.Builder builder = connection.readRequestBuilder();
builder.addTagAddress("bool-value-1", "%DB2:0.0:BOOL"); // true
builder.addTagAddress("bool-value-2", "%DB2:2.1:BOOL"); // false
Expand All @@ -53,9 +63,12 @@ public static void main(String[] args) throws Exception {
builder.addTagAddress("udint-array", "%DB2:50:UDINT[2]"); // 12345, 23456
builder.addTagAddress("real-value", "%DB2:58:REAL"); // 3.14159
builder.addTagAddress("real-array", "%DB2:62:REAL[2]"); // 12.345, 12.345
builder.addTagAddress("lreal-value", "%DB2:70:LREAL"); // 3.14159265358979
builder.addTagAddress("lreal-array", "%DB2:78:LREAL[2]"); // 1.2345, -1.2345

// builder.addTagAddress("lreal-value", "%DB2:70:LREAL"); // 3.14159265358979
// builder.addTagAddress("lreal-array", "%DB2:78:LREAL[2]"); // 1.2345, -1.2345

builder.addTagAddress("string-value", "%DB2:94:STRING(10)"); // "Hurz"

// When reading a sized STRING array, this has to be translated into multiple items
//builder.addField("string-array", "%DB2:350:STRING(10)[2]"); // "Wolf", "Lamm"
builder.addTagAddress("time-value", "%DB2:862:TIME"); // 1234ms
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.plc4x.java.s7.readwrite;

import org.apache.plc4x.java.DefaultPlcDriverManager;
import org.apache.plc4x.java.api.PlcConnection;
import org.apache.plc4x.java.api.messages.PlcReadRequest;
import org.apache.plc4x.java.api.messages.PlcReadResponse;
import org.apache.plc4x.java.api.messages.PlcWriteRequest;
import org.apache.plc4x.java.api.messages.PlcWriteResponse;
import org.apache.plc4x.java.api.types.PlcResponseCode;
import org.apache.plc4x.java.api.value.PlcValue;

public class ManualS7CounterTest {

public static void main(String[] args) throws Exception {

//Maybe a S7-1200
String URL1 = "s7://192.168.23.30";

//S7-400
String URL2 = "s7://192.168.0.47?remote-rack=0&"
+ "remote-slot=3&"
+ "controller-type=S7_400&read-timeout=8&"
+ "ping=false&ping-time=2&retry-time=3";


try (PlcConnection connection = new DefaultPlcDriverManager().getConnection(URL2)) {
final PlcReadRequest.Builder readBuilder = connection.readRequestBuilder();

final PlcWriteRequest.Builder writeBuilder = connection.writeRequestBuilder();

writeBuilder.addTagAddress("counter-0", "%C0:COUNTER", Integer.decode("0x0123"));
writeBuilder.addTagAddress("counter-1", "%C9:COUNTER", Integer.decode("0x0456"));
writeBuilder.addTagAddress("counter-2", "%C12:COUNTER", Integer.decode("0x0789"));
writeBuilder.addTagAddress("counter-3", "%C18:COUNTER", Integer.decode("0x0012"));

final PlcWriteRequest writeRequest = writeBuilder.build();
final PlcWriteResponse writeResposne = writeRequest.execute().get();

if ( writeResposne.getResponseCode("counter-3") == PlcResponseCode.OK ){
System.out.println("Write the counter");
} else {
System.out.println("Problems....");
}

readBuilder.addTagAddress("counter-0", "%C0:COUNTER"); // Set this counter to 123
readBuilder.addTagAddress("counter-1", "%C9:COUNTER"); // Set this counter to 456
readBuilder.addTagAddress("counter-2", "%C12:COUNTER"); // Set this counter to 789
readBuilder.addTagAddress("counter-3", "%C18:COUNTER"); // Set this counter to 012
readBuilder.addTagAddress("counters", "%C0:COUNTER[20]"); // Set this counter to 000

final PlcReadRequest readRequest = readBuilder.build();

final PlcReadResponse readResponse = readRequest.execute().get();

System.out.println(readResponse);

byte[] responseBytes = readResponse.getPlcValue("counter-0").getRaw();

short bcd_0 = readResponse.getShort("counter-0");
short bcd_1 = readResponse.getShort("counter-1");
short bcd_2 = readResponse.getShort("counter-2");
short bcd_3 = readResponse.getShort("counter-3");
System.out.println("counter-0 = " + convertShortToBcd(bcd_0));
System.out.println("counter-1 = " + convertShortToBcd(bcd_1));
System.out.println("counter-2 = " + convertShortToBcd(bcd_2));
System.out.println("counter-3 = " + convertShortToBcd(bcd_3));

PlcValue plcValues = readResponse.getPlcValue("counters");

System.out.println(plcValues.toString());

if (plcValues.isList()) {
System.out.println(plcValues.getIndex(0).getShort() == bcd_0);
System.out.println(plcValues.getIndex(9).getShort() == bcd_1);
System.out.println(plcValues.getIndex(12).getShort() == bcd_2);
System.out.println(plcValues.getIndex(18).getShort() == bcd_3);
}


}
}


private static short convertShortToBcd(short incomingShort) {
return (short) (((incomingShort >> 8) & 0x0F) * 100 +
((incomingShort >> 4) & 0x0F) * 10 +
(incomingShort & 0x000f));
}

}
1 change: 1 addition & 0 deletions protocols/s7/src/main/resources/protocols/s7/s7.mspec
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,7 @@
['0x0B' UDINT ['0x07' , 'D' , '4' , 'INT' , 'INTEGER' , 'IEC61131_UDINT' , 'false' , 'false' , 'true' , 'true' , 'true' ]]
['0x0C' LINT ['0x00' , 'X' , '8' , 'INT' , 'BYTE_WORD_DWORD' , 'IEC61131_LINT' , 'false' , 'false' , 'false' , 'true' , 'false' ]]
['0x0D' ULINT ['0x00' , 'X' , '8' , 'INT' , 'BYTE_WORD_DWORD' , 'IEC61131_ULINT' , 'false' , 'false' , 'false' , 'true' , 'false' ]]
['0x1C' COUNTER ['0x1C' , 'X' , '2' , 'null' , 'OCTET_STRING' , 'IEC61131_INT' , 'true' , 'true' , 'true' , 'true' , 'false' ]]

// Floating point values
['0x0E' REAL ['0x08' , 'D' , '4' , 'null' , 'REAL' , 'IEC61131_REAL' , 'true' , 'true' , 'true' , 'true' , 'true' ]]
Expand Down

0 comments on commit 6037919

Please sign in to comment.