Hi,

I have been doing further tests with the S7 driver. I would like to ask you 
some doubts, which i am not sure if relate to the PLC4X driver or the actual 
hardware (Siemens S7-300) i am using (pretty novice in PLC data access im 
afraid...)


The "Read value" tests I have done work correctly for different datatypes, for 
example:

        builderReader.addItem("mivariable", "%DB20:DBX5.0:BOOL"); //Single 
boolean value
        builderReader.addItem("mivariable", "%DB20:DBB06:BYTE"); //returns a 
PlcList (Array of 8 boolean values)
        builderReader.addItem("mivariable", "%DB20:DBW06:WORD"); //returns a 
PlcList (Array of 16 boolean values)
        builderReader.addItem("mivariable", "%DB20:DBD06:DWORD"); //returns a 
PlcList (Array of 32 boolean values)
        builderReader.addItem("mivariable", "%DB20:DBW06:INT"); //returns a 
PlcInteger (16 bit integer (signed))
        builderReader.addItem("mivariable", "%DB20:DBD06:DINT"); //returns a 
PlcInteger (32 bit integer (signed))
        builderReader.addItem("mivariable", "%DB20:DBD06:REAL"); //returns a 
PlcFloat (32 bit IEEE 754 full precision floating point value (signed))


In the "Write value" tests, write request for all datatype seem to work fine:

        builderWriter.addItem("mivariable", "%DB20:DBX6.1:BOOL", true); //write 
response code "OK"
        builderWriter.addItem("mivariable", "%DB20:DBB06:BYTE", 127); //write 
response code "OK"
        builderWriter.addItem("mivariable", "%DB20:DBW06:WORD", 7); //write 
response code "OK"
        builderWriter.addItem("mivariable", "%DB20:DBD06:DWORD", 1); //write 
response code "OK"
                        

Except for the 'INT' and 'DINT' datatypes, which return an INTERNAL_ERROR 
response code

        builderWriter.addItem("mivariable", "%DB20:DBW06:INT", 1); //write 
response code "INTERNAL_ERROR"
        builderWriter.addItem("mivariable", "%DB20:DBD06:DINT", 1); //write 
response code "INTERNAL_ERROR"


Is the INTERNAL_ERROR code a hardware issue? (Maybe a device configuration 
problem?)

---

Also, the other thing I found in the test:

When I write a value different than BOOL, and then read the same memory 
address, I dont get the actual value but a 'false' filled array. TO give some 
examples,

with BOOL values:

        builderWriter.addItem("mivariable", "%DB20:DBX6.1:BOOL", true); 
        builderReader.addItem("mivariable", "%DB20:DBX6.1:BOOL"); //return true

        builderWriter.addItem("mivariable", "%DB20:DBX6.1:BOOL", false); 
        builderReader.addItem("mivariable", "%DB20:DBX6.1:BOOL"); //return false

The written value is "correctly updated" and read fine.

with other datatypes:

        builderWriter.addItem("mivariable", "%DB20:DBB06:BYTE", 1);
        builderReader.addItem("mivariable", "%DB20:DBB06:BYTE"); //returns 
[false, false, false, false, false, false, false, false]

        builderWriter.addItem("mivariable", "%DB20:DBB06:BYTE", 127);
        builderReader.addItem("mivariable", "%DB20:DBB06:BYTE"); //returns 
[false, false, false, false, false, false, false, false]


I have tried to combine write single bits and read other datatypes, which seem 
to work fine:

        builderWriter.addItem("mivariable", "%DB20:DBX6.1:BOOL", true);
        builderReader.addItem("mivariable", "%DB20:DBB06:BYTE"); //returns 
[false, false, false, false, false, false, true, false]
        builderReader.addItem("mivariable", "%DB20:DBW06:INT"); //return 512
        
And the other way around, write a byte and read single bits

        builderWriter.addItem("mivariable", "%DB20:DBB06:BYTE", 127);
        builderReader.addItem("mivariable-0", "%DB20:DBX6.0:BOOL"); //returns 
false
        builderReader.addItem("mivariable-1", "%DB20:DBX6.1:BOOL"); //returns 
false
        builderReader.addItem("mivariable-2", "%DB20:DBX6.2:BOOL"); //returns 
