FP_ASCII_TO_BIN

ASCII -> binary conversion

This FP instruction converts ASCII code stored in the area specified by s2_AsciiData to 16-bit/32-bit binary data. The conversion method is specified by s1_Format. The converted result is stored in the area specified by d_BinaryData.

NOTE

The FP instructions FP_ASCII_TO_BIN and FP_ASCII_CHECK do not differentiate between upper and lower case.

Parameters

Input

s1_Format (STRING)

Control and format string set in inverted commas

s2_AsciiData (BOOL, INT, UINT, WORD, DINT, UDINT, DWORD, REAL, DATE, TOD, DT, STRING)

Starting address for storing ASCII data

n_Conversions (WORD, INT, UINT)

Quantity of numbers to be converted: 0–65535

n_AsciiDataStartPosition (INT)

Starting position in ASCII data: 0–255

Output

d_BinaryData (BOOL, INT, UINT, WORD, DINT, UDINT, DWORD, REAL, DATE, TOD, DT, STRING)

Starting address for storing binary data

Explanation of each digit of the control code s1_Format:

Position in the control code

Description

s1_Format

16-bit PLCs

32-bit PLCs

Conversion direction

     

+

forward (only for hexadecimal numbers with specifier x, X)

'+%4X'

if plus sign is omitted: reverse (default setting)

'%4X'

%

format string specifier (mandatory)

 

Padding format

     

0

fill with zeros

'%06x'

-

+

add a plus sign

'%+4d'

-

-

left alignment

'%-6d'

-

˽

(space) add a space instead of plus sign

'%˽4d'

-

#

insert 0x for hexadecimal numbers

'%#4X'

-

append always a decimal point for real number

'%#8.0f'

-

8

width of the ASCII data element

'%08d'

no width

  • FP_BIN_TO_ASCII: required minimum width is assumed

  • FP_ASCII_TO_BIN and FP_ASCII_CHECK: comma separator is required

'%d,'

-

Precision after decimal point

     

.5

any digit after decimal point

'%8.5f'

-

I

double length specifier

e.g. specifier i with l = Ii -> DINT

'+%4ld'

Format specifier

     

i

INT

'%10i'

u

UINT

'%10u'

-

d

INT

'%6d'

x

hexadecimal lower case

'+%4x'

X

hexadecimal upper case

'+%4X'

b

BCD

'+%5b'

-

f

floating-point number

'+%-6.2f'

-

e

exponential 1.23e10

'+%9.3e'

-

E

exponential upper case 1.23E10

'+%9.3E'

-

g

floating or exponential

'+%12g'

-

G

floating or exponential upper case

'+%9.3G'

-

  • For FP_BIN_TO_ASCII: any string can be appended to the conversion e.g. 'PANA'

  • For FP_ASCII_TO_BIN and FP_ASCII_CHECK: only comma is allowed

'+%8dPANA'

-

available on 16-bit/32-bit PLCs

