TcAdsClient.WriteAnyString Method (Int32, String, Int32, Encoding)

Writes the string (Potentially unsafe!)

Namespace:  TwinCAT.Ads
Assembly:  TwinCAT.Ads (in TwinCAT.Ads.dll) Version: 4.3.0.0

Syntax

C#

public void WriteAnyString(
    int variableHandle,
    string value,
    int length,
    Encoding encoding
)

VB

Public Sub WriteAnyString ( 
    variableHandle As Integer,
    value As String,
    length As Integer,
    encoding As Encoding
)

Parameters

variableHandle

Type: System.Int32
The variable handle.

value

Type: System.String
The value.

length

Type: System.Int32
The length of the string to write

encoding

Type: System.Text.Encoding
The encoding.

Exceptions

Exception

Condition

NotImplementedException

Remarks

Note

Data can be overwritten

Potentially this method is unsafe because following data can be overwritten after the string symbol. Please be sure to specify the string length lower than the string size reserved within the process image!

Examples

The following code shows how to Read/Write string values with the ANY concept.

Read/Write Any Strings

using (TcAdsClient client = new TcAdsClient())
{
    client.Connect(851); // Connect to local port 851 (PLC)

    int stringHandle = client.CreateVariableHandle("MAIN.string"); // Symbol "string" defined in MAIN as STRING
    int wStringHandle = client.CreateVariableHandle("MAIN.wstring"); // Symbol "string" defined in MAIN as WSTRING

    try
    {
    string str = client.ReadAnyString(stringHandle, 80, Encoding.Default);
    string wStr = client.ReadAnyString(wStringHandle, 80, Encoding.Unicode);

    string changedValue = "Changed";

    // Attention, take care that the memory of the string in the process image is not exceeded!
    client.WriteAnyString(stringHandle, changedValue, 80, Encoding.Default);
    client.WriteAnyString(wStringHandle, changedValue, 80, Encoding.Unicode);
    }
    finally
    {
    client.DeleteVariableHandle(stringHandle);
    client.DeleteVariableHandle(wStringHandle);
    }
}

Reference

TcAdsClient Class

WriteAnyString Overload

TwinCAT.Ads Namespace