The text of a manual · part 21 of 25
Attended Workstations (Catalog Nos. 2708-DH5B2L & -DH5B4L) (Series B) User Manual, pages 146 to 153
Pages 146 to 153 of 186: K - A-B VBASIC and Visual BASIC Tips; L - Differences Between A-B VBASIC and Visual BASIC
The original document is the PDF (513 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 2708
Page 146
Appendix
A–B
K
A-B VBASIC and Visual BASIC Tips Misspellings Don’t misspell names. BASIC is unforgiving of such errors since it will just create a new variable which is zero or blank. Strings in TYPEs Strings which are defined in TYPE statements are of a fixed length. These are always blank padded on the end. Variables Beginning with “FN” Do not start any variable name with FN, Fn, Fn, or fn. BASIC treats these as a reference to the archaic DEF FN.... mechanism and produces a syntax error. Accidental Omission of %, &, ! # OR $ If you leave the trailing character off of variable names, you will usually get no warning from BASIC. Instead, the variable will be a different one than what you expected. Watch out for this! If you happened to pick a String/Integer mismatch, you will get syntax errors. Integer/Real mismatches are very difficult to locate. Another peculiarity of trailing characters is that if you have a function named XYZ!, and you attempt to reference that function as XYZ# or XYZ% or anything other than XYZ! or XYZ then you will get a MULTIPLE DEFINITION error at the function declaration even though the reference is many lines later. Use of Colons as Statement Separators Use of colon “:” as a statement separator can cause a number of problems with both Visual BASIC and A-B VBASIC. An example follows: CL2 : PRINT “This is a test” ;
K–1
Page 147
Appendix K A-B VBASIC and Visual Basic Tips
The programmer assumed that this statement would clear the second line of the display and then print “This is a test”. But, this is not what actually happens! This is compiled as: CL2 :
PRINT “This is a test” ;
where CL2 is a local label which is effectively ignored. The rule should be NEVER have a line which looks like: < identifier > : < rest of line > unless you INTEND for <identifier> to be a label. CONSTant Declarations “CONST name = value” does not quite follow the same rules as variable assignments in the same position. The major difference is that the type of ‘name’ is determined by the contents of ‘value’. A DEFINT or DEFSNG #, or $ after the name to set the type explicitly. The trailing symbol is NOT required when the constant is referenced. PRINT “text”; : <statement> A problem for Pascal and C programmers is coding: PRINT “text”, <statement> instead of: PRINT “text”; : <statement> If the statement is a function such as “e = ReadStr%(3,s$)” then the result is for e to retain its previous value, and for an extra 0 or 1 to follow the printed text.
K–2
Page 148
Appendix
A–B
L
Differences Between A-B VBASIC and Visual BASIC This document describes the differences between Allen-Bradley VBASIC and Microsoft Visual BASIC. It first lists general characteristics unique to A-B VBASIC, and then provides additional information on keywords supported by A-B VBASIC. In this discussion, all references to “VBDOS” refer to Visual Basic for DOS. Also any references to “A-B VBASIC” refers to A-B VBASIC, used for programming a DH5 terminal. These references are for our convenience and do not refer to any trademark. Any time the “VBDOS manual” is mentioned, we are referring to the Visual Basic Reference Manual for MS–DOS. To some extent, most comments here also apply to QuickBASIC V4.5, Microsoft PDS 7.1, and QBASIC versions of the BASIC language.
General Restrictions
1. All arrays must be DIMensioned. 2. STATIC and DYNAMIC arrays and metacommands do not apply in A-B VBASIC. 3. Periods are not allowed within a variable name, unless that variable is part of a user defined TYPE. 4. The entire A-B VBASIC program (i.e. the input to LXB), must reside in one file. 5. The exponentiation operator (^), may have a positive integer exponent only. 6. “@” AND “[“ AND “]” are invalid outside literals. 7. Some of the general syntax implemented by some BASIC compilers is not allowed. The following syntax may be familiar to some users but is not mentioned in the VBDOS manual and is not recognized by the A-B VBASIC cross–compiler (LXB): [ and ] instead of ( and ) to delimit array subscripts. ? as a synonym for PRINT. PRINT#, INPUT#, and WRITE# without a space preceding #. 8. DOUBLE precision floating–point data A-B VBASIC does not support double precision real arithmetic. Specifically, the interpreter in the terminal is not programmed to do DOUBLE math, and may or may not generate a “Double not supported” error message whenever it encounters anything which has to do with DOUBLE operations, depending on firmware version. However, the A-B VBASIC cross–compiler accepts the following DOUBLE–related syntax in most respects, and you may not see an error message until the program is downloaded into the terminal and executed: DEFDBL L–1
Page 149
Appendix L Differences Between A-B VBASIC and Visual BASIC
# suffix on variables Floating–point constants with 8 or more significant digits AS DOUBLE clauses Functions involving DOUBLE, such as CVD and MKD 9. Restriction on the exponentiation operator Exponentiation is implemented by repetitive multiplication, so small exponents are recommended. Full real exponentiation is provided in the function called “Power!” in the BASIC source file MATH.BAS. A-B VBASIC supports exponentiation to positive integer powers only. For example: SUPPORTED: X^2 NOT SUPPORTED : X^5.7 or X^–2
10.Unsupported math intrinsics These intrinsic functions, relating to exponent and trigonometric functions are not implemented in the interpreter running in the DH5 terminal, but BASIC source code implementing all of the below is provided in the file MATH.BAS and may be copied into programs as required: ATN COS EXP LOG SIN SQR TAN
Supported A-B VBASIC Keywords
ABS(numeric–expression) Returns the absolute value of a numeric expression. ABS(–1) and ABS(1) are both 1.
ASC(string–expression) Returns the ASCII value of the first character in string–expression
BEEP Sounds the speaker. Produces the same sound as PRINT CHR$(7).
CDBL(numeric–expression) No longer supported. Converts a numeric–expression to a double–precision number. Note the restrictions on DOUBLE precision usage above.
CHAIN filespec Transfers control from current program to another program residing in file memory. COMMON is NOT supported. Therefore, all variables are lost between the CHAIN unless first stored in a file and retrieved after the CHAINing. L–2
Page 150
Appendix L Differences Between A-B VBASIC and Visual BASIC
LINE INPUT Request$ IF Request$ = “JUMP” THEN CHAIN “JUMP”
The interpreter does not tolerate a CHAIN to a non–existent program. Make sure that you have downloaded any program which will be CHAINed to, before any other program attempts to CHAIN to it, or the program will crash with a “Bad Chain File” error message. A special set of Network Directives is required to download a program for the express purpose of a subsequent CHAIN. See the User’s Guide for a further discussion of these Network Directives.
CHR$(code) Returns a one–character string whose ASCII code is the argument. Msg$ = CHR$(34) + “Quoted string” + CHR$(34)
This example adds a double–quote character to the beginning and the end of the string.
CINT(numeric–expression) Converts a numeric–expression to an integer by rounding the expression’s fractional part.
CLNG(numeric–expression) Converts a numeric–expression to a long (4–byte) integer by rounding the expression’s fractional part.
CLOSE [ [#]filenumber[,[#]filenumber]...] Concludes I/O to a file or device. Closing some reserved files, such as HOST, have additional side effects. See the description of those devices for details. OPEN “EMPLOYEE” FOR OUTPUT AS #1 PRINT #1, “Employee –”;EmpBadge$ CLOSE #1
CLS [{ 0 | 1 | 2 }] Clears the front panel LCD, homes the cursor. All forms shown are accepted, but all have the same effect.
CONST constname=expression In a procedure a CONST overrides a GLOBAL variable of the same name. The text of a string CONST is to be present in one place, regardless of the number of times it is referenced. However, because string CONST L–3
Page 151
Appendix L Differences Between A-B VBASIC and Visual BASIC statements produce executable code, they should be placed at the start of a program. CONST FALSE = 0, TRUE = NOT FALSE
CSNG(numeric–expression) Converts a numeric expression to a single precision value.
CSRLIN Returns the current line(row) of the cursor on the front panel display. See also POS(0). Row = CSRLIN LOCATE Row, 5
CVI, CVS, CVL, (CVD) (CVD is no longer supported) See VBDOS manual for specific information. TYPE...END TYPE is much more versatile for use with random files.
DATE$ Returns a string containing the current date or sets the date. The format of the date string is yymmddw, where yy is the year, mm is the month, dd is the day, and w is the day of the week (1 = SUNDAY). This is different than the VBDOS format! DateString$ = DATE$
DATE$ = ”yymmddw” Sets the current date. See format above. Note that A-B VBASIC will automatically compute and update the day of the week (w) if it is omitted or specified incorrectly. This method of setting the actual date is in addition to the Setup Menus and the Network Directives which allow the host to set the date. Note also that this format is different than the VBDOS date! See also TIME$. DATE$ = “931115”
‘Set the date to “11/15/1993”
DECLARE {FUNCTION | SUB} name(parameters) Declares references to BASIC procedures and invokes argument type checking. Sometimes the VBDOS editor insists on adding a clause reading AS ANY. This syntax is tolerated but is unable to perform full parameter verification. SEE “prototyping”. L–4
Page 152
Appendix L Differences Between A-B VBASIC and Visual BASIC
DECLARE FUNCTION GetTime$() DECLARE SUB SetTime$()
The generic name for declaring a procedure’s name and its parameters is “prototyping”. LXB.EXE creates a prototype for every procedure encountered whether by the DECLARE statement, the FUNCTION statement, or the SUB statement. To allow the user to include files of DECLARE statements without regard to whether the code for each procedure is also included, LXB will not issue an error if a procedure is DECLAREd but never defined. Multiple DECLAREs of the same procedure are permitted but should be identical. Use of a procedure without its definition will cause an error. Be aware that the various editors used by Microsoft in their BASICs (QB, QBX, VBDOS, QBASIC, etc.) usually sort subroutines into alphabetic sequence, which can ruin any arrangements you may have made to eliminate DECLARE statements. If prototypes are not present in the source when VBDOS runs a program, it simply creates them. However, if they are missing when the LIB compiler is invoked, a compiler error results.
DEFINT letterrange[,letterrange]... DEFSNG letterrange[,letterrange]... (DEFDBL letterrange[,letterrange]...) No longer supported DEFLNG letterrange[,letterrange]... DEFSTR letterrange[,letterrange]... Defines the default type of all variables beginning with the indicated letter range as either integer, single precision, double precision, long integer, or string. A-B VBASIC converts any double precision operations to single precision, so see the details in the “General Restrictions” section. Generally it is best to define all variables with the following command at the very beginning of the program, so that all math operations use the fastest form of arithmetic processing. Variables which need to be floating point can then be explicitly typed by using the ! or # trailer. DEFINT A–Z X! = 123.45
’Overrides DEFINT statement!
DIM [SHARED] variable[(subscripts)][AS type]... Declares a variable and allocates storage space. VBDOS will not allow a fixed length string (nor an array of them) to be passed to a procedure, neither will A-B VBASIC. So, as an alternative, use TYPE..END TYPE to define a variable or array of fixed length strings, and pass it instead. L–5
Page 153
Appendix L Differences Between A-B VBASIC and Visual BASIC
OPTION BASE is not supported, so in order to set the lower subscript use the following: DIM A(lower TO upper) if zero is not preferred as the lowest subscript. A declaration using AS precludes variables of the same name, even with type suffixes. For example, NumOfBytes! = 10 will cause an error if there was a preceding DIM NumOfBytes AS INTEGER DIM SHARED ON$, OFF$, Now AS INTEGER
DO...LOOP [{WHILE/UNTIL} booleanexpression] Repeats a block of statements WHILE a condition is TRUE or UNTIL a condition becomes TRUE. DO: X = X + 1: LOOP WHILE X < 200
END [{FUNCTION/IF/SELECT/SUB/TYPE}] Ends a BASIC program, procedure, or block. See FUNCTION, IF, SELECT, SUB, and TYPE for the particular statement being ended. See VBDOS manual for detailed information. See Also SYSTEM.
EOF(filenumber) Tests for the end of file condition. See the chapter on Special Devices for details about specific devices.... Effect of EOF on a sequential file: TRUE is returned whenever the file position is = file length. (End–of–File is reached) Effect of EOF on binary or random files: TRUE is returned if the previous GET did not get “all” the data that was requested.
• For random reads, “all” means the number of bytes specified as the LENgth in the OPEN, or the variable’s size, whichever is less. • For binary reads, “all” means the size of the variable. The difference between binary/random and sequential mode is that, in the former, EOF returns FALSE even though the next GET might not return any data. Therefore, for binary and random files, do a GET but then test EOF. If TRUE is returned, the variable does not contain a full record and may not contain anything at all. Subsequent calls to EOF will continue to return TRUE. EOF “latches” in this state until a SEEK operation is performed. Some statements also reset this latch because they do an implied SEEK as part of their operation. See OPEN example on next page. OPEN “EMPLOYEE” FOR INPUT AS #1 DO WHILE NOT EOF(1) LINE INPUT #1, EmpBadge$ IF EmpBadge$ = BadgeInput$ THEN L–6