false
        builderReader.addItem("mivariable-3", "%DB20:DBX6.3:BOOL"); //returns 
false
        builderReader.addItem("mivariable-4", "%DB20:DBX6.4:BOOL"); //returns 
false
        builderReader.addItem("mivariable-5", "%DB20:DBX6.5:BOOL"); //returns 
false
        builderReader.addItem("mivariable-6", "%DB20:DBX6.6:BOOL"); //returns 
false
        builderReader.addItem("mivariable-7", "%DB20:DBX6.7:BOOL"); //returns 
false

Which leads me to think that could be a write issue, right?

Again I understand that these issues could be an actual hardware configuration 
problem.. but any help or advice would be much appreciated

Thank you in advance!

iñigo 
        
P.S. I attached the test classes if anyone wants to take a look.

----------------------------------------- 
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: "Iñigo Angulo Otegui" <iang...@zylk.net>
Para: "dev" <dev@plc4x.apache.org>
Enviados: Jueves, 2 de Julio 2020 12:45:49
Asunto: Re: S7 write test doubt

Hi Julian,

Yes, we saw the adopters page and were thinking to contribute there. We will 
prepare a short text describing the work we are doing with the project and send 
it to you soon.

Thanks 
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: "Julian Feinauer" <j.feina...@pragmaticminds.de>
Para: "dev" <dev@plc4x.apache.org>
Enviados: Jueves, 2 de Julio 2020 12:00:53
Asunto: Re: S7 write test doubt

And, as i shortly checked your company profile which looks pretty cool... if 
you like it, or use it, dont forget to enter your company on the adopters page 
: )

https://plc4x.apache.org/users/adopters.html

Julian

Am 02.07.20, 10:31 schrieb "Iñigo Angulo" <iang...@zylk.net>:

    Hi Chris,

    Yes, using the 0.8.0-SNAPSHOT with the update you did the problem is solved 
now. 

    Thank you for the help and the fast solution!

    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 13:18:34
    Asunto: Re: S7 write test doubt

    Hi all,

    build passed locall, pushed the changes, build on ci passed .... so you 
should be ready to give things a new try.

    However keep in mind, if you built with 0.8.0-SNAPSHOT once today, please 
be sure to do a "mvn -U install" build or you won't get the update today.

    Would be cool if you could reply if this helped.

    Chris



    Am 01.07.20, 12:30 schrieb "Julian Feinauer" <j.feina...@pragmaticminds.de>:

        Thanks both of you!
        As I just wrote in Slack we can only grow and improve if we get 
feedback, Bug reports and usage scenarios.
        And of course thanks Chris for that fast reply and fix <3

        Julian

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

            Hi Chris,

            No problem. I will keep track of the issue in Jira, and use the 
0.8.0-SNAPSHOT version.

            Thank you for your help

            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 12:06:28
            Asunto: Re: S7 write test doubt

            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 
                        -----------------------------------------
package net.zylk.plc4x.test.protocols;

