You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@plc4x.apache.org by Oles Bovsunivsky <ol...@gmail.com> on 2020/04/12 17:26:00 UTC

Need help)

Hi PLC4X team)
Could you help me?)
When I try read data from plc s7_300 (Vipa), I see this ->
[0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0] - This must be 5
How I can get 5 ?)

My code:
{
String connectionString = "s7://192.168.0.5/0/2";

        try (PlcConnection plcConnection = new
PlcDriverManager().getConnection(connectionString)) {

            PlcReadRequest.Builder builder =
plcConnection.readRequestBuilder();
            builder.addItem("value-1", "%IW14:WORD");
            builder.addItem("value-2", "%IW12:WORD");
            builder.addItem("value-3", "%IW14:WORD");
            builder.addItem("value-4", "%IW16:WORD");

            builder.addItem("value-6", "%MD272:DWORD");
            builder.addItem("value-7", "%MD276:DWORD");
            builder.addItem("value-8", "%MD284:DWORD");

            PlcReadRequest readRequest = builder.build();

            PlcReadResponse response = readRequest.execute().get();

            ArrayList a = new ArrayList();
            for (String fieldName : response.getFieldNames()) {
                if(response.getResponseCode(fieldName) ==
PlcResponseCode.OK) {

                    System.out.println("SECONR IF->>>>"+fieldName+"->>>>"+
response.getField(fieldName));
                    System.out.println("SECONR IF->>>>"+fieldName+"->>>>"+
response.getAllIntegers(fieldName));
                    System.out.println("SECONR IF->>>>"+fieldName+"->>>>"+
response.getObject(fieldName));
                    System.out.println("SECONR IF->>>>"+fieldName+"->>>>"+
response.getAllBytes(fieldName));
                    System.out.println("SECONR IF->>>>"+fieldName+"->>>>"+
response.getRequest());
                    System.out.println("SECONR IF->>>>"+fieldName+"->>>>"+
response.getResponseCode(fieldName));
                    System.out.println("SECONR IF->>>>"+fieldName+"->>>>"+
response.getNumberOfValues(fieldName));
                }

            }

        } catch (InterruptedException ex) {
            ex.printStackTrace();
        } catch (ExecutionException ex) {
            ex.printStackTrace();
        } catch (PlcConnectionException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
}
Thanks)

Re: Need help)

Posted by Christofer Dutz <ch...@c-ware.de>.
Hi Oles,

welcome on our friendly project list :-)

I am happy that your problem is a quite simple one. 
You are using "WORD" as a datatype. We tried to keep things close to this spec:
https://en.wikipedia.org/wiki/IEC_61131-3 which most PLC vendors seem to respect.

Here you can see that "WORD" and "DWORD" are considered Bit-Strings (Sequences of Boolean values)

If you want your values to be interpreted as integer values, use the integer types in your addresses instead:

WORD --> INT / UINT
DWORD -->  DINT / UDINT

That should be all.

Chris


Am 13.04.20, 00:18 schrieb "Oles Bovsunivsky" <ol...@gmail.com>:

    Hi PLC4X team)
    Could you help me?)
    When I try read data from plc s7_300 (Vipa), I see this ->
    [0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0] - This must be 5
    How I can get 5 ?)
    
    My code:
    {
    String connectionString = "s7://192.168.0.5/0/2";
    
            try (PlcConnection plcConnection = new
    PlcDriverManager().getConnection(connectionString)) {
    
                PlcReadRequest.Builder builder =
    plcConnection.readRequestBuilder();
                builder.addItem("value-1", "%IW14:WORD");
                builder.addItem("value-2", "%IW12:WORD");
                builder.addItem("value-3", "%IW14:WORD");
                builder.addItem("value-4", "%IW16:WORD");
    
                builder.addItem("value-6", "%MD272:DWORD");
                builder.addItem("value-7", "%MD276:DWORD");
                builder.addItem("value-8", "%MD284:DWORD");
    
                PlcReadRequest readRequest = builder.build();
    
                PlcReadResponse response = readRequest.execute().get();
    
                ArrayList a = new ArrayList();
                for (String fieldName : response.getFieldNames()) {
                    if(response.getResponseCode(fieldName) ==
    PlcResponseCode.OK) {
    
                        System.out.println("SECONR IF->>>>"+fieldName+"->>>>"+
    response.getField(fieldName));
                        System.out.println("SECONR IF->>>>"+fieldName+"->>>>"+
    response.getAllIntegers(fieldName));
                        System.out.println("SECONR IF->>>>"+fieldName+"->>>>"+
    response.getObject(fieldName));
                        System.out.println("SECONR IF->>>>"+fieldName+"->>>>"+
    response.getAllBytes(fieldName));
                        System.out.println("SECONR IF->>>>"+fieldName+"->>>>"+
    response.getRequest());
                        System.out.println("SECONR IF->>>>"+fieldName+"->>>>"+
    response.getResponseCode(fieldName));
                        System.out.println("SECONR IF->>>>"+fieldName+"->>>>"+
    response.getNumberOfValues(fieldName));
                    }
    
                }
    
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            } catch (ExecutionException ex) {
                ex.printStackTrace();
            } catch (PlcConnectionException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
    Thanks)