Hi Iñigo,

welcome to the party ... I really hope we'll be able to get you started asap ...

I just had a look at the code and PlcInteger does have the constructor ...

    public PlcInteger(Short value) {
        super(value.intValue(), true);
    }

So I'll try to reproduce the problem ... but for now ... could you just remove 
the cast from your items?

    builderWriter.addItem("mivariable-1", "%DB20:DBX6.7:BOOL", true); 
    builderWriter.addItem("mivariable-4", "%DB20:DBW06.0:INT", 2);
    builderWriter.addItem("mivariable-2", "%DB20:DBB06:BYTE", 0);

Cause PLC4X internally already ensures everything fits into the bounds and 
internally PLC4X handles the "(short) 2" as an "(int) 2" anyway ;-)


Chris




Am 01.07.20, 11:19 schrieb "Iñigo Angulo" <iang...@zylk.net>:

    Hi, 

    I started doing some basic tests with the S7 protocol using a Siemens 
S7-300, and the latest 0.7.0 release. The tests consist of reading and writing 
several DB variables. I was able to read different datatypes, for instance: 

    builder.addItem("mivariable-1", "%DB20:DBX05.0:BOOL"); 
    builder.addItem("mivariable-4", "%DB20:DBW06:INT"); 

    However, when I try to write the same memory addresses I only manage to 
write BOOL values, for example 

    builderWriter.addItem("mivariable-1", "%DB20:DBX6.7:BOOL", true); //WORKS 
OK 
    builderWriter.addItem("mivariable-4", "%DB20:DBW06.0:INT", (short)2); // 
FAILS 
    builderWriter.addItem("mivariable-2", "%DB20:DBB06:BYTE", (byte)0); // 
FAILS 

    I am unable to write any datatype different than BOOL. 

    I am facing the following Error message: 

    Exception in thread "main" 
org.apache.plc4x.java.api.exceptions.PlcRuntimeException: Error initializing 
field class PlcInteger 
    at 
org.apache.plc4x.java.s7.readwrite.field.S7PlcFieldHandler.internalEncodeInteger(S7PlcFieldHandler.java:394)
 
    at 
org.apache.plc4x.java.s7.readwrite.field.S7PlcFieldHandler.encodeShort(S7PlcFieldHandler.java:86)
 
    at 
org.apache.plc4x.java.spi.messages.DefaultPlcWriteRequest$Builder.lambda$build$0(DefaultPlcWriteRequest.java:270)
 
    at java.base/java.util.TreeMap.forEach(TreeMap.java:1002) 
    at 
org.apache.plc4x.java.spi.messages.DefaultPlcWriteRequest$Builder.build(DefaultPlcWriteRequest.java:265)
 
    at net.zylk.plc4x.test.protocols.WriteS7.main(WriteS7.java:102) 
    Caused by: java.lang.NoSuchMethodException: 
org.apache.plc4x.java.api.value.PlcInteger.<init>([Ljava.lang.Short;) 
    at java.base/java.lang.Class.getConstructor0(Class.java:3349) 
    at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2553) 
    at 
org.apache.plc4x.java.s7.readwrite.field.S7PlcFieldHandler.internalEncodeInteger(S7PlcFieldHandler.java:392)
 
    ... 5 more 

    Maybe I am misunderstanding the sintax and how the values should be 
written? 

    --- 
    To try to offer some more information about this error: 
    Digging into that class (S7PlcFieldHandler) I saw that there are some 
fields that are initialized based on the value of the datatype 

    case INT: 
    minValue = BigInteger.valueOf(Short.MIN_VALUE); 
    maxValue = BigInteger.valueOf(Short.MAX_VALUE); 
    fieldType = PlcInteger.class; 
    valueType = Short[].class; 
    castedValues = new Short[values.length]; 
    break; 

    Then, the line that throws the Exception seems to be trying to inferr a 
PlcInteger class constructor based on the datatype 

    // Create the field item. 
    try { 
    return 
fieldType.getDeclaredConstructor(valueType).newInstance(castedValues); 
    } catch (InstantiationException | IllegalAccessException | 
InvocationTargetException | NoSuchMethodException e) { 
    throw new PlcRuntimeException("Error initializing field class " + 
fieldType.getSimpleName(), e); 
    } 

    However there appears to be a "mismatch" between the valueType (Short[]) 
and the constructor (Short) in the PlcInteger class, which leads me to think i 
am missing something on the write value sintax? 

    Thank you in advance, any help will be welcome. 

    Iñigo 



    ----- 
    the test class code: 

    public class WriteS7 { 
    private static final Logger _log = LoggerFactory.getLogger(WriteS7.class); 

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

    String connectionString = 
"s7://10.105.143.1:102?remote-rack=0&remote-slot=0&controller-type=S7_300"; 

    _log.info(String.format("Establishing connection to %s", 
connectionString));; 

    try (PlcConnection plcConnection = new 
PlcDriverManager().getConnection(connectionString)) { 
    _log.info("Conecting... " + connectionString); 
    if (!plcConnection.getMetadata().canWrite()) { 
    _log.error(String.format("Connection %s doesn't support writing.", 
connectionString)); 
    return; 
    } 

    PlcWriteRequest.Builder builderWriter = null; 
    builderWriter = plcConnection.writeRequestBuilder(); 

    // builderWriter.addItem("mivariable-26", "%DB20:DBX6.7:BOOL", false); //OK 
    builderWriter.addItem("mivariable-2", "%DB20:DBB06:BYTE", (byte)0); //FAILS 
    // builderWriter.addItem("mivariable-4", "%DB20:DBW06.0:INT", (short)2); 
//FAILS 


    PlcWriteRequest writeRequest = builderWriter.build(); 
    PlcWriteResponse writeResponse = writeRequest.execute().get(); 

    GeneralFunctions.printPlc4XWriteReponse(writeResponse); //print response 
    } 

    } 
    } 


    ----------------------------------------- 
    Iñigo Angulo 

    ZYLK.net :: consultoría.openSource 
    Ribera de Axpe, 11 
    Edificio A, modulo 201-203 
    48950 Erandio (Bizkaia) 
    +34 944272119 
    ----------------------------------------- 

Reply via email to