TSL Automation Solutions Logo

Section 15.4.20 · Chapter 15: Using Macros

String Operation

■ STRCPY

Format STRCPY(P1, P2 ) Data Type U
Function Copies the string in P2 to P1.
P1 (I) The byte array that receives a copy of the string in P2. The byte array must be large enough to hold the string and the null terminator.
P2 (I) The source, i.e. the byte array that contains the null-terminated string to be copied.
Example 1 $U10 = “ABCDE”
STRCPY($U20, $U10)
After the command STRCPY is executed, the byte array $U20 contains the string “ABCDE” and the memory content is like the following:
Word Low Byte High Byte
$U20 'A' 'B'
$U21 'C' 'D'
$U22 'E' 0
Example 2 $U10 = “12”
STRCPY($U20, $U10)
After the command STRCPY is executed, the byte array $U20 contains the string “12” and the memory content is like the following:
Word Low Byte High Byte
$U20 '1' '2'
$U21 0 Undefined

■ STRCAT

Format STRCAT(P1, P2 )
Function Appends string in P2 to string in P1.
P1 (I) The byte array that contains a null-terminated string to which the command appends P2. The byte array must be large enough to hold both strings and the null terminator.
P2 (I) The byte array that contains a null-terminated string to be appended to the string in P1.
Example 1 $U10 = “ABC”
$U20 = “12345”
STRCAT($U10, $U20) /* After this command is executed, the byte array $U10 contains “ABC12345” */
Example 2 $U100 = “C:\MyFolder\”
$U130 = “Test”
$U140 = “.txt“
STRCAT($U100, $U130)
STRCAT($U100, $U140) /* After this command is executed, the byte array $U100 contains “C:\MyFolder\Test.txt” */

■ STRLEN

Format P1 = STRLEN(P2 )
Function Gets the length of string P2 and saves the result in P1.
P1 (I) The word to receive the result.
P2 (I) The byte array that stores the null-terminated string.
Example 1 $U10 = “ABC”
$U20 = STRLEN($U10) /* After this command is executed, the value of $U20 is 3. */

■ STRCMP

Format P1 = STRCMP(P2,P3 )
Function Compares strings P2 and P3 lexicographically and saves a value indicating their relationship in P1.
P1 (I) The value of comparison result.
Value Description
0 P2 and P3 are identical.
1 P2 is greater than P3.
0xFFFF P2 is less than P3.
P2,P3 (I) The byte array that contains a null-terminated string to compare.
Example 1 $U10 = “ABC”
$U20 = “abc”
$U30 = STRCMP($U10, $U20) /* After this command is executed, $U30 is 0xFFFF*/
Example 2 $U10 = “XYZ”
$U20 = “ABC”
$U30 = STRCMP($U10, $U20) /* After this command is executed, $U30 is 1*/
Example 3 $U10 = “ABC”
$U20 = “ABC”
$U30 = STRCMP($U10, $U20) /* After this command is executed, $U30 is 0*/

■ STRICMP

Format P1 = STRICMP(P2,P3 )
Function Compares lowercase version of strings P2 and P3 lexicographically and saves a value indicating their relationship in P1.
P1 (I) The value of comparison result.
Value Description
0 P2 and P3 are identical.
1 P2 is greater than P3.
0xFFFF P2 is less than P3.
P2,P3 (I) The byte array that contains a null-terminated string to compare.
Example 1 $U10 = “ABC”
$U20 = “abc”
$U30 = STRICMP($U10, $U20) /* After this command is executed, $U30 is 0*/
Example 2 $U10 = “XYZ”
$U20 = “ABC”
$U30 = STRICMP($U10, $U20) /* After this command is executed, $U30 is 1*/
Example 3 $U10 = “ABC”
$U20 = “ABC”
$U30 = STRCMP($U10, $U20) /* After this command is executed, $U30 is 0xFFFF*/

■ STRNCMP

