Hi Iñigo,

I will have to ask you to wait for a little while as you stumbled over a 
problem which goes a little deeper and is related to a major refactoring we did 
just before Christmas. I am pretty puzzled that no one ever noticed this, but 
I'll immediately start working on fixing this.

I'm tracking progress here: 
https://issues.apache.org/jira/browse/PLC4X-206


If that's done I would like to ask you to use the 0.8.0-SNAPSHOT till we've 
released the next version of PLC4X.

Chris


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

    Hi Chris,

    Thanks for the fast answer!

    I will keep your advice about not adding the cast to the values, and 
letting the driver do its work.

    If you want me to try to reproduce any other test just let me know.

    Iñigo


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

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

    ----- Mensaje original -----
    De: "Christofer Dutz" <christofer.d...@c-ware.de>
    Para: "dev" <dev@plc4x.apache.org>
    Enviados: Miércoles, 1 de Julio 2020 11:48:48
    Asunto: Re: S7 write test doubt

    And let me give you a little more context ... with the INT type in S7 you 
happen to know that there an INT is a 16 Bit signed integer.

    We wanted to abstract from that. Cause another PLC could define INT as 32 
bit integer ... So you can generally just work with any numeric type and pass 
that in. The driver will inspect the value ranges for that particular protocol 
and give some sensible error messages (At least it should ;-) ).

    But ... I just noticed that indeed I am seeing the same problems you are 
seeing and that my suggestion just causes a different error ... I'll get 
working on fixing this right away.

    I'm really a bit confused about this ... but it does prove that most people 
just care about reading ;-)

    Will keep you and the others posted on any progress.

    Chris



    Am 01.07.20, 11:33 schrieb "Christofer Dutz" <christofer.d...@c-ware.de>:

        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