Read/Write primitive values

Reading writing Values from ADS Devices is the most essential part of the communication API. There are several options for communication with your application.

The following section shows the different scenarios as code snippets.

HowTo Read/Write Values

Read/Write AnyType by IndexGroup/IndexOffset

using (TcAdsClient client = new TcAdsClient())
{
    UInt32 valueToRead = 0;
    UInt32 valueToWrite = 42;

    client.Connect(AmsNetId.Local, 851);
    adsClient.WriteAny(0x4020, 0x0, valueToWrite);
    valueToRead = (uint)adsClient.ReadAny(0x4020, 0x0, typeof(UInt32));
}

Read/Write AnyType by variable handle

using (TcAdsClient client = new TcAdsClient())
{
    int varHandle = 0;
    client.Connect(AmsNetId.Local, 851);
    try
    {
    UInt32 valueToRead = 0;
    UInt32 valueToWrite = 42;

    varHandle = client.CreateVariableHandle("MAIN.nCounter");
    adsClient.WriteAny(varHandle, valueToWrite);
    valueToRead = (uint)adsClient.ReadAny(varHandle, typeof(UInt32));
    }
    finally
    {
    // Unregister VarHandle after Use
    client.DeleteVariableHandle(varHandle);
    }
}

Read/Write AnyType by instance/symbol path

using (TcAdsClient client = new TcAdsClient())
{
    UInt32 valueToRead = 0;
    UInt32 valueToWrite = 42;

    client.Connect(AmsNetId.Local, 851);
    adsClient.WriteSymbol("MAIN.nCounter", valueToWrite, false);
    valueToRead = (uint)adsClient.ReadSymbol("MAIN.nCounter", typeof(UInt32),false);
}

Read/Write AnyType by IAdsSymbol

using (TcAdsClient client = new TcAdsClient())
{
    UInt32 valueToRead = 0;
    UInt32 valueToWrite = 42;

    client.Connect(AmsNetId.Local, 851);

    ITcAdsSymbol symbol = adsClient.ReadSymbolInfo("MAIN.nCounter");
    adsClient.WriteSymbol(symbol, valueToWrite);
    valueToRead = (uint)adsClient.ReadSymbol(symbol);
}

Read/Write AnyType by SymbolBrowser

using (TcAdsClient client = new TcAdsClient())
{
    UInt32 valueToRead = 0;
    UInt32 valueToWrite = 42;

    client.Connect(AmsNetId.Local, 851);

    // Load all Symbols + DataTypes
    ISymbolLoader loader = SymbolLoaderFactory.Create(client, SymbolLoaderSettings.Default);
    Symbol symbol = (Symbol)loader.Symbols["MAIN.nCounter"];

    // Works for ALL Primitive 'ANY TYPES' Symbols
    symbol.WriteValue(valueToWrite); 
    valueToRead = (UInt32)symbol.ReadValue();
}

Read/Write dynamic types by SymbolBrowser

using (TcAdsClient client = new TcAdsClient())
{
    UInt32 valueToRead = 0;
    UInt32 valueToWrite = 42;

    client.Connect(AmsNetId.Local, 851);

    // Load all Symbols + DataTypes
    // Primitive Parts will be automatically resolved to .NET Primitive types.
    IDynamicSymbolLoader loader = (IDynamicSymbolLoader)SymbolLoaderFactory.Create(client, SymbolLoaderSettings.DefaultDynamic);

    dynamic symbols = loader.SymbolsDynamic;
    dynamic main = symbols.Main;

    // Use typed object to use InfoTips
    DynamicSymbol nCounter = main.nCounter;

    // or to be fullDynamic 
    //dynamic nCounter = main.nCounter;

    // Works for ALL sorts of types (not restricted to ANY_TYPE basing primitive types)
    nCounter.WriteValue(valueToWrite);
    valueToRead = (uint)nCounter.ReadValue();
}