SysVsscanf, extracts values ​​from string

List

This page is part of the IEC 61131-3 Programming Manual. Go to the index.

This function reads the string Str and interprets its contents based on the string defined in Format which specifies the format in which to interpret the variable. In VarType the type of variable is indicated in and VarAdd his address.

The function returns TRUE if variable value found, otherwise FALSE.

Replaces "SysVarsscanf"

Replaces the function SysVarsscanf changing the type of input parameters from string to pointer to string. The previous statement:

i:=SysVarsscanf(ADR(AStr), 'Value:%d', UDINT_TYPE, ADR(AVar)); 

Becomes:

i:=SysVsscanf(ADR(AStr), ADR('Value:%d'), VR_TYPE#UDINT_TYPE, ADR(AVar)); 
information circle

Function

CODESYS: Not available

LogicLab: eLLabXUnified12Lib

Description

Str (@STRING) Pointer to the string variable from which to extract the variable.
Format (@STRING) It has two types of arguments, ordinary characters that are checked in the variable String to read and the conversion specifications, distinguished by the percentage symbol (%) and by a character that specifies the format with which to interpret the defined variable (Definition).
VarType (VR_TYPE) Variable type to extract (Definition).
VarAdd (PVOID) Variable address.

The function returns a variable (BOOL), FALSE if error, TRUE if the variable has been acquired.

Image F SysVsscanf
Notes for handling string variables

This function values ​​the variable whose allocation address is defined in VarAdd, and we all know how dangerous it is to get out of the defined dimension for a variable. Now for numeric variables the size is inherent in the type, but in the STRING variables it is necessary to ensure that the result of the value scan does not exceed the size of the buffer allocated for the string.

In this regard I recommend always defining in the field Format in addition to the conversion specifications also the maximum size of the string that can be acquired. So as you see in the example below in the field Format the symbol % it is always followed by the definition of the maximum length defined for the result variable.

Use of regular expressions

The function specifier can also be defined using regular expressions, here are some examples.

// Some regular expression usage examples

    i:=SysVsscanf(ADR('Example 10+3=13$r$n'), ADR('%16[ -~]'), STRING_TYPE, ADR(SVar)); //SVar='Example 10+3=13'
    i:=SysVsscanf(ADR('Example 10+3=13$r$n'), ADR('%16[$20-$7E]'), STRING_TYPE, ADR(SVar)); //SVar='Example 10+3=13'
    i:=SysVsscanf(ADR('Example 10+3=13$r$n'), ADR('%16[a-zA-Z]'), STRING_TYPE, ADR(SVar)); //SVar='Example'
    i:=SysVsscanf(ADR('Example 10+3=13$r$n'), ADR('%16[a-z]'), STRING_TYPE, ADR(SVar)); //SVar=''
    i:=SysVsscanf(ADR('Example 10+3=13$r$n'), ADR('%16[0-9a-zA-Z]'), STRING_TYPE, ADR(SVar)); //SVar='Example'
    i:=SysVsscanf(ADR('Example 10+3=13$r$n'), ADR('%16[ 0-9a-zA-Z]'), STRING_TYPE, ADR(SVar)); //SVar='Example 10'
    i:=SysVsscanf(ADR('Example 10+3=13$r$n'), ADR('%16[+= 0-9a-zA-Z]'), STRING_TYPE, ADR(SVar)); //SVar='Example 10+3=13'

// [End of file]

Examples

How to use the examples.
Here are some examples of variable value acquisition from a string.

LogicLab (Ptp116, ST_SysVsscanf)
PROGRAM ST_SysVsscanf
VAR
    AStr : STRING[ 32 ]; (* Auxiliary string *)
    Res : ARRAY[0..19] OF BOOL; (* Scanf results *)
    UDVar : ARRAY[0..2] OF UDINT; (* UDINT variable *)
    RVar : ARRAY[0..3] OF REAL; (* REAL variable *)
    SVar1 : STRING[ 8 ]; (* String variable *)
    USVar : ARRAY[0..1] OF USINT; (* USINT variable *)
    SVar2 : STRING[ 8 ]; (* String variable *)
    SVar3 : STRING[ 8 ]; (* String variable *)
    SVar4 : STRING[ 8 ]; (* String variable *)
    SVar5 : STRING[ 8 ]; (* String variable *)
    SVar6 : STRING[ 8 ]; (* String variable *)
    ULVar : ARRAY[0..1] OF ULINT; (* ULINT variable *)
    LVar : ARRAY[0..1] OF LINT; (* LINT variable *)
    LRVar : LREAL; (* LREAL variable *)
END_VAR

// *****************************************************************************
// PROGRAM "ST_SysVsscanf"
// *****************************************************************************
// Some examples of read formatted data from string.
// -----------------------------------------------------------------------------

    // -------------------------------------------------------------------------
    // READ INTEGER DATA
    // -------------------------------------------------------------------------

    AStr:='Value:1234'; //Auxiliary string
    Res[0]:=SysVsscanf(ADR(AStr), ADR('Value:%ld'), UDINT_TYPE, ADR(UDVar[0])); //UDVar[0]=1234
    Res[1]:=SysVsscanf(SysStrFind(ADR(AStr), ADR('Value:'), FIND_GET_END), ADR('%ld'), UDINT_TYPE, ADR(UDVar[1])); //UDVar[10]=1234
    Res[2]:=SysVsscanf(ADR(AStr), ADR('%ld'), UDINT_TYPE, ADR(UDVar[2])); //UDVar[2]=0

    // -------------------------------------------------------------------------
    // READ REAL DATA
    // -------------------------------------------------------------------------

    AStr:='Value:12.34'; //Auxiliary string
    Res[3]:=SysVsscanf(ADR(AStr), ADR('Value:%f'), REAL_TYPE, ADR(RVar[0])); //RVar[0]=12.34
    Res[4]:=SysVsscanf(SysStrFind(ADR(AStr), ADR('Value:'), FIND_GET_END), ADR('%f'), REAL_TYPE, ADR(RVar[1])); //RVar[10]=12.34

    // -------------------------------------------------------------------------
    // READ DATA WITH DIGIT DEFINITION
    // -------------------------------------------------------------------------

    AStr:=' 124.5 '; //Auxiliary string
    Res[5]:=SysVsscanf(ADR(AStr), ADR('%d'), USINT_TYPE, ADR(USVar[0])); //USVar[0]=124
    Res[6]:=SysVsscanf(ADR(AStr), ADR('%2d'), USINT_TYPE, ADR(USVar[1])); //USVar[1]=12

    Res[7]:=SysVsscanf(ADR(AStr), ADR('%f'), REAL_TYPE, ADR(RVar[2])); //RVar[2]=124.5
    Res[8]:=SysVsscanf(ADR(AStr), ADR('%2f'), REAL_TYPE, ADR(RVar[3])); //RVar[3]=12.0

    // -------------------------------------------------------------------------
    // READ LONG INTEGER DATA
    // -------------------------------------------------------------------------

    AStr:='Value:1234567890123456789'; //Auxiliary string
    Res[9]:=SysVsscanf(ADR(AStr), ADR('Value:%llu'), ULINT_TYPE, ADR(ULVar[0])); //ULVar[0]=1234567890123456789
    Res[10]:=SysVsscanf(ADR(AStr), ADR('Value:%4llu'), ULINT_TYPE, ADR(ULVar[1])); //ULVar[1]=1234

    AStr:='Value:-1234567890123456789'; //Auxiliary string
    Res[11]:=SysVsscanf(ADR(AStr), ADR('Value:%lld'), LINT_TYPE, ADR(LVar[0])); //LVar[0]=-1234567890123456789
    Res[12]:=SysVsscanf(ADR(AStr), ADR('Value:%4lld'), LINT_TYPE, ADR(LVar[1])); //LVar[10]=-123

    // -------------------------------------------------------------------------
    // READ LONG REAL DATA
    // -------------------------------------------------------------------------

    AStr:='Value:1234.12345'; //Auxiliary string
    Res[13]:=SysVsscanf(ADR(AStr), ADR('Value:%llf'), LREAL_TYPE, ADR(LRVar)); //LVar=1234.12345

    // -------------------------------------------------------------------------
    // READ STRING EXAMPLES
    // -------------------------------------------------------------------------

    AStr:='ab+cd:e'; //Auxiliary string
    Res[14]:=SysVsscanf(ADR(AStr), ADR('%8s'), STRING_TYPE, ADR(SVar1)); //SVar1='ab+cd:e'
    Res[15]:=SysVsscanf(ADR(AStr), ADR('%3s'), STRING_TYPE, ADR(SVar2)); //SVar2='ab+'
    Res[16]:=SysVsscanf(ADR(AStr), ADR('%3[a-z]'), STRING_TYPE, ADR(SVar3)); //SVar3='ab'
    Res[17]:=SysVsscanf(ADR(AStr), ADR('%8[a-z]'), STRING_TYPE, ADR(SVar4)); //SVar4='ab'
    Res[18]:=SysVsscanf(ADR(AStr), ADR('%8[^+]'), STRING_TYPE, ADR(SVar5)); //SVar5='ab'
    Res[19]:=SysVsscanf(ADR(AStr), ADR('%8[^:]'), STRING_TYPE, ADR(SVar6)); //SVar5='ab+cd'

// [End of file]
Was this article helpful?