You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

Api Change - Chris Copy

General

  • I ensured all of our classes have the "Plc" prefix, as I did encounter situations in which very generic type names had collisions and I had to use the fully qualified class name (including the package name). The "Plc" prefix reduces this risk.
  • Removed the "XYZItems"
  • Introduced a PlcFieldRequest / Response classes
  • TO BE CONTINUED ...

PlcField (Aka Address)

  • I Renamed Address to PlcField
  • I still think there is no immediate need to define any fields or methods in this interface as I haven't seen any common ground yet. Even if we are currently adding "TYPE" information to most fields, in Modbus the type is directly tied to the address you are reading. So If you are reading a "Register" or a "Coil", the datatype is fixed.

PlcNamedField

  • New Class building a unity of named fields.
  • This class is only used for constructing the PlcFieldRequest objects as it allows passing in pairs of name and field.

PlcConnection

  • I removed the "getLister" method as I haven't seen any protocol or use-case where this actually made sense.

  • I changed the "parseAddress" to "prepareField". This abstract method has to be implemented by every Driver and it handles parsing of the field description for each protocol. Some times I could imagine, that more work is involved than simple parsing of a string (For Beckhoff ADS and EtherNet/IP this method could actively connect to the corresponding resource so in future read operations only the connection-id has to be provided)

PlcRequest & PlcResponse

  • TO BE CONTINUED ... 

Example Code

Here comes some example code to demonstrate how a Programm using this API could look like.

public static void main(String[] args) throws Exception {
    PlcDriverManager manager = new PlcDriverManager();
    try (PlcConnection conn = manager.getConnection("s7://192.168.167.211/0/0")) {
        Optional<PlcReader> optionalReader = conn.getReader();
        if (!optionalReader.isPresent()) {
            return;
        }
 
        PlcReader Reader = optionalReader.get();
 
        PlcReadRequest request = new PlcReadRequest(
            new PlcNamedField("parameterX", conn.prepareField("%DB8.DBX3:INT")),
            new PlcNamedField("parameterY", conn.prepareField("%DB9.DBW4:DOUBLE")),
            new PlcNamedField("others", conn.prepareField("%DB2.DBW2:BYTE[10]")));
 
        CompletableFuture<PlcReadResponse> future = reader.read(request);
 
        PlcReadResponse response = future.get();
 

        // Access an item by it's name.
        System.out.println("Parameter X: " + response.getInteger("parameterX"));
 
        // Iterate over all items
        for(String fieldName: response.getFieldNames()) {
			switch(fieldName) {
				case "parameterY": 
	                System.out.println("Parameter y: " + response.getDouble(fieldName));
					break;
				case "others": 
	                for(int i = 0; i < response.getNumValues(fieldName); i++) {
    	                // Intentionally using short instead of byte
        	            // In this case it will return Byte[i] but convert that into a Short.
            	        System.out.println(" - " + response.getShort(fieldName, i));
                	}
					break;
			}
        }
    }
}




  • No labels