Variables

In CRBasic, the declaration of variables (via the DIM or the PUBLIC statement) allows an optional type descriptor As that specifies the data type. The data types are Float, Long, Boolean, and String. The default type is Float.

Example variables declared with optional data types

Public PTemp As Float, Batt_volt

Public Counter As Long

Public SiteName As String * 24

As Float specifies the default data type. If no data type is explicitly specified with the As statement, then Float is assumed. Measurement variables are stored and calculations are performed internally in IEEE 4 byte floating point with some operations calculated in double precision. A good rule of thumb is that resolution will be better than 1 in the seventh digit.

As Long specifies the variable as a 32 bit integer. There are two possible reasons a user would do this: (1) speed, since the CR6 Operating System can do math on integers faster than with Floats, and (2) resolution, since the Long has 31 bits compared to the 24 bits in the Float. A good application of the As Long declaration is a counter that is expected to get very large.

As Boolean specifies the variable as a 4 byte Boolean. Boolean variables are typically used for flags and to represent conditions or hardware that have only 2 states (e.g., On/Off, High/Low). A Boolean variable uses the same 32 bit long integer format as a Long but can set to only one of two values: True, which is represented as –1, and false, which is represented with 0. When a Float or Long integer is converted to a Boolean, zero is False (0), any non-zero value will set the Boolean to True (-1). The Boolean data type allows application software to display it as an On/Off, True/False, Red/Blue, etc.

The CR6 uses –1 rather than some other non-zero number because the AND and OR operators are the same for logical statements and binary bitwise comparisons. The number -1 is expressed in binary with all bits equal to 1, the number 0 has all bits equal to 0. When –1 is anded with any other number the result is the other number, ensuring that if the other number is non-zero (true), the result will be non-zero.

As String * size specifies the variable as a string of ASCII characters, NULL terminated, with an optional size specifying the maximum number of characters in the string. A string is convenient in handling serial sensors, dial strings, text messages, etc. When size is not specified, a default of 24 characters will be used (23 usable bytes and 1 terminating byte).

As a special case, a string can be declared As String * 1. This allows the efficient storage of a single character. The string will take up 4 bytes in memory and when stored in a data table, but it will hold only one character.

Structures (StructureType/EndStructureType) are an advanced technique used to organize variables and display data in a structured manner. They can significantly shorten program code, especially for instructions that output an array of values, such as AVW200()GPS(), and SDI12Recorder(). For example, a single StructureType may be used to organize and display data for multiple vibrating wire sensors or many SDI-12 sensors without creating aliases for each sensor. See the CRBasic Editor help for detailed instruction information and program examples: https://help.campbellsci.com/crbasic/cr6/ .