Examples of control string s1_Format

  • Example: '+%4X' converts an ASCII value with a width of four characters in upper case in forward direction (valid for 16-bit/32-bit PLCs)

    ASCII data

    Conversion result in binary data

    '˽12A'

    16#12A (displayed in hexadecimal format)

  • Example: '%4X' converts an ASCII value with a width of four characters in upper case in reverse direction (valid for 16-bit/32-bit PLCs)

    ASCII data

    Conversion result in binary data

    '˽B2A'

    16#B2A (displayed in hexadecimal format)

  • Example: '%06d' converts an ASCII value with a width of six characters, decimal value with three leading zeros (valid for 32-bit PLCs only)

    ASCII data

    Conversion result in binary data

    '000100'

    16#100 (displayed in hexadecimal format)

  • Example: '%+4d' converts an ASCII value with a width of four characters, decimal value, + sign added (valid for 32-bit PLCs only)

    ASCII data

    Conversion result in binary data

    '+100 ˽ ˽'

    100 (displayed in decimal format)

  • Example: '%-6d' converts an ASCII value with a width of six characters, left aligned (valid for 32-bit PLCs only)

    ASCII data

    Conversion result in binary data

    '100˽ ˽ ˽'

    100 (displayed in decimal format)

  • Example: '%˽4d' converts an ASCII value with a width of four characters with one leading space (valid for 32-bit PLCs only)

    ASCII data

    Conversion result in binary data

    '˽100'

    100 (displayed in decimal format)

  • Example: '%#8.0f' converts an ASCII value with a width of eight characters, floating-point number, four leading spaces (valid for 32-bit PLCs only)

    ASCII data

    Conversion result in binary data

    '˽ ˽ ˽ ˽123.'

    123.45678 (displayed in decimal format)

  • Example: '%8.3f' converts an ASCII value with a width of eight characters, three characters precision after decimal point (valid for 32-bit PLCs only)

    ASCII data

    Conversion result in binary data

    '˽123.456'

    123.45599 (displayed in decimal format)

  • Example: '+%10u' converts an ASCII value with a width of 10 characters, seven leading spaces, unsigned integer (valid for 32-bit PLCs only)

    ASCII data

    Conversion result in binary data

    '˽ ˽ ˽ ˽ ˽ ˽ ˽100'

    -100 (displayed in decimal format)

  • Example: '%06d' converts an ASCII value with a width of six characters with three leading spaces (valid for 16-bit/32-bit PLCs)

    ASCII data

    Conversion result in binary data

    '˽ ˽ ˽100'

    100 (displayed in decimal format)

    Permissible range for the value before the specifier:

    1–15 before specifiers d, ld, i, li

    1–4 before specifier X

    1–8 before specifier lX

    if no width is specified, comma is appended e.g. '%d,' (valid for 32-bit PLCs only)

    ASCII data

    Conversion result in binary data

    '100,'

    100 (displayed in decimal format)

    • FP_BIN_TO_ASCII: required minimum width is assumed

    • FP_ASCII_TO_BIN and FP_ASCII_CHECK: comma separator is required

  • Example: '+%4ld' converts an ASCII value with a width of four characters, requires DINT or DWORD for the converted result (valid for 16-bit/32-bit PLCs)

    ASCII data

    Conversion result in binary data

    '˽100'

    100 (displayed in decimal format)

  • Example: '+%6i' converts an ASCII value with a width of six characters, three leading spaces, signed integer in forward direction (valid for 16-bit/32-bit PLCs)

    ASCII data

    Conversion result in binary data

    '˽ ˽ ˽-100'

    -100 (displayed in decimal format)

  • Example: '+%6d' converts an ASCII value with a width of six characters with three leading zeros in forward direction (valid for 16-bit/32-bit PLCs)

    ASCII data

    Conversion result in binary data

    '000100'

    100 (displayed in decimal format)

  • Example: '+%4X' converts an ASCII value with a width of four characters, hexadecimal number in upper case in forward direction (valid for 16-bit/32-bit PLCs)

    ASCII data

    Conversion result in binary data

    '˽12A'

    16#12A (displayed in hexadecimal format)

  • Example: '+%5b' converts an ASCII value with a width of five characters, BCD data (valid for 32-bit PLCs only)

    ASCII data

    Conversion result in binary data

    '˽123'

    16#123 (displayed in hexadecimal format)

  • Example: '+%-6.2f' converts an ASCII value with a width of six characters, left aligned, precision two digits after decimal point (valid for 32-bit PLCs only)

    ASCII data

    Conversion result in binary data

    '1.23˽'

    1.2345 (displayed in decimal format)

  • Example: '+%9.3e' converts an ASCII value with a width of 9 characters, precision 3 digits after decimal point, exponential lower case (valid for 32-bit PLCs only)

    ASCII data

    Conversion result in binary data

    '1.235e+03'

    1234.5678 (displayed in decimal format)

  • Example: '1.235E+03' converts an ASCII value with a width of 9 characters, precision 3 digits after decimal point, exponential upper case (valid for 32-bit PLCs only)

    ASCII data

    Conversion result in binary data

    '1.235E+03'

    1234.5678 (displayed in decimal format)

  • Example: '+%12g' converts an ASCII value with a width of 12 characters, floating-point number (valid for 32-bit PLCs only)

    ASCII data

    Conversion result in binary data

    '˽ ˽ ˽ ˽ ˽1234.57'

    1234.5678 (displayed in decimal format)

  • Example: '+%9.3G' converts an ASCII value with a width of nine characters, precision three characters after decimal point, floating-point number, exponential upper case (valid for 32-bit PLCs only)

    ASCII data

    Conversion result in binary data

    '˽ ˽ ˽1.E+03'

    1234 (displayed in decimal format)

  • For FP_BIN_TO_ASCII: any string can be appended to the conversion

    Example: '+%8dPANA' converts an ASCII value with a width of eight characters, decimal number, 'PANA' is appended (valid for 32-bit PLCs only)

    ASCII data

    Conversion result in binary data

    '˽ ˽ ˽ ˽ ˽100PANA'

    100 (displayed in decimal format)

