Skip to content

Optimization using SymbolLoaderFactory #5

@dfreiberger

Description

@dfreiberger

In using this library for reading/writing a very large nested structure, I found that significant improvements in read/write time (up to 92% speedup on writes to a structure of 21kb, from 68 seconds down to 5 seconds) can be made by utilizing SymbolLoaderFactory to pre-load the datatypes for the PLC prior to executing read and write operations. This looks like:

private async Task InitializeClient(AmsNetId amsNetId, int adsPort, string CallerSource)
{
    // Initialize the ADS client
    client = new AdsClient();
    client.Connect(amsNetId, adsPort);
    
    // Load datatypes
    var loader = SymbolLoaderFactory.Create(client, SymbolLoaderSettings.DefaultDynamic);
    var symbolsResult = await loader.GetSymbolsAsync(cancel);
    
    _symbols = symbolsResult.Symbols;
...

Elsewhere I wrote custom functions to read the object as json and to "merge" a JObject back into the DynamicValue prior to writing to the PLC.

public async Task<string> ReadObject(string objectPath)
{
    var symbol = (DynamicSymbol)_symbols[objectPath];
    
    var readResult = await symbol.ReadValueAsync(cancel);
    
    // try to convert to json
    var jsonObject = JObject.FromObject(readResult.Value).ToString(Formatting.Indented);
...

private async Task WriteJson(string objectPath, JObject writeObject)
{
        var symbol = (DynamicSymbol)_symbols[objectPath];

        var readResult = await symbol.ReadValueAsync(cancel);

        var dynamicValue = (DynamicValue)readResult.Value;
        dynamicValue.UpdateMode = ValueUpdateMode.Triggered; // necessary so that setting values internally does not trigger ADS writes

        // Merge the JSON objects recursively
        MergeJObjectIntoDynamicValue(dynamicValue, writeObject);

        // Write the JSON object to the PLC
        await symbol.WriteValueAsync(managedResult, cancel);
...

This basically replaces most of the functions in the TcJsonSettings library, except for TryConvertToPlcOpenType and logic to merge a JObject with a DynamicValue.

Does it make sense to try to bring this optimization into this library? If so, I am not sure on first glance how to handle this since there needs to be a persistent reference to the symbol maintained somewhere that these methods could reference. Do you have suggestions on this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions