Programming

Script Basic

Script Basic is an implementation of old fashioned "standard" Basic, the way it was before visual everything. It is ideal for creating applications that need to interface a device with a proprietary ASCII protocol to an open control system (Modbus or BACnet).

Script Basic may be found in these products:

AddMe Jr. Basic
Babel Buster SP Custom
i.Board2
i.Board

The following screen shot shows the View/Edit view of an application in which a proprietery ASCII protocol is being translated to BACnet IP. The line that begins with "print #1" is sending a command to the ASCII device, the "line input" retrieves the reply (waiting for up to 0.5 seconds as defined by the timeout command). Then standard Basic string functions are used to parse the reply.

The built-in editor is acceptable for working with small programs, but you may wish to use a more powerful Windows editor for larger programs. If you do, the upload is a simple HTML file upload.

The Virtual Terminal provides a means to do simple debugging. Any print or line input statements that are not directed to files will use the virtual terminal. If the program is running in the background (configured as the auto-run program), virtual terminal output will be discarded, and virtual input will return nulls.

The one feature that makes the Control Solutions implementation of Script Basic really useful is file I/O, and the special files in particular. This is the mechanism for communication with an external serial ASCII device, or with an external TCP device connected via a socket connection. This is where you tame the wild side of proprietary.

File I/O - Includes COM port I/O 

ScriptBasic handles the files the same way as any other BASIC type language. You open a file, read from it, write to it and finally close the file. To make a jumpstart see a simple example:

open "myfile.txt" for output as 1
print#1,"This is the first line of myfile\n"
close 1

open "myfile.txt" for input as 1
line input #1, a
close 1
print a

This simple program opens the file named `myfile.txt' in the current directory, prints a single line into it, and closes the file. Next time it opens the file for reading, reads a line from it, closes the file and prints the line read from the file to the screen.

When you open a file you have to have a file number that you want the file associated with. In the example above this number is 1. This is called many times the "file number". Whenever you do something with an opened file you have to use the file number.

You may open two "special" files for file access. These are a TCP socket, and the serial communications port (if supported by hardware, which it is on AddMe Jr. Basic).

This example will open the COM port and simply echo each line entered back to the port:

open "COM:38400" for comm as 1
line input #1, mystring
print #1, mystring

This example will open a TCP socket and query a remote web server, and print the page returned by the server to the virtual terminal page:

on error goto ErrorLabel
open "192.168.1.112" for socket as 1
print #1, "GET /UE/QueryCGI?reg1 HTTP/1.1\r\n"
print #1, "Host: icandoit\r\n"
print #1, "User-Agent: Control Solutions i.Board\r\n"
print #1, "Accept: text/xml,text/html\r\n"
print #1, "Keep-Alive: 300\r\n"
print #1, "Connection: keep-alive\r\n"
print #1, "\r\n"
while not eof(1)
line input #1, a
print a
wend
close 1
stop
ErrorLabel:
print "Server unreachable\n"

Communicating with a proprietary device is of little use if you cannot translate the data to something accessible to an open system. This is where the second most useful feature of the Control Solutions implementation of Script Basic comes into play. Numeric data may be exchanged between your Basic program and Modbus registers or BACnet objects (as applicable for the product). Write data to the open system, or read data from the open system, using a standardized register access.

Register Access

You may read any of the local registers using the getreg function, and write them using the setreg command. Note that getreg is a function while setreg is not; it is a command and therefore requires no parenthesis. Consider the following example:

    MyData = getreg (22)
    MyData = MyData * 2.5
    setreg 24, MyData

The above example will place the contents of register #22 in the variable MyData, then multiply it by 2.5, then place that value in register #24. Variables may be used in place of the constants used in the above example.

BACnet/IP objects are accessed via the register interface. Register numbers are BACnet object type multiplied times 1,000 plus object number starting at #1. Register numbers corresponding to BACnet objects are as follows:

Object Type Object Number Register Number
Analog Input AI 1-300 1-300
Analog Output AO 1-100 1001-1100
Analog Value AV 1-100 2001-2100
Binary Input BI 1-300 3001-3300
Binary Output BO 1-100 4001-4100
Binary Value BV 1-100 5001-5100
Multi-State Input MI 1-300 13001-13300
Multi-State Output MO 1-100 14001-14100
Multi-State Value MV 1-100 19001-19100