Time Saving Programming John Markham, Janet Albers
Short Cut
Short Cut Basic Measure and Store Programs Sensor Connections Setting up DataTables Cards and the SC115 are supported Wiring Diagrams copy and paste from *.DEF file All new Edlog programs. If it can t be written and maintained completely in Short Cut use a CRBasic datalogger. Multiplexers Calculations & Control
Custom Sensors
Custom Sensors Article https://www.campbellsci.com/blog/create-customsensors-short-cut
CRBasic Functions and Subroutines
About Subroutines and Functions Independent section of code that performs a specific task and can return a value or modify an argument Use when you need to: Develop and/or debug a large program (500 lines) Reuse code (3 or more times) Share code with others, including customers
Function Structure A Function returns a value. Write that value to a variable. Function CtoF_Func(TempC) Return (TempC * 1.8 + 32) EndFunction PTemp_F01 = CtoF_Func(PTemp_C)
Subroutine Structure A Subroutine is Called. Sub Units_Sub(TempC, TempF, TempK) TempF = TempC * 1.8 + 32 TempK = TempC + 273.15 EndSub Call Units_Sub(PTemp_C, PTemp_F02, Ptemp_K01)
Which should I use? Subroutine First choice or you re not sure which to use Multiple values need to be returned An array of values needs to be returned Working with strings holding data containing nulls Function You only want to return a single value, especially inside an expression for ease of use / readability TempF = CtoF(TempC) If CtoF(TempC) > 80 Then ACon = TRUE
Do: Use Subroutine unless Function is needed Structure the Subroutine or Function so that it is easily reusable to those already familiar with CRBasic instructions Use local variables whenever possible Initialize your local variables each call Break up complex processing tasks into several simpler ones Get in the habit of using Call when calling a subroutine
Don t: These probably work but test thoroughly. Put measurements in Functions Put measurements in Subroutines with arguments Put Scan/NextScan inside Subroutines or Functions Use recursive calls or deep nesting
Recycle Code
Only sell one kind of station The Easy Way
Challenges Different sensor combinations Different data collection requirements Different wiring/physical setup Different communication options
Strategies Don t write the same code twice Reduce, Reuse, Recycle Think ahead Start ahead of the game Consolidate settings Make a Master Program Write for everything, use what you need Divide and conquer
Don t Write The Same Code Twice: Modular Programming Write code in discrete pieces that can be moved between programs Keep track of all related code Variables/Data Tables Measurements Processing Update code with configuration details of new station
Don t Write The Same Code Twice: Using Functions and Subroutines Reuse the same code multiple times in the same program More info in the Function and Subroutine section
Think Ahead: CRBasic Templates Generate a cache of basic programs for different station types Establish program structure Create naming conventions
Think Ahead: Constant table ConstTable.EndConstTable Give it a name Reference constants where possible in program Use conditionals to select sections of code to run
Think Ahead: Constant table Change constants from multiple locations Terminal Table Monitor Keypad Watch out for recompile
Make A Master Program: Config File Master program is identical on every station Lock down propriety code Pull constant table out of program Include ( Drive:FileName )
Config File
Make A Master Program: DLD files CRBasic DLD files Edlog DLD files CRBasic.DLD files can be run on any logger. Beware: Some instructions/parameters are logger specific Select a compiler in CRBasic Editor
Make A Master Program: Conditional Compiling Allows only part of a program to be executed at run time #If...#Else...#Endif Allows declaration of parallel variables/constants LoggerType function Combine a DLD program, config file, and conditional compiling to create a Master Program Conditional Compile and Save
Loops and Logic If Then Else
Select Case Basic conditional statements If Then Add additional conditionals with else/else if statements Consolidate similar conditional statements into Select Case statements where possible
Bitwise Logic: AND, OR, NOT Combine nested or multiple If Then statements with AND, OR, and NOT for brevity and clarity Only performed on Integers. Other types converted before evaluated Beware: True (-1) 11111111, False(0) 00000000
For Next Executes a block of code a specified number of times Great for arrays Multi-dimensional arrays can be handled with nested For Next loops
Do.Loop Loops run the same code repeatedly While a condition is true Until a condition is met ExitDo While Wend is still accepted syntax, but not as flexible Watch out for infinite loops
Comments And Documentation
Need I say more? Put Comments in your CRBasic Programs Short Cut puts comments in. Your successor will thank you! Your favorite Support Engineer will thank you! You will thank yourself!
CRBasic Comments Header (Template) Version number Customer/Installation information Author s contact information Date Summary of changes made Wiring (from Short Cut *.DEF file)
CRBasic Comments Use comments to explain WHY you added an instruction or a section of code
CRBasic Comments Document all expressions or equations Use headings for each program section
CRBasic Comments Put comments at the end of Subroutine and Function declarations. They show up in the search.
CRBasic Comments When a sensor returns a group of variables (an Array) document what each value is. (And/or use Alias)
Readable Results
Readable Results
Debugging Tips
Debugging Tips: Hardware Use terminal window to sniff port traffic Check wiring and cabling If it is a serial problem switch Tx and Rx Does the hardware match the software/code
Debugging Tips: Code Use timers around loops Use counters in loops and logic Make all variables Public Slow scan rate or limit the number of runs to see what is happening Create variables to store intermediate values of interest Version programs
Debugging Tips: Code Test each 'module' separately (you did write modular code, right?) Comment out code until you find the offending lines
Debugging Tips: Rethink The Problem Get someone else to look at it Work on something else for a while and come back later Clearly articulate your problem to someone else (even an imaginary friend)
Debugging Tips: Compiler Just because the PC CRBasic compiler says a program can compile OK, doesn't mean it will run, or even compile in a real datalogger. Datalogger may have different (usually older) operating system Timing. PC can t check timing. Check the Status Table Use error line references in compile window Be careful, sometimes the error is precipitated by something going wrong before
Debugging Tips: Compiler Status Table Compile Results gives run-time errors Updated as program runs, not just at compile time Variable out of bounds Watchdog info memory errors
The End