Error flags

sys_bIsOperationErrorHold (turns to TRUE and remains TRUE)
  • if there is an error in the control string specified by sFormat.

  • if forward direction (+) is specified in sFormat when the format is decimal.

  • if the number of ASCII characters per converted unit specified by n_Conversions exceeds 4 for 16-bit data or 8 for 32-bit data when hexadecimal format is specified by s1_Format.

  • if 0 is specified for the no. of 16- or 32-bit (1- or 2-word) units to be converted in n_Conversions.

  • if the number of 16- or 32-bit decimal numbers to be converted specified by n_Conversions exceeds the area for storing ASCII data.

  • if the converted result exceeds the area.

sys_bIsOperationErrorNonHold (turns to TRUE for one scan)
  • if there is an error in the control string specified by sFormat.

  • if forward direction (+) is specified in sFormat when the format is decimal.

  • if the number of ASCII characters per converted unit specified by n_Conversions exceeds 4 for 16-bit data or 8 for 32-bit data when hexadecimal format is specified by s1_Format.

  • if 0 is specified for the no. of 16- or 32-bit (1- or 2-word) units to be converted in n_Conversions.

  • if the number of 16- or 32-bit decimal numbers to be converted specified by n_Conversions exceeds the area for storing ASCII data.

  • if the converted result exceeds the area.

Example

POU header

All input and output variables used for programming this function have been declared in the POU header. The same POU header is used for all programming languages.

	VAR
		bStart: BOOL:=FALSE;
		sExample1: STRING[32]:='*1234,5678,';
		arrayValuesExample1: ARRAY [0..1] OF UINT:=[2(0)];
	END_VAR

POU body

When bStart is set to TRUE, the instruction is carried out. It converts 2 x 4 decimal ASCII characters to binary data. Offset = 1 ASCII character (8-bit).

LD body

BODY
    WORKSPACE
        NETWORK_LIST_TYPE := NWTYPELD ;
        ACTIVE_NETWORK := 0 ;
    END_WORKSPACE
    NET_WORK
        NETWORK_TYPE := NWTYPELD ;
        NETWORK_LABEL :=  ;
        NETWORK_TITLE :=  ;
        NETWORK_HEIGHT := 8 ;
        NETWORK_BODY
B(B_VARIN,,'%d~',26,2,28,4,);
B(B_VARIN,,2,26,4,28,6,);
B(B_VARIN,,1,26,5,28,7,);
B(B_VAROUT,,arrayValuesExample1,41,2,43,4,);
B(B_F,Adr_Of_VarOffs_I!,,14,2,23,6,,?D@'Var'?DOffs?CAdr);
B(B_VARIN,,sExample1,12,3,14,5,);
B(B_VARIN,,2,12,4,14,6,);
B(B_F,FP_ASCII_TO_BIN!,,28,0,41,7,,?DEN?hs1_Format?ds2_AsciiData?dn_Conversions?dn_AsciiDataStartPosition?AENO?cd_BinaryData);
B(B_CONTACT,,bStart,5,1,7,3,);
L(23,4,28,4);
L(7,2,28,2);
L(1,2,5,2);
L(1,0,1,8);
        END_NETWORK_BODY
    END_NET_WORK
END_BODY

ST body

If (bStart) then
    FP_ASCII_TO_BIN(s1_Format := '%d,',
                s2_AsciiData := Adr_Of_VarOffs(Var := sExample1, Offs := 2),
                n_Conversions := 2, 
                n_AsciiDataStartPosition := 1,
                d_BinaryData => arrayValuesExample1);
End_if;

Modified on: 2024-11-29Feedback on this pagePanasonic hotline