[西门子] TwinCAT - 字符串相关函数 02

[复制链接]
查看68990 | 回复0 | 2024-1-27 09:08:07 | 显示全部楼层 |阅读模式
1. FB_FormatString
iX := iX + 1;iY := iY + 1;fbFormat( sFormat := 'Measure X: %+.10d, Y: %+.10d',           arg1 := F_DINT( iX ),           arg2 := F_DINT( iY ),           sOut => sOut,           bError => bError,           nErrID => nErrID );

sOut = 'Measure X: +0000000130, Y: +0000000130'

2. FB_EnumStringNumbers
TYPEST_ScanRes :STRUCTsNumber   : T_MaxString;nPos      : INT;sRemain   : T_MaxString;END_STRUCTEND_TYPE
PROGRAMMAINVARsSearch    : T_MaxString := 'Some numbers in string: +-12e-34, -56, +78';fbEnum     : FB_EnumStringNumbers := ( eType := eNumGroup_Float (* eNumGroup_Signed, eNumGroup_Unsigned *) );arrNums    : ARRAY[1..MAX_SCAN_NUMS] OF ST_ScanRes;idx        : INT;length     : INT;bEnum      : BOOL := TRUE;END_VARVARCONSTANTMAX_SCAN_NUMS : INT := 10;END_VAR
IF bEnum THEN    bEnum := FALSE;
    MEMSET( ADR( arrNums ), 0, SIZEOF( arrNums ) );    idx := 0;    length := LEN( sSearch );
    fbEnum( sSearch := sSearch, eCmd := eEnumCmd_First );    WHILE NOT fbEnum.bEOS DO        IF idx < MAX_SCAN_NUMS THEN            idx := idx + 1;
            arrNums[idx].sNumber:= fbEnum.sNumber;            arrNums[idx].nPos := fbEnum.nPos;            IF fbEnum.nPos <> 0 THEN                arrNums[idx].sRemain:= RIGHT( sSearch, length - fbEnum.nPos + 1 );            END_IF
        END_IF        fbEnum( eCmd := eEnumCmd_Next );    END_WHILEEND_IF

搜索到以下:'-12e-34''-56''+78'

3. DATA_TO_HEXSTR
PROGRAMMAINVARstr    : T_MaxString := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';number : DWORD := 16#BECF1234;char   : BYTE := 16#07;null   : UDINT := 0;
overflow   : ARRAY[0..86] OF BYTE; (* data overflow *)cbOverflow : UDINT;
sH1,sH2, sH3, sH4, sH5 : T_MaxString;END_VAR

sH1 := DATA_TO_HEXSTR( pData := ADR(str), cbData := LEN(str), FALSE );sH2 := DATA_TO_HEXSTR( pData := ADR(number), cbData := SIZEOF(number), FALSE );sH3 := DATA_TO_HEXSTR( pData := ADR(char), cbData := SIZEOF(char), FALSE );sH4 := DATA_TO_HEXSTR( pData := ADR(null), cbData := SIZEOF(null), FALSE );cbOverflow:= SIZEOF(overflow);sH5 := DATA_TO_HEXSTR( pData := ADR(overflow), cbData := cbOverflow, FALSE );

sH1 = '61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78 79 7A 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 30 31 32 33 34 35 36 37 38 39'sH2 = '34 12 CF BE'

sH3 = '07'

sH4 = '00 00 00 00'

sH5 = '00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00.'

4. HEXSTR_TO_DATA
PROGRAMMAINVARsH     : STRING := 'AB CD EF 01 23 45 67 89';data   : ARRAY[0..10] OF BYTE;cbData : UDINT;END_VAR
cbData := HEXSTR_TO_DATA( sH, ADR( data ), SIZEOF( data ) );


5. F_FormatArgToStr
double := 12345.6789;errID  := F_FormatArgToStr( FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 20, 8, TYPEFIELD_F, F_LREAL( double ), s1 );errID  := F_FormatArgToStr( FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, 20, 8, TYPEFIELD_F, F_LREAL( double ), s2 );errID  := F_FormatArgToStr( FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, 20, 8, TYPEFIELD_F, F_LREAL( double ), s3 );errID  := F_FormatArgToStr( FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, 20, 8, TYPEFIELD_F, F_LREAL( double ), s4 );errID  := F_FormatArgToStr( TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, 20, 8, TYPEFIELD_F, F_LREAL( double ), s5 );L1 := LEN( s1 );L2 := LEN( s2 );L3 := LEN( s3 );L4 := LEN( s4 );L5 := LEN( s5 );

s1 = '12345.67890000'

s2 = '      12345.67890000'

s3 = '12345.67890000      '

s4 = '00000012345.67890000'

s5 = '+12345.67890000     '

L1 = 14

L2 = 20

L3 = 20

L4 = 20

L5 = 20

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册哦

x
您需要登录后才可以回帖 登录 | 注册哦

本版积分规则