Browse
(440) 299-7778
Menu
(440) 299-7778
One-year parts and labor warranty30-day money-back guaranteeContact

The text of a manual · part 12 of 17

1771-DB Basic Module User Manual, pages 111 to 120

Pages 111 to 120 of 167

The original document is the PDF (943 KB). This is its text as extracted, unedited; tables and drawings read best in the PDF, and each page heading opens it at that page.

For the Allen-Bradley 1771-DB

Page 111

Chapter 5 Operating Functions

5.10.2 The CHR Operator

The CHR() operator converts a numeric expression to an ASCII character. Example: uPRINT CHR(65) A Like the ASC() operator, the CHR() operator also selects individual characters in a defined ASCII string. Example: u5 STRING 1000,40 u10 $(1)=“The BASIC Module” u20 FOR I=1 TO 16 : PRINT CHR($(1),I), : NEXT I u30 PRINT: FOR I=16 TO 1 STEP -1 u40 PRINT CHR($(1),I), : NEXT I uRUN The BASIC Module eludoM CISAB ehT In the above example, the expressions contained within the parenthesis, following the CHR operator have the same meaning as the expressions in the ASC() operator. Unlike the ASC() operator, you CANNOT assign the CHR() operator a value. A statement such as CHR($(1),1) = H, is INVALID and generates a BAD SYNTAX ERROR. Use the ASC() operator to change a value in a string, or use the string support call routine – replace string in a string. See Section 5.10.3 below titled, “String Support Calls” for more information. Important: Use the CHR() function only in a print statement.

5.10.2.1 Clearing the Screen on an Allen-Bradley Industrial Terminal Refer to Appendix B at the rear of this manual. In “Column 1” under ”DEC”, you find that 12 equals ASCII FF (form feed). Type PRINT CHR(12) to clear the industrial terminal screen with a form feed.

5–74

Page 112

Chapter 5 Operating Functions

5.10.2 The CHR Operator (continued)

5.10.2.2 Cursor Positioning on an Industrial Terminal Allen-Bradley Industrial Terminal – Refer to the Industrial Terminal User’s Manual (publication number 1770-6.5.3), Table 6-3. You can control cursor positioning by typing: (CTRL)(P)(COLUMN#)(;)(ROW#)(A). The ASCII equivalent of (CTRL)(P) is DLE. Using Appendix B, the DEC equivalent of ASCII DLE is 16. PRINT CHR(16), “40;10ATEST” This prints “TEST” at column 40 row 10 on your Allen-Bradley Industrial Terminal.

5.10.3 String Support Calls

To enhance the BASIC Module we have added several string manipulation routines that make programming easier. Strings in the BASIC Module are declared by the STRING statement. You must execute the STRING statement before you can access or call any strings or string routines. The STRING statement has two arguments or numbers that follow it. They are: the total amount of space to allocate to string storage and, the maximum size in characters of each string. Since you terminate strings using a carriage return character, each string is given an extra byte of storage for its carriage return if it is the maximum length. You can determine the number of strings allowed by taking the first number and dividing it by one plus the second number. Note that you must use the strings consecutively starting with string 0 through the allowed number of strings. All strings are allocated the maximum number of characters regardless of the actual number used. All of these routines use or modify strings as part of their operation. The mechanism for passing a string to the support routine is to PUSH its number or subscript onto the stack. The support routine can then get the string number from the module’s argument stack, and locate it in string memory. Many of these routines also require the length of a string as an input. This number must normally be inclusively between zero and the second number used in the last STRING statement which specifies the maximum size of a string. However, in all cases, if a string length argument of minus one ( -1) is given, it is interpreted as the maximum allowable string length.

5–75

Page 113

Chapter 5 Operating Functions

5.10.3 String Support Calls (continued)

It is important to note that since the carriage return character is the string terminator, you cannot use it within a string as one of its characters. If the high bit is set in a carriage return character (use 141 instead of 13 as the decimal value of the carriage return character) the BASIC Module does not recognize it as the end of string character and passes it to the output device. Most devices use a seven bit ASCII code, ignore the top bit and treat the 141 as a normal carriage return. Important: All undefined characters of a string (i.e. characters following CR) are nulls. If data is input to a string using the ASC($(X),I) command, you must be sure the string is (CR) terminated properly. You can initialize the string or add a (CR) to terminate the string. Example: u10 STRING 100,10 u20 FOR I=1 TO 5 u30 ASC($(0),I)=65 u40 NEXT I u50 PRINT $(0) uRUN AAAAA The string is stored in memory without a (CR) terminator. Be sure to insert a (CR) (0DH) into the last position of the string if using the above method. Example: u10 STRING 100,10 u20 FOR I=1 TO 5 u30 ASC ($(0),I) = 65 u40 NEXT I u45 ASC($(0),6)=0DH u50 PRINT $(0) or u10 STRING 100,10 u15 $(0))=“ ” u20 FOR 1 TO 5 u30 ASC ($(0),I) = 65 u40 NEXT I u50 PRINT $(0) The following three programs allow you to determine how much string space to allocate when you know two of three variables associated with the strings: number of characters in the longest string. number of string variables. amount of memory to allocate for strings.

