| 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"
|