import org.apache.plc4x.java.PlcDriverManager;
import org.apache.plc4x.java.api.PlcConnection;
import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
import org.apache.plc4x.java.api.messages.PlcReadRequest;
import org.apache.plc4x.java.api.messages.PlcReadResponse;
import org.apache.plc4x.java.api.types.PlcResponseCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ReadS7 {
	private static final Logger _log = LoggerFactory.getLogger(ReadS7.class);
	
	public static void main(String[] args) throws PlcConnectionException, Exception {
		
		/** 0.6.0 */
		//String ip = "10.105.143.1";
		//String slot = "0";
		//String rack = "0";
		//String connectionString = String.format("s7://%s/%s/%s?controller-type=S7_300", ip, slot, rack);
		
		
		/** 0.7.0 */
		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)) {

			// check if can read
			if (!plcConnection.getMetadata().canRead()) {
				_log.error(String.format("Connection %s doesn't support reading.", connectionString));
				return;
			}

			PlcReadRequest.Builder builderReader = null;
			builderReader = plcConnection.readRequestBuilder();

						
			/** DOCU: https://plc4x.apache.org/users/protocols/s7.html DataTypes */
			
			/** read bit */ 
			//builderReader.addItem("mivariable", "%DB20:DBX6.1:BOOL"); //Single boolean value
			
			/** read byte */
			//builderReader.addItem("mivariable", "%DB20:DBB06:BYTE"); //returns a PlcList (Array of 8 boolean values)
						
			/** read word */
			//builderReader.addItem("mivariable", "%DB20:DBW06:WORD"); //returns a PlcList (Array of 16 boolean values)
			
			/** read dword */
			//builderReader.addItem("mivariable", "%DB20:DBD06:DWORD"); //returns a PlcList (Array of 32 boolean values)
			
			/** read int */
			//builderReader.addItem("mivariable", "%DB20:DBW06:INT"); //returns a PlcInteger (16 bit integer (signed))

			/** read dint */
			//builderReader.addItem("mivariable", "%DB20:DBD06:DINT"); //returns a PlcInteger (32 bit integer (signed))
						
			/** read real */
			//builderReader.addItem("mivariable", "%DB20:DBD06:REAL"); //returns a PlcFloat (32 bit IEEE 754 full precision floating point value (signed))

			
			
			
			/** read byte, bit a bit */
			//builderReader.addItem("mivariable", "%DB20:DBB06:BYTE"); //returns a PlcList (Array of 8 boolean values)
//			builderReader.addItem("mivariable-0", "%DB20:DBX6.0:BOOL");
//			builderReader.addItem("mivariable-1", "%DB20:DBX6.1:BOOL");
//			builderReader.addItem("mivariable-2", "%DB20:DBX6.2:BOOL");
//			builderReader.addItem("mivariable-3", "%DB20:DBX6.3:BOOL");
//			builderReader.addItem("mivariable-4", "%DB20:DBX6.4:BOOL");
//			builderReader.addItem("mivariable-5", "%DB20:DBX6.5:BOOL");
//			builderReader.addItem("mivariable-6", "%DB20:DBX6.6:BOOL");
//			builderReader.addItem("mivariable-7", "%DB20:DBX6.7:BOOL");
			
			
			/** COUNTER READ */
//			builderReader.addItem("value-1", "%DB10.DBW10:INT"); // %DB10.DBW10:INT  equals  %DB10:10:INT
			
			
			
			
			
			PlcReadRequest readRequest = builderReader.build();
			PlcReadResponse readResponse = readRequest.execute().get();
//			GeneralFunctions.printPlc4XReadReponse(readResponse);
			
			for (String fieldName : readResponse.getFieldNames()) {
			    if(readResponse.getResponseCode(fieldName) == PlcResponseCode.OK) {
			        System.out.println("Value["+fieldName+"]: " + readResponse.getObject(fieldName).getClass() + ": " + readResponse.getObject(fieldName));
			    }
			    // Something went wrong, to output an error message instead.
			    else {
			    	System.out.println("Error[" + fieldName + "]: " + readResponse.getResponseCode(fieldName).name());
			    }
			}
			
		}
	}
	
	
	
	
	/**
	 * The memory area:
	 * 		Data Block:[DB] DB areas are the most common place to store data required by the different functions of the device, these data block are numbered which is part of the address.
	 * 		(others)
	 *          Merker:[M] arbitrary marker variables or flag registers reside here.
	 *          Input:[I] digital and analog input module values, mapped into memory.
	 *          Output:[Q] similarly memory mapped outputs.
	 *          Counter:[C] values of different counters used by the PLC program.
	 *          Timer:[T] values of different timers used by the PLC program.
	 *          There are other less common memory areas as well (such as local data [L] and peripheral access [P] and so on).
	 *          
	 * The type of the variable determines its length and how it should be interpreted. A few examples are:
	 * 			BIT:[X] a single bit. -> in PLC4x is called BOOL (see org.apache.plc4x.java.s7.netty.model.types.TransportSize;)
	 * 			WORD: two bytes wide unsigned integer.
	 * 			DINT: four bytes wide signed integer.
	 * 			REAL: four bytes wide IEEE floating point number.
	 * 			COUNTER: counter type used by the PLC program counters.
	 * 
	 * ----
	 * PLC4X S7
	 * 
	 * In general all S7 addresses have this format:
	 * 		%{Memory-Area}{start-address}:{Data-Type}[{array-size}]
	 * 		(If the array-part is omitted, the size-default of 1 is assumed.)
	 * 
	 * Generally there are two types of addresses:
	 *     Bit-Addresses {Memory-Area-Code}{Start-Byte-Address}.{Bit-Offset}:BOOL[{Count}]
	 *     Byte-Addresses {Memory-Area-Code}{Start-Byte-Address}:{Data-Type-Code}[{count}]
	 *     
	 * Bit addresses are only used if the datatype: BOOL is used.
	 * The array notation of these can be omitted. In this case a Count of 1 is used per default.
	 * Start-Byte-Address and Bit-Offset in above list both represent unsigned integer values.
	 * 
	 * In case of accessing data in the data block memory area, the syntax is quite a bit more complex:
	 *     DB{Data-Block-Number}.DB{Short-Data-Type-Code}{Start-Byte-Address}.{Bit-Offset}:BOOL[{Count}]
	 *     DB{Data-Block-Number}.DB{Short-Data-Type-Code}{Start-Byte-Address}:{Data-Type-Code}[{Count}]
	 *     
	 * These addresses can usually be copied directly out of TIA portal. However we also implemented a shorter version, as above version does have some unnecesary boilerplate parts (The .DB in the middle as well as the Short-Data-Type-Code)
	 * 
	 * The shorter syntax looks like this:
	 * 
	 * DB{Data-Block-Number}.{Start-Byte-Address}.{Bit-Offset}:BOOL[{Count}]
	 * DB{Data-Block-Number}.{Start-Byte-Address}:{Data-Type-Code}[{Count}]
	 * 
	 * The S7 driver will handle both types of notation equally.
	 * 
	 *  * 
	 * 
	 * 
	 * 
	 */
	
}