Format P1 = STRNCMP(P2,P3,P4 )
Function Lexicographically compares, at most, the first? P4 characters in strings P2 and P3 and saves a value indicating the relationship between the substrings in P1.
P1 (I) The value of comparison result.
Value Description
0 P2's substring and .P3's substring are identical
1 P2's substring is greater than P3's substring .
0xFFFF P2's substring is less than P3's substring .
Note: The comparison ends if a terminating null character is reached in either string before P4 characters are compared. If the strings are equal when a terminating null character is reached in either string before P4 characters are compared, the shorter string is less.
The characters from 91 to 96 in the ASCII table ('[', '\', ']', '^', '_', and '`') will evaluate as less than any alphabetic character.
P2,P3 (I) The byte array that contains a null-terminated string to compare.
P4 (I/C) The number of characters to compare.
Example 1 $U10 = “XYZ”
$U20 = “XYZAB”
$U30 = STRNCMP($U10, $U20,4) /* After this command is executed, $U30 is 0xFFFF*/
Example 2 $U10 = “ABZ”
$U20 = “ABC”
$U30 = STRNCMP($U10, $U20,2) /* After this command is executed, $U30 is 0*/
Example 3 $U10 = “AXC”
$U20 = “ABC”
$U30 = STRNCMP($U10, $U20,3) /* After this command is executed, $U30 is 1*/
$U30 = STRCMP($U10, $U20) /* After this command is executed, $U30 is 0xFFFF*/

■ STRCHR

Format P1 = STRCHR(P2,P3 )
Function Finds the first occurrence of a character P3 in a string P2 and saves a search result in value indicating the position of the found character in P1.
P1 (I) The value of search result. If the character P3 is not found in P2, the result value is 0xFFFF.Otherwise, the result value is the index to the first occurrence of character P3 in a string P2.
P2 (I) The byte array that contains a null-terminated source string.
P3 (I/C) The byte that contains a character code to be located.
Example 1 $U10 = “The quick brown dog jumps over the lazy fox.”
$U20 = 0x72 /* The ASCII code of character 'r' */
$U30 = STRCHR($U10, $U20) /* After this command is executed, $U30 is 11*/

■ NUM2STR

Format P1 = NUM2STR(P2,P3 ) Data Type U/UD
Function Converts the number in P2 to a string with P3 characters and saves the result in P1.
P1 (I) The byte array that stores the result.
P2 (I/C) The number or the location that holds the number to be converted.
P3 (I/C) Specifies the exact number of characters that the result should have. If the number of digits of P2 is less than P3, the result is padded on the left with zeros. If the number of digits of P2 exceeds P3, the higher digits are truncated. If P3 is 0, there is no limitation on the length of the result.
Example 1 $U120 = 123
$U100 = NUM2STR($U120, 0) (U) /* After this command is executed, the byte array $U100 contains “123”. */
Example 2 $U120 = 1234567 (UD)
$U100 = NUM2STR($U120, 10) (UD) /* After this command is executed, the byte array $U100 contains “0001234567”. */
Example 3 $U120 = 1234567 (UD)
$U100 = NUM2STR($U120, 5) (UD) /* After this command is executed, the byte array $U100 contains “34567”. */

■ TIME2STR

Format P1 = TIME2STR(P2 ) Data Type U
Function Converts the current system time to a string using the format specified by P2 and saves the result in P1.
P1 (I) The byte array that stores the result.
P2 (I/C) Specifies the desired conversion format.
Format P2 Value Remark
hhmmss 0 hh: hour(00-23); mm: minute(00-59); ss: second(00-59)
hhmm 1 hh, mm: same as above
Example 1 $U10 = TIME2STR(0) /* Assume that the current system time is 12:30:59. After this command is executed, the byte array $U10 contains “123059”. */

■ DATE2STR

Format P1 = DATE2STR(P2 ) Data Type U
Function Converts the current system date to a string using the format specified by P2 and saves the result in P1.
P1 (I) The byte array that stores the result.
P2 (I/C) Specifies the desired conversion format.
Format P2 Value Remark
YYMMDD 0 YY: year (00-99); MM: month(01-12); DD: day(01-31)
YYMM 1 YY, MM: same as above
YYMMMDD 2 YY: year (00-99); MMM: month(JAN-DEC); DD: day(01-31)
YYMMM 3 YY, MMM: same as above
Example 1 $U10 = DATE2STR(0) /* Assume that the current system date is December 7, 2008. After this command is executed, the byte array $U10 contains “081207”. */
Example 2 $U20 = DATE2STR(3) /* Assume that the current system date is December 31, 2008. After this command is executed, the byte array $U20 contains “08DEC”. */

■ TD2STR

Format P1 = TD2STR(P2 ) Data Type U
Function Converts the current system time and date to a string using the format specified by P2 and saves the result in P1.
P1 (I) The byte array that stores the result.
P2 (I/C) Specifies the desired conversion format.
Format P2 Value Remark
YYMMDD_hhmmss 0 YY: year (00-99); MM: month(01-12); DD: day(01-31)
hh: hour(00-23); mm: minute(00-59) ; ss: second(00-59)
YYMMMDD_hhmmss 1 YY, DD, hh, mm, ss: same as above
MMM: month(JAN-DEC)
YYMMDD_hhmm 2 YY, DD, hh, mm: same as above; MM: month(01-12)
YYMMMDD_hhmm 3 YY, DD, hh, mm: same as above;
MMM: month(JAN-DEC)
Example 1 $U10 = TD2STR(0) /* Assume that the current system date is December 7, 2008 and the current system time is 15:18:30. After this command is executed, the byte array $U10 contains “081207_151830". */
Example 2 $U20 = TD2STR(3) /* Assume that the current system date is December 31, 2008 and the current system time is 13:30:00. After this command is executed, the byte array $U20 contains “08DEC31_1330”. */

■ I2A

Format P1 = I2A(P2,P3 ) Data Type U/S/UD/SD
Function Converts the integer number in P2 to a string and saves the result in P1. The string is generated according to the format specified by P3.
P1 (I) The byte array that stores the result. The result is a null terminated string.
P2 (I/C) The integer number or the location that holds the integer number to be converted.
P3 (I/C) Specifies where to insert a decimal point in the string. A decimal point is inserted to the left of the nth digit when P3 is n. No decimal point is inserted when P3 is 0.
Example 1 $U120 = 123
$U100 = I2A($U120, 0) /* After this command is executed, the byte array $U100 contains “123”. */
Example 2 $U120 = 1234567 (UD)
$U100 = I2A($U120, 2) (UD) /* After this command is executed, the byte array $U100 contains “12345.67”. */
Example 3 $U120 = -12345 (S)
$U100 = I2A($U120, 1) (UD) /* After this command is executed, the byte array $U100 contains “-1234.5”. */

■ A2I

Format P1 = A2I(P2,P3,P4 ) Data Type U/S/UD/SD
Function Converts the string P2 to an integer value and saves the result in P1.
P1 (I) The location that stores the result. The result is 0 when there is any conversion error.
P2 (I) The byte array that holds the string to be converted.
P3 (I/C) Specifies the length of the string. It is allowed to specify 0 for P3. When P3 is 0, the string must be a null terminated string.
P4 (I/C) Specifies how many fractional digits in the string are to be converted.
Example 1 $U120 = “123”
$U100 = A2I($U120, 0, 0) /* After this command is executed, the value in word $U100 is 123. */
Example 2 $U120 = “1234567”
$U100 = A2I($U120, 6, 0) (UD) /* After this command is executed, the value in double word $U100 is 123456. */
Example 3 $U120 = “-123.45”
$U100 = A2I($U120, 0, 2) (S) /* After this command is executed, the value in word $U100 is -12345. */

■ F2A

Format P1 = F2A(P2,P3 ) Data Type F
Function Converts the floating point number in P2 to a string and saves the result in P1. The string is generated according to the format specified by P3.
P1 (I) The byte array that stores the result. The result is a null terminated string.
P2 (I/C) The floating point number or the location that holds the floating point number to be converted.
P3 (I/C) Specifies the number of fractional digits the string can have.
Example 1 $U120 = 123.45 (F)
$U100 = F2A($U120, 2) /* After this command is executed, the byte array $U100 contains “123.45”. */
Example 2 $U120 = 567.89 (F)
$U100 = F2A($U120, 1) (UD) /* After this command is executed, the byte array $U100 contains “567.8”. */
Example 3 $U120 = -1234.5 (S)
$U100 = F2A($U120, 1) (UD) /* After this command is executed, the byte array $U100 contains “-1234.5”. */

■ A2F

Format P1 = A2F(P2,P3 ) Data Type F
Function Converts the string P2 to a floating point number and saves the result in P1.
P1 (I) The location that stores the result. The result is 0 when there is any conversion error.
P2 (I) The byte array that holds the string to be converted.
P3 (I/C) Specifies the length of the string. It is allowed to specify 0 for P3. When P3 is 0, the string must be a null terminated string.
Example 1 $U120 = “123.4”
$U100 = A2F($U120, 0) /*The value of the floating point number in double word $U100 is 123.4. */
Example 2 $U120 = “1234567”
$U100 = A2F($U120, 6) (UD) /* The value of the floating point number in double word $U100 is 123456. */
Example 3 $U120 = “-123.45”
$U100 = A2F($U120, 0) (S) /* The value of the floating point number in double word $U100 is -123.45. */

■ GET_BAR_CODE

Format P1 = GET_BAR_CODE(P2) Data Type U
Function Gets the bar code input from the bar code reader connected to the specified USB port.
P1 (I) The starting location of the byte array to store the bar code.

The bar code is stored in the byte array as a null-terminated character string. The length of a bar code can be up to 1024 characters. Make sure the byte array has sufficient space for your application. If there is no bar code input, the first 2 bytes of the byte array are set to 0. There is only one bar code input buffer in the system, so if a bar code is not fetched in time, it could be overwritten by a new bar code input. The bar code in the buffer will be erased immediately after it is fetched, so the command can get the bar code of one input only once.

The most effective way to get a bar code is to use the macro command in an Event macro and the Event macro is triggered by the system bit BarCodeInputViaUsb. When there is a bar code input, the system bit triggers the Event macro once. This guarantees that a bar code input will always be fetched and processed.

Note: Only T series models support this command.
P2 (I/C) The number of the USB port that connects to the bar code reader.

For T series models, you can only specify 1.
Example 1 $U100 = GET_BAR_CODE(1) /* Get the bar code scanned by the bar code reader connected to the USB port 1 */

■ GB2U

Format P1 = GB2U(P2,P3 ) Data Type U
Function Converts the GB code string in P3 to an UNICODE string in P2 and saves the number of characters in P1.
P1 (I) The number of characters.
P2 (I) Destination. The word array that stores the null-terminated UNICODE or word string.
P3 (I) Source. The byte array that holds the null-terminated GB code string to be converted.
Example 1 $U20 = “ABC”
$U30 = GB2U($U10, $U20) /*After this command is executed, the value of $U30 is 4, and $U10 = 0x41, $U11 = 0x42, $U12 = 0x43 */

■ U2GB

Format P1 = U2GB(P2,P3 ) Data Type U
Function Converts the UNICODE string in P3 to a GB code string in P2 and saves the number of characters in P1.
P1 (I) The number of characters.
P2 (I) Destination. The byte array that holds the null-terminated GB code string after converted.
P3 (I) Source. The word array that stores the null-terminated UNICODE or word string.
Example 1 $U20 = 0x41 // "A" $U21 = 0x42 // "B" $U22 = 0x43 // "C" $U23 = 0 $U30 = U2GB($U10, $U20) /* After this command is executed, the value of $U30 is 3, and $U10 = 0x4241, $U11 = 0x43 */

■ WCSLEN

Format P1 = WCSLEN(P2) Data Type U
Function Gets the length of UNICODE or word string P2 and saves the result in P1.
P1 (I) The number of characters.
P2 (I) The word array that stores the null-terminated UNICODE or word string.
Example 1 $U10 = 0x41 // "A" $U11 = 0x42 // "B" $U12 = 0x43 // "C" $U13 = 0 $U20 = U2GB($U10) /* After this command is executed, the value of $U20 is 3 */

Need PM Designer itself?

V4 for current PK, PT, ITV and PM panels, V2 for older models. Free, sent by email.

Source.Section 15.4.20 of the PM Designer Software User Manual, Edition 2.0, © Cermate Technologies Inc. (www.cermate.com, service@cermate.com). Reproduced online, with the manual's own figures, by TSL Automation Solutions, authorised Cermate distributor, for users of Cermate HMI panels. PM Designer and PanelMaster are trademarks of Cermate Technologies Inc.

Text and figures are the manufacturer's and may describe features your panel or software version does not have. For a plain-language introduction read the PM Designer guide; for help with a specific panel contact TSL Automation.