readString
[ Function ]
public readString(length?: number): string;
Reads bytes and interprets them as UTF-8 encoded Unicode code points. Bytes are read until a byte with the value null is read, the number of bytes read reaches the value of the parameter length
or the end of the data is reached. If the optional parameter length
has been set, the read pointer is advanced by the value of length
, otherwise the read pointer is advanced by the actual number of bytes read, either after the position of the null byte or at the end of the data.
When reading strings from the PLC, please note that TwinCAT reserves one byte more than the specified string length, in order to have space for a final null byte. If, for example, a string is to be read that was declared in the PLC with length n, it must be read with the length n+1.
Parameter
Name | Type | Description |
---|---|---|
length [ optional ] | The length of the string to be read. If a byte with the value null is read before the specified length is reached, the read process is terminated, but the read pointer is still advanced by the specified length. |
Return value
Type | Description |
---|---|
The read string |
Sample - JavaScript
// Contains a string, followed by seven null bytes, followed by another string
var reader = new TcHmi.Base64BinaryReader('SGVsbG8sIFdvcmxkIQAAAAAAAADkuK3mlocgZXNwYcOxb2wg4KS54KS/4KSo4KWN4KSm4KWAINin2YTYudix2KjZitipINGA0YPRgdGB0LrQuNC5IOaXpeacrOiqng==');
// Reads the first 20 chars and saves the read position
var result1 = reader.readString(20); // 'Hello, World!'
// Reads the rest
var result2 = reader.readString(); // '中文 español हिन्दी العربية русский 日本語'
Available from version 1.10 |