Example usage ‒ GDS services

This section contains a few examples for the usage of the Java RSC API.

Note:
The Java RSC API will soon be replaced by a better solution. Therefore the support for this tool is already discontinued. If you decide to use the Java RSC API anyway you'll be on your own.

See section RSC (Remote Service Calls) for further information of available RSC services and their usage.

Read variables:

You can access data which are available in the Global Data Space (GDS) with the com.phoenixcontact.arp.plc.gds.services.IDataAccessService. This example shows how you can read a value.

try (ServiceManager serviceManager = new ServiceManager()) {
      serviceManager.connect(connectionInfo, securityInfo);
      IDataAccessService service = serviceManager.getService(IDataAccessService.class);
      // Full variable name uri.
      // ComponentName-1/ProgramName-1.Variable_Name
      // ComponentName-1/Global_Variable_Name
      // ComponentName-1/ProgramName-1.Array_Variable_Name
      // ComponentName-1/ProgramName-1.Array_Variable_Name[index]
      // ComponentName-1/ProgramName-1.Struct_Variable_Name.Element1.Leaf
      // ComponentName-1/ProgramName-1.Struct_Variable_Name.Element1.LeafArray
      // ComponentName-1/ProgramName-1.Struct_Variable_Name.Element1.LeafArray[index]
      String portName = "Arp.Plc.Esm/ESM_DATA.ESM_COUNT";
      // com.phoenixcontact.arp.plc.gds.services.ReadItem
      // read variable
      ReadItem item = service.readSingle(portName);
      if(DataAccessError.NONE.equals(item.getError())) {
        // com.phoenixcontact.ade.commonremoting.utils.AdeObject
        AdeObject obj = item.getValue();
        // print value.
        System.out.println("Value: " + obj.getValue());
        // print data type.
        System.out.println("Data type: " + obj.getCoreType());
      } else {
        // An error occurred.
        // print error type
        System.out.println("Error: " + item.getError());
      }
}

Write variables:

Like reading, you can use the com.phoenixcontact.arp.plc.gds.services.IDataAccessService to write data to the GDS. The following examples are for writing different data types.

String:

try (ServiceManager serviceManager = new ServiceManager()) {
      serviceManager.connect(connectionInfo, securityInfo);
      IDataAccessService service = serviceManager.getService(IDataAccessService.class);
      WriteItem writeItem = new WriteItem();
      // Full variable name uri.
      // ComponentName-1/ProgramName-1.Variable_Name
      // ComponentName-1/Global_Variable_Name
      // ComponentName-1/ProgramName-1.Array_Variable_Name
      // ComponentName-1/ProgramName-1.Array_Variable_Name[index]
      // ComponentName-1/ProgramName-1.Struct_Variable_Name.Element1.Leaf
      // ComponentName-1/ProgramName-1.Struct_Variable_Name.Element1.LeafArray
      // ComponentName-1/ProgramName-1.Struct_Variable_Name.Element1.LeafArray[index]
      String portName = "URI of a writeable variable";
      writeItem.setPortName(portName);
      AdeObject value = new AdeObject();
      // The default encoding for Strings will be UTF16. If you want to use a different encoding you
      // must define the CoreType.
      value.setCoreType(CoreType.UTF8_STRING);
      value.setValue("Test string");
      writeItem.setValue(value);
      // write variable
      DataAccessError error = service.writeSingle(writeItem);
      if (!DataAccessError.NONE.equals(error)) {
        // An error occurred.
        // print error type
        System.out.println("Error: " + error);
      }
}

Unsigned types: 

try (ServiceManager serviceManager = new ServiceManager()) {
      serviceManager.connect(connectionInfo, securityInfo);
      IDataAccessService service = serviceManager.getService(IDataAccessService.class);
      WriteItem writeItem = new WriteItem();
      // Full variable name uri.
      // ComponentName-1/ProgramName-1.Variable_Name
      // ComponentName-1/Global_Variable_Name
      // ComponentName-1/ProgramName-1.Array_Variable_Name
      // ComponentName-1/ProgramName-1.Array_Variable_Name[index]
      // ComponentName-1/ProgramName-1.Struct_Variable_Name.Element1.Leaf
      // ComponentName-1/ProgramName-1.Struct_Variable_Name.Element1.LeafArray
      // ComponentName-1/ProgramName-1.Struct_Variable_Name.Element1.LeafArray[index]
      String portName = "URI of a writeable variable";
      writeItem.setPortName(portName);
      AdeObject value = new AdeObject();
      // For unsigned data types you must define the core type.
      value.setCoreType(CoreType.U1);
      // Remember that unsigned types require the bigger data type e.g. uint 8 requires int16 (short)
      value.setValue((short) 0xFF);
      writeItem.setValue(value);
      // write variable
      DataAccessError error = service.writeSingle(writeItem);
      if (!DataAccessError.NONE.equals(error)) {
        // An error occurred.
        // print error type
        System.out.println("Error: " + error);
      }
    }
}

Project download

The com.phoenixcontact.arp.system.commons.services.io.IFileService allows you to upload a file to the device. The methods provided by the FileService are for uploading the project files to the device.
The com.phoenixcontact.arp.plc.domain.services.IPlcManagerService2 will be used to stop the current project and load the new project.

try (ServiceManager serviceManager = new ServiceManager()) {
      serviceManager.connect(connectionInfo, securityInfo);
      IPlcManagerService2 plcService = serviceManager.getService(IPlcManagerService2.class);
      // Stop program
      plcService.stop(false);
      plcService.reset(false);
      // Upload project file generated by PLCnext Engineer.
      String projectPath = "/opt/plcnext/projects/PCWE";
      String projectFile = projectPath + "/thefilename";
      String sourceFile = "your/file/to/upload";
      IFileService fileService = serviceManager.getService(IFileService.class);
      try (FileInputStream data = new FileInputStream(sourceFile)) {
        boolean overwrite = true;
        error = fileService.write(projectFile, overwrite, new TraitItem[0], data);
        if (FileSystemError.NONE.equals(error)) {
          System.out.println("success");
        } else {
          // An error occurred.
          // print error type
          System.out.println("Error: " + error);
        }
      }
      plcService.load(false);
      plcService.start(PlcStartKind.WARM, false);
}

Project download change

The download change is performed similar to a normal download. Instead of stopping and starting the program, you call the plcService.change(false).

try (ServiceManager serviceManager = new ServiceManager()) {
      serviceManager.connect(connectionInfo, securityInfo);
      IPlcManagerService2 plcService = serviceManager.getService(IPlcManagerService2.class);
      // Upload project file generated by PLCnext Engineer.
      String projectPath = "/opt/plcnext/projects/PCWE";
      String projectFile = projectPath + "/thefilename";
      String sourceFile = "your/file/to/upload";
      IFileService fileService = serviceManager.getService(IFileService.class);
      try (FileInputStream data = new FileInputStream(sourceFile)) {
        boolean overwrite = true;
        error = fileService.write(projectFile, overwrite, new TraitItem[0], data);
        if (FileSystemError.NONE.equals(error)) {
          System.out.println("success");
        } else {
          // An error occurred.
          // print error type
          System.out.println("Error: " + error);
        }
      }
      plcService.change(false);
}

 

 

 


• Published/reviewed: 2024-05-06   ★  Revision 068 •