AdsBinaryReader.ReadPlcAnsiString Method

Reads a PLC string from the current stream (Encoding.Default, ANSI Encoding)

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

Syntax

C#

public string ReadPlcAnsiString(
    int byteLength
)

VB

Public Function ReadPlcAnsiString ( 
    byteLength As Integer
) As String

Parameters

byteLength

Type: System.Int32
The length of the string in the PLC (byte length equals character count on PLC + '\0')

Return Value

Type: String
The string being read (until the first '\0' character)

Remarks

The byte length of a STRING[80] in the PLC is 81. The byte length of a WSTRING[80] in the PLC is 162. Because of ANSI Encoding the number of Chars could differ with the number of Bytes (e.g on Double Byte Codepages DBCS, Codepage 932, Japan)

Examples

The following code shows how to Read/Write ANSI string values..

Read/Write Strings

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

    int handle = client.CreateVariableHandle("MAIN.string"); // Symbol "string" in MAIN defined as string

    try
    {
    // Read ANSI String string[80]
    int byteSize = 81; // Size of 80 ANSI chars + /0 (STRING[80])
    AdsStream readStream = new AdsStream(byteSize); // Size of 80 ANSI chars + /0 (STRING[80])
    AdsBinaryReader reader = new AdsBinaryReader(readStream);
    client.Read(handle, readStream, 0, byteSize); // Read 81 bytes
    string value = reader.ReadPlcString(byteSize,Encoding.Default);

    // Write ANSI String string[80]
    AdsStream writeStream = new AdsStream(byteSize);
    AdsBinaryWriter writer = new AdsBinaryWriter(writeStream);
    value = "Changed";
    writer.WritePlcString(value, 80,Encoding.Default); // Max 80 characters!
    client.Write(handle, writeStream, 0, byteSize);
    }
    finally
    {
    client.DeleteVariableHandle(handle);
    }
}

Reference

AdsBinaryReader Class

TwinCAT.Ads Namespace