0

I need to get a byte code of char in Codesys (using ST language). Is there a way to do it?

For example, in c++ it is quiet straightforward:

int c = 'h';
Perotto
  • 194
  • 1
  • 14

2 Answers2

1

There are few ways to do that but concept is the same. You convert it to BYTE.

VAR
    sTest: STRING(1) := 'h';
    bChar: BYTE;
END_VAR

bChar := STRING_TO_BYTE(sTest);

But I like most use pointers. Here is example of function that return ASCII code of given character in a string.

FUNCTION STRING_TO_ASCII: BYTE
    VAR_INPUT
        pbIn: POINTER TO BYTE;
        bCharNum: BYTE; (* Character number in a string start with 0 *)
    END_VAR
    pbIn := pbIn + bCharNum;
    STRING_TO_ASCII := pbIn^;
END_FUNCTION

Now you can use it in program

VAR
    sTest: STRING(250) := 'Hello Wold!';
    bChar: BYTE;
END_VAR

bChar := STRING_TO_ASCII(sTest, 0); (* Character H *)
bChar := STRING_TO_ASCII(sTest, 1); (* Character e *)
Sergey Romanov
  • 2,949
  • 4
  • 23
  • 38
0

Create the Function CHR() with Variables:

FUNCTION CHR : BYTE
VAR_INPUT
    CHAR:STRING(1);
 END_VAR
VAR
    pBYTE:POINTER TO BYTE;
END_VAR

Using the following code:

pBYTE := ADR(CHAR);
CHR := pBYTE^;

This does the job quite well

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103