package net.zylk.plc4x.test.protocols;

import org.apache.plc4x.java.PlcDriverManager;
import org.apache.plc4x.java.api.PlcConnection;
import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
import org.apache.plc4x.java.api.messages.PlcWriteRequest;
import org.apache.plc4x.java.api.messages.PlcWriteResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


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

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

		/** 0.6.0 */
//		String ip = "10.105.143.1";
//		String slot = "0";
//		String rack = "0";
//		String connectionString = String.format("s7://%s/%s/%s?controller-type=S7_300", ip, slot, rack);
		
		/** 0.7.0 */
		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();			
	
			/** write bit */
			builderWriter.addItem("mivariable", "%DB20:DBX6.1:BOOL", true); //OK
			
			/** write byte */
			//builderWriter.addItem("mivariable", "%DB20:DBB06:BYTE", 127); //write request returns OK (but when read it gives a 'false' filled array)
			
			/** write word */
			//builderWriter.addItem("mivariable", "%DB20:DBW06:WORD", 7); //write request returns OK (but when read it gives a 'false' filled array)
			
			/** write dword */
			//builderWriter.addItem("mivariable", "%DB20:DBD06:DWORD", 1); //write request returns OK (but when read it gives a 'false' filled array)
			
			/** write int */
			//builderWriter.addItem("mivariable", "%DB20:DBW06:INT", 1); //write request returns INTERNAL_ERROR

			/** write dint */
			//builderWriter.addItem("mivariable", "%DB20:DBD06:DINT", 1); //write request returns INTERNAL_ERROR
			
			/** write real */
			//builderWriter.addItem("mivariable", "%DB20:DBD06:REAL", 1); //error 
				/** error message:
				 * Exception in thread "main" org.apache.plc4x.java.api.exceptions.PlcRuntimeException: Invalid encoder for type REAL
				 * 		at org.apache.plc4x.java.s7.readwrite.field.S7PlcFieldHandler.encodeInteger(S7PlcFieldHandler.java:130)
				 */
			
			

			PlcWriteRequest writeRequest = builderWriter.build();
			PlcWriteResponse writeResponse = writeRequest.execute().get();
			
			
			
			for (String fieldName : writeResponse.getFieldNames()) {
				_log.info(String.format("%s -> %s ", fieldName, writeResponse.getResponseCode(fieldName)));
			}
			
		}
		
	}
	
	
}

Reply via email to