How to write an ASCII value into a string in Structured Text for a PLC?

16.1k views Asked by At

For a PLC program written in Structured Text I need a string which starts with the ASCII values 0x01 0x01 0x01 0x00 0x12.

In Java I would do this with:

String literal = "\x01\x01\x01\x0\x12";

How can I achieve the same in Structured Text for a PLC?

2

There are 2 answers

0
franji1 On BEST ANSWER

It appears that IEC-61131's escape sequence is $hh, so just use

'$01$01$01$00$12'

see STRING data type https://en.wikipedia.org/wiki/IEC_61131-3#Data_types

they show some example string literals using $ as the escape sequence character (ala \ in C/C++/Java)

0
Flanker On

Try this:

VAR
 string: STRING[5]:='abcde';
 char : ARRAY[0..4] OF BYTE := 16#x01, 16#01, 16#01, 16#0, 16#12;
 ptr : POINTER TO BYTE;
 i : INT;
END_VAR

ptr:=ADR(string);
FOR i:=0 TO 4 DO
 ptr^ = char[i];
 ptr:=ptr+1;
END_FOR