Access Data via IndexGroup/IndexOffset

Reading/Writing values by Index/Group index offset are the most basic way to access data via ADS. This address combination directly link into the process image of virtual ADS Devices.

As long the process image is static this is unproblematic and a system near access, but if the content is more dynamic and the address changes over time the IndexGroup/IndexOffset can get invalid.

Examples about moving addresses could be:

In that case other access methods could be advantageous.

Another important point is that the data access is not type safe. The values are read or written to or from byte buffers and the proper marshalling/unmarshalling is the task of the application code.

Asynchronous access

Access ProcessImage Data by IndexGroup/IndexOffset

CancellationToken cancel = CancellationToken.None;

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

    client.Connect(AmsNetId.Local, 851);

    byte[] writeData = new byte[sizeof(uint)];

    // Write an UINT32 Value
    MemoryStream writeStream = new MemoryStream(writeData);
    BinaryWriter writer = new BinaryWriter(writeStream);
    writer.Write(valueToWrite);
    ResultWrite resultWrite = await client.WriteAsync(0x4020, 0x0, writeData.AsMemory(),cancel);

    // Read an UINT32 Value
    byte[] readData = new byte[sizeof(uint)];
    ResultRead resultRead = await client.ReadAsync(0x4020, 0x0, readData.AsMemory(),cancel);

    MemoryStream readStream = new MemoryStream(readData);
    BinaryReader reader = new BinaryReader(readStream);
    valueToRead = reader.ReadUInt32();
}

Synchronous access

Access ProcessImage Data by IndexGroup/IndexOffset

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

    client.Connect(AmsNetId.Local, 851);

    // Write an UINT32 Value
    byte[] writeData = new byte[sizeof(uint)];
    MemoryStream writeStream = new MemoryStream(writeData);
    BinaryWriter writer = new BinaryWriter(writeStream);
    writer.Write(valueToWrite);
    client.Write(0x4020, 0x0, writeData);

    // Read an UINT32 Value
    byte[] readData = new byte[sizeof(uint)];
    int readBytes = client.Read(0x4020, 0x0, readData);

    MemoryStream readStream = new MemoryStream(readData);
    readStream.Position = 0;
    BinaryReader reader = new BinaryReader(readStream);
    valueToRead = reader.ReadUInt32();
}