5–76

Page 114

Chapter 5 Operating Functions

5.10.3 String Support Calls (continued)

Example program 1:

uLIST 10 REM STRING ALLOCATION COMPUTATION KNOWING: 20 REM 1)# CHARACTERS IN LONGEST STRING 2)# OF STRING VARIABLES 30 PRINT : PRINT 40 INPUT “HOW MANY CHARACTERS IN YOUR LONGEST STRING”C 50 INPUT “HOW MANY STRING VARIABLES WILL YOU NEED”V 60 PRINT 70 N=(((C+1)*V)+1) : REM COMPUTE THE # OF BYTES OF MEMORY NEEDED 80 PRINT: PRINT 90 PRINT “YOU NEED TO ALLOCATE”,N,“BYTES OF MEMORY FOR”,V,“VARIABLES 100 PRINT ”CONTAINING”,C,“CHARACTERS EACH” 110 PRINT : PRINT 120 PRINT“ STRING”,N“,”,C 130 END uRUN HOW MANY CHARACTERS IN YOUR LONGEST STRING ?21 HOW MANY VARIABLES WILL YOU NEED ?15 YOU NEED TO ALLOCATE 331 BYTES OF MEMORY FOR 15 VARIABLES CONTAINING 21 CHARACTERS EACH STRING 331, 21 READY

5–77

Page 115

Chapter 5 Operating Functions

5.10.3 String Support Calls (continued)

Example program 2:

uLIST 10 REM STRING ALLOCATION COMPUTATION KNOWING: 20 REM 1)# CHARACTERS IN LONGEST STRING 2) AMOUNT OF STRING MEMORY 30 PRINT : PRINT 40 INPUT “HOW MANY CHARACTERS IN YOUR LONGEST STRING”C 50 INPUT “# OF BYTES OF MEMORY CAN YOU ALLOCATE FOR STRINGS”N 60 PRINT 70 V=INT((N-1)/(C+1)) : REM COMPUTE THE # OF POSSIBLE VARIABLES 80 N=(V*(C+1))+1 : REM COMPUTE HOW MUCH MEMORY IS ACTUALLY NEEDED 90 PRINT: PRINT 100 PRINT “YOU NEED TO ALLOCATE”,N,“BYTES OF MEMORY FOR”,V,“VARIABLES 110 PRINT “CONTAINING”,C,“CHARACTERS EACH 120 PRINT: PRINT 130 PRINT“ STRING”,N,“,“C 140 END uRUN HOW MANY CHARACTERS IN YOUR LONGEST STRING ?20 # OF BYTES OF MEMORY CAN YOU ALLOCATE FOR STRINGS ?500 YOU NEED TO ALLOCATE 485 BYTES OF MEMORY FOR 22 VARIABLES CONTAINING 21 CHARACTERS EACH STRING 485 , 21 READY u

5–78

Page 116

Chapter 5 Operating Functions

5.10.3 String Support Calls (continued)

Example program 3:

10 REM STRING ALLOCATION COMPUTATION KNOWING: 20 REM 1) AMOUNT OF STRING MEMORY 2) #OF STRING VARIABLES 30 PRINT: PRINT 40 INPUT “ENTER # OF BYTES OF MEMORY YOU CAN ALLOCATE FOR STRINGS”N 50 INPUT “HOW MANY STRING VARIABLES WILL YOU NEED”V 60 PRINT 70 C=INT(((N-1)/V)-1) : REM COMPUTE THE # OF CHARACTERS/STRING 80 N=(V*(C+1))+1 : REM COMPUTE THE # OF BYTES OF MEMORY NEEDED 90 PRINT: PRINT 100 PRINT “YOU NEED TO ALLOCATE”,N,“BYTES OF MEMORY FOR”, V,“VARIABLES” 110 PRINT “CONTAINING”,“C,CHARACTERS EACH” 120 PRINT: PRINT 130 PRINT“ STRING”,N,“,”,C 140 END uRUN ENTER # OF BYTES OF MEMORY YOU CAN ALLOCATE FOR STRINGS 500 HOW MANY STRING VARIABLES WILL YOU NEED ? 15 YOU NEED TO ALLOCATE 496 BYTES OF MEMORY FOR 15 VARIABLES CONTAINING 32 CHARACTERS EACH STRING 496 , 32 READY u

5–79

Page 117

Chapter 5 Operating Functions

5.10.3 String Support Calls (continued)

