Read/Write PlcOpen types (DATE, TIME ...)

The following PLCOpen types have specific representations within the TwinCAT.Ads Communication Library: DT

The following section shows the different scenarios as code snippets.

HowTo Read/Write PlcOpen values

Reading writing by streams:

Read/Write PlcOpen types (streamed, async)

CancellationToken cancel = CancellationToken.None;

using (AdsClient client = new AdsClient())
{
    client.Connect(AmsNetId.Local, 851); // Connect to local plc

    ResultHandle resultHandleTime = await client.CreateVariableHandleAsync("MAIN.time", cancel); // TIME
    ResultHandle resultHandleLTime = await client.CreateVariableHandleAsync("MAIN.lTime", cancel); // LTIME
    ResultHandle resultHandleDate = await client.CreateVariableHandleAsync("MAIN.date", cancel); // DATE

    if (resultHandleTime.Succeeded && resultHandleLTime.Succeeded && resultHandleDate.Succeeded)
    {
    try
    {
        byte[] readBuffer = new byte[LTIME.MarshalSize]; // Largest PlcOpen Type is 8 Bytes
        byte[] writeBuffer = new byte[LTIME.MarshalSize];

        // Reading raw value TIME
        await client.ReadAsync(resultHandleTime.Handle, readBuffer.AsMemory(0, TIME.MarshalSize), cancel);

        // Unmarshalling
        TIME plcTime = null;
        PrimitiveTypeMarshaler.Default.Unmarshal(readBuffer.AsSpan(0, TIME.MarshalSize), out plcTime);
        TimeSpan time = plcTime.Time;

        // Writing raw value TIME
        PrimitiveTypeMarshaler.Default.Marshal(time, writeBuffer.AsSpan());
        await client.WriteAsync(resultHandleTime.Handle, writeBuffer.AsMemory(0, TIME.MarshalSize), cancel);

        // Reading raw value LTIME
        await client.ReadAsync(resultHandleLTime.Handle, readBuffer.AsMemory(0, LTIME.MarshalSize), cancel);

        // Unmarshalling
        LTIME plcLTime = null;
        PrimitiveTypeMarshaler.Default.Unmarshal(readBuffer.AsSpan(0, LTIME.MarshalSize), out plcLTime);
        TimeSpan lTime = plcLTime.Time;

        // Writing raw value LTIME
        PrimitiveTypeMarshaler.Default.Marshal(lTime, writeBuffer.AsSpan());
        await client.WriteAsync(resultHandleLTime.Handle, writeBuffer.AsMemory(0, LTIME.MarshalSize), cancel);

        // Reading raw value DATE
        DATE plcDate = null;
        await client.ReadAsync(resultHandleDate.Handle, readBuffer.AsMemory(0, DATE.MarshalSize), cancel);

        // Unmarshalling
        PrimitiveTypeMarshaler.Default.Unmarshal(readBuffer.AsSpan(0, DATE.MarshalSize), out plcDate);
        DateTimeOffset dateTime = plcDate.Date;

        // Writeing raw value DATE
        PrimitiveTypeMarshaler.Default.Marshal(plcDate, writeBuffer.AsSpan());
        await client.WriteAsync(resultHandleDate.Handle, writeBuffer.AsMemory(0, DATE.MarshalSize), cancel);
    }
    finally
    {
        ResultAds r1 = await client.DeleteVariableHandleAsync(resultHandleLTime.Handle, cancel);
        ResultAds r2 = await client.DeleteVariableHandleAsync(resultHandleTime.Handle, cancel);
        ResultAds r3 = await client.DeleteVariableHandleAsync(resultHandleDate.Handle, cancel);
    }
    }
}

Read/Write PlcOpen types (streamed, sync)

