The key functions of the server are:
When a client connection is established, the client/server can:
The server is written in Python (with one small section of C code to handle initialising the serial port and the low level read/write details).
Python is a simple, yet powerful programming language that bridges the gap between C and shell programming, and is thus ideally suited for 'throw-away programming' and rapid prototyping. Its syntax is put together from constructs borrowed from a variety of other languages; most prominent are influences from ABC, C, Modula-3 and Icon.
The Python interpreter is easily extended with new functions and data types implemented in C. Python is also suitable as an extension language for highly customizable C applications such as editors or window managers.
Python is available for various operating systems, amongst which several flavors of Unix, Amoeba, the Apple Macintosh O.S., and MS-DOS.
Prior to using Python, the testing software was developed in C, and as the hardware and controller software was slowly assembled, the test programs would be extended, piece by piece. This became very unwieldy, very rapidly - despite our best efforts at designing the test software before we started (aka the joys of research!). There was also the familiar edit-compile-test cycle that tends to inhibit prototyping software.
The combination of Python's interactive interpreter and object orientation allowed us to rapidly develop (and extend) test classes that modelled the hardware. The very high level nature of the language allows one to easily interface to real-world systems and interactively manipulate (and extend) the system.
We were so pleased with Python's power and elegance that we moved beyond using it as a rapid development/prototyping language, and have used it as the basis for the final server product as well. Python's networking support at both the socket level and protocol level (eg. http, nttp and ftp support are all available as library functions) makes it an excellent language for rapidly developing robust networking tools.
import rs485 # RS-485 interface library import node # basic node class library rs485.init() # initialise the network myNode = node.Node(2) # set up node 2 while 1: # loop until the node goes off-line status = myNode.ping() # ping the node if status != NODE_UP: print "Node is not responding" break else: print "Node is responding..." time.sleep(60) # sleep for 60 secondsThe following script creates a new class of node called TestNode that pings the node when the node object is created to make sure it is connected and offers a new method which simply pings the node once a minute for ten minutes to check the node.
import node class TestNode(Node): def __init__(self): # object initialisation Node.__init__(self) if self.ping != NODE_UP: raise NodeException, "Node is not connected" def testMe(self): for i in range (0, 10): if status != NODE_UP: print "Node is not responding" break else: print "Node is responding..." time.sleep(60) # sleep for 60 secondsWhile these scripts are trivial examples, note that there are no braces or BEGIN/END statements: control blocks are determined by the indentation level. You either love it or you hate it!
Note also that typing is dynamic, but, unlink Tcl, Python supports a wide range of data types, such as integers, floats, strings, lists, tuples, and arrays.