5.10.3.1 String Repeat – CALL 60 This routine allows you to repeat a character and place it in a string. You can use the String Repeat when designing output formats. First PUSH the number of times to repeat the character, then PUSH the number of the string containing the character to be repeated. No arguments are POPed. You cannot repeat more characters than the string’s maximum length.

u10 REM STRING REPEAT EXAMPLE PROGRAM u20 STRING 1000,50 u30 $(1)=“*” u40 PUSH 40 :REM THE NUMBER OF TIMES TO REPEAT CHARACTER u50 PUSH 1 :REM WHICH STRING CONTAINS CHARACTER u60 CALL 60 u70 PRINT $(1) u80 END uRUN ***************************************** READY

5–80

Page 118

Chapter 5 Operating Functions

5.10.3 String Support Calls (continued)

5.10.3.2 String Append (Concatenation) – CALL 61 This routine allows you to append one string to the end of another string. The CALL expects two string arguments. The first is the string number of the string to be appended and the second is the string number of the base string. If the resulting string is longer than the maximum string length, the append characters are lost. There are no output arguments. This is a string concatenation assignment. (Example: $(1)=$(1)+$(2)).

u10 STRING 200,20 u20 $(1)=“How are” u30 $(2)=“you?” u40 PRINT “BEFORE:”, u50 PRINT “$1=”,$(1),“ $2=”,$(2) u60 PUSH 2 :REM STRING NUMBER OF STRING TO BE APPENDED u70 PUSH 1 :REM BASE STRING NUMBER u80 CALL 61 :REM INVOKE STRING CONCATENATION ROUTINE u90 PRINT “AFTER:”, u100 PRINT “$1=”,$(1),“ $2=”,$(2) u110 END uRUN BEFORE: AFTER:

$1=How are $2=you? $1=How are you? $2=you?

READY

5.10.3.3 Number to String Conversion – CALL 62 This routine converts a number or numeric variable into a string. You must allocate a minimum of 14 characters for the string. If the exponent of the value to be converted is anticipated to be 100 or greater, you must allocate 15 characters. Error checking traps string allocation of less than 14 characters only. PUSH the value to be converted PUSH the number of the string to receive the value. Example: u10 STRING 100,14 u20 INPUT “ENTER A NUMBER TO CONVERT TO A STRING ”,N u30 PUSH N u40 PUSH 1: REM CONVERT NUMBER TO STRING 1 u50 CALL 62: REM DO THE CONVERSION u60 PRINT $(1) u70 END

5–81

Page 119

Chapter 5 Operating Functions

5.10.3 String Support Calls (continued)

5.10.3.4 String to Number Conversion – CALL 63 This routine converts the first decimal number found in the specified string to a number on the argument stack. Valid numbers and associated characters are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ., E, +, -. The comma (,) is not a valid number character and terminates the conversion. Two bytes are POPed after the CALL: validity of the value actual value If the string does not contain a legal value a 0 (zero) is returned. A valid value is between 1 and 255. PUSH the number of the string to convert. Two POPs are required. First POP the validity value, then POP the value. If a string contains a number followed by an E followed by a letter or non-numeric character, it is assumed that no number was found since the letter is not a valid exponent (UAB701EA returns a zero in the first argument popped indicating that no valid number was in the string). Example: u10 STRING 100,14 u20 INPUT “ENTER A STRING TO CONVERT”,$(1) u30 PUSH 1: REM CONVERT STRING 1 u40 CALL 63: REM DO THE CONVERSION u50 POP V,N u60 IF V tu 0 THEN PRINT $(1),“ ”,N: GO TO 80 u70 PRINT “INVALID OR NO VALUE FOUND” 80 END

5–82

Page 120

Chapter 5 Operating Functions

5.10.3 String Support Calls (continued)

u10 u20 u30 u40 u50 u60 u70 u80 u90 u100 u110

5.10.3.5 Find a String in a String – CALL 64 This routine finds a string within a string. It locates the first occurrence (position) of this string. This call expects two input arguments . The first is the string to be found, the second is the string to be searched for a match. One return argument is required. If the number is not zero then a match was located at the position indicated by the value of the return argument. This routine is similar to the BASIC INSTR$(findstr$,str$). (Example: L=INSTR$($(1),$(2))

REM SAMPLE FIND STRING IN STRING ROUTINE STRING 1000,20 $(1)=“456” $(2)=“12345678” PUSH 1 :REM STRING NUMBER OF STRING TO BE FOUND PUSH 2 :REM BASE STRING NUMBER CALL 64 :REM GET LOCATION OF FIRST CHARACTER POP L IF L=0 THEN PRINT “NOT FOUND” IF Lu0 THEN PRINT “FOUND AT LOCATION ”,L END uRUN FOUND AT LOCATION 4 READY

5–83