using (AdsClient client = new AdsClient())
{
    client.Connect(AmsNetId.Local, 851); // Connect to local plc

    uint handleTime = 0;
    uint handleLTime = 0;
    uint handleDate = 0;

    try
    {
        handleTime = client.CreateVariableHandle("MAIN.time"); // TIME
        handleLTime = client.CreateVariableHandle("MAIN.lTime"); // LTIME
        handleDate = client.CreateVariableHandle("MAIN.date"); // DATE

        byte[] readBuffer = new byte[LTIME.MarshalSize]; // Largest PlcOpen Type is 8 Bytes
        byte[] writeBuffer = new byte[LTIME.MarshalSize];

        // Reading raw value TIME
        client.Read(handleTime, readBuffer.AsMemory(0,TIME.MarshalSize));

        // Unmarshalling
        TIME plcTime = null;
        PrimitiveTypeMarshaler.Default.Unmarshal(readBuffer.AsSpan(0, TIME.MarshalSize), out plcTime);
        TimeSpan time = plcTime.Time;

        // Writing raw value TIME
        PrimitiveTypeMarshaler.Default.Marshal(time, writeBuffer.AsSpan());
        client.Write(handleTime, writeBuffer.AsMemory(0,TIME.MarshalSize));

        // Reading raw value LTIME
        client.Read(handleLTime, readBuffer.AsMemory(0, LTIME.MarshalSize));

        // Unmarshalling
        LTIME plcLTime = null;
        PrimitiveTypeMarshaler.Default.Unmarshal(readBuffer.AsSpan(0, LTIME.MarshalSize), out plcLTime);
        TimeSpan lTime = plcLTime.Time;

        // Writing raw value LTIME
        PrimitiveTypeMarshaler.Default.Marshal(lTime, writeBuffer.AsSpan());
        client.Write(handleLTime, writeBuffer.AsMemory(0, LTIME.MarshalSize));

        // Reading raw value DATE
        DATE plcDate = null;
        client.Read(handleDate, readBuffer.AsMemory(0, DATE.MarshalSize));

        // Unmarshalling
    PrimitiveTypeMarshaler.Default.Unmarshal(readBuffer.AsSpan(0, DATE.MarshalSize), out plcDate);
    DateTimeOffset dateTime = plcDate.Date;

        // Writeing raw value DATE
        PrimitiveTypeMarshaler.Default.Marshal(plcDate, writeBuffer.AsSpan());
        client.Write(handleDate, writeBuffer.AsMemory(0, DATE.MarshalSize));
    }
    finally
    {
        client.DeleteVariableHandle(handleLTime);
        client.DeleteVariableHandle(handleTime);
        client.DeleteVariableHandle(handleDate);
    }
}

Reading writing by ANY type concept:

Read/Write PlcOpen types (ANY, async)

CancellationToken cancel = CancellationToken.None;

using (AdsClient client = new AdsClient())
{
    client.Connect(AmsNetId.Local, 851); // Connect to local plc

    ResultHandle resultHandleTime = await client.CreateVariableHandleAsync("MAIN.time",cancel); // TIME
    ResultHandle resultHandleDate = await client.CreateVariableHandleAsync("MAIN.date", cancel); // DATE
    ResultHandle resultHandleLTime = await client.CreateVariableHandleAsync("MAIN.lTime", cancel); // LTIME

    if (resultHandleTime.Succeeded && resultHandleDate.Succeeded && resultHandleLTime.Succeeded)
    {
        try
        {
            ResultAnyValue resultTime = await client.ReadAnyAsync(resultHandleTime.Handle, typeof(TIME), cancel); // TIME

            TIME time = (TIME)resultTime.Value;
            TimeSpan timeSpan = time.Time;

            await client.WriteAnyAsync(resultHandleTime.Handle, time, cancel);

            ResultAnyValue resultData = await client.ReadAnyAsync(resultHandleDate.Handle, typeof(DATE), cancel); // DATE
            DATE date = (DATE)resultTime.Value;
            DateTimeOffset dateTime = date.Date;

            await client.WriteAnyAsync(resultHandleDate.Handle, date, cancel);

            ResultAnyValue resultLTime = await client.ReadAnyAsync(resultHandleLTime.Handle, typeof(LTIME), cancel); // LTIME
            LTIME lTime = (LTIME)resultTime.Value;
            TimeSpan lTimeSpan = lTime.Time;

            await client.WriteAnyAsync(resultHandleLTime.Handle, lTime, cancel);
        }
        finally
        {
            ResultAds r1 = await client.DeleteVariableHandleAsync(resultHandleTime.Handle,cancel);
            ResultAds r2 = await client.DeleteVariableHandleAsync(resultHandleDate.Handle,cancel);
            ResultAds r3 = await client.DeleteVariableHandleAsync(resultHandleLTime.Handle,cancel);
        }
    }
}

Read/Write PlcOpen types (ANY, sync)

using (AdsClient client = new AdsClient())
{
    client.Connect(AmsNetId.Local, 851); // Connect to local plc

    uint handleTime = 0;
    uint handleDate = 0;
    uint handleLTime = 0;

    try
    {
        handleTime = client.CreateVariableHandle("MAIN.time"); // TIME
        handleDate = client.CreateVariableHandle("MAIN.date"); // DATE
        handleLTime = client.CreateVariableHandle("MAIN.lTime"); // LTIME

        TIME time = (TIME)client.ReadAny(handleTime, typeof(TIME)); // TIME
        TimeSpan timeSpan = time.Time;
        client.WriteAny(handleTime, time);

        DATE date = (DATE)client.ReadAny(handleDate, typeof(DATE)); // DATE
        DateTimeOffset dateTime = date.Date;
        client.WriteAny(handleDate, date);

        LTIME ltime = (LTIME)client.ReadAny(handleLTime, typeof(LTIME)); // LTIME
        TimeSpan lTimeSpan = ltime.Time;
        client.WriteAny(handleLTime, ltime);
    }
    finally
    {
        client.DeleteVariableHandle(handleTime);
        client.DeleteVariableHandle(handleDate);
        client.DeleteVariableHandle(handleLTime);
    }
}

Beckhoff Automation GmbH & Co. KG 2001-2026