Les Smithson's Python extension for LegOS LNP

Introduction

Pylnp is a Python extension for the LegOS LNP library. This allows you to write Python scripts on Linux that talk to your LegOS application on the RCX, using the IR link. Writing Python scripts is faster & easier than writing 'C' programs. It also allows you to communicate with LegOS interactively.

Please note that this does not allow you to run Python on the RCX.

If you don't know what Python is, look here. It should be ready-installed on most standard Linux distributions.

Here's a quick example Python script that sends a message to the brick, and prints a reply:

 
# Import the LNP extension. This initializes lnp
import lnp
# Send a message to the RCX
lnp.iwrite('hello')
# Read & print the response
lnp.iread()
'world'
 

Simple, Eh?

Pylnp supports integrity & addressing read/write operations. Read calls may be blocking or non-blocking (although non-blocking reads don't work yet.)

Download

Download the Pylnp tarball here.

Installation & Running

These instructions are for Linux RedHat 6.0. It should work for most Linux distros. If not, let me know! I have not tested this on Windows.

Documentation

The lnp extension has lots of embedded doc strings. For convenience, I've reproduced them here:

lnp.__doc__

 
This module interfaces to LegOS lnp, using the lnpd package from 
Martin Cornelius (Martin.Cornelius@t-online.de). It allows a python script 
to send & received data via the IR tower to an RCX running LegOS. The
following functions are available:
  iread()                Blocking LNP integrity read
  ireada(callable)       Asynchronous LNP integrity read
  iwrite(buf)            LNP integrity write
  aread(port)            Blocking LNP addressing read
  areada(port, callable) Asynchronous LNP addressing read
  awrite(buf, dst, src)  LNP addressing write 

All reads are mutually exclusive. That is, only one asynchronous or
blocking integrity or addressing read may be outstanding at one time.
 

lnp.aread.__doc__

 
aread(port)

Performs a blocking LNP addressing read on port 'port', returning
the result as the tuple (buf, port), where buf is the buffer string
read from the RCX and 'port' is the RCX source port number. The
string may contain embedded nulls. Any outstanding asynchronous LNP
reads (integrity or addressing) are cancelled.
 

lnp.areada.__doc__

 
aread(port)

Performs a blocking LNP addressing read on port 'port', returning
the result as the tuple (buf, port), where buf is the buffer string
read from the RCX and 'port' is the RCX source port number. The
string may contain embedded nulls. Any outstanding asynchronous LNP
reads (integrity or addressing) are cancelled.
>>> print lnp.areada.__doc__
areada(port, callable)

Performs an asynchronous LNP addressing read. The callable object
'callable' is called with the string read from the RCX, and the RCX
port number. The string may contain embedded nulls. Any outstanding
asynchronous LNP integrity reads are cancelled.
 

lnp.awrite.__doc__

 
awrite(buf, dst, src)

Performs LNP addressing write, sending the buffer buf to the RCX.
The string may contain embedded nulls. dst & src are the lnp
destination & source ports.
 

lnp.iread.__doc__

 
iread()

Performs a blocking LNP integrity read, returning the result as a
string. The string  may contain embedded nulls. Any outstanding
asynchronous LNP reads (integrity or addressing) are cancelled.

lnp.ireada.__doc__

 
ireada(callable)

Performs an asynchronous LNP integrity read. The callable object 'callable' 
is called with the string read from the RCX. The string may contain 
embedded nulls. Any outstanding asynchronous LNP addressing reads are
cancelled.
 

lnp.iwrite.__doc__

 
iwrite(buf)

Performs LNP integrity write, sending the buffer buf to the RCX.
The string may contain embedded nulls.

Application Notes

I've used this for general hacking around with lnp & legOS.

Pylnp allows you to embed nulls in the data, so you can send binary as well as ordinary strings. Use the python struct module to handle binaries.

Multi-threaded applications must ensure that any lnp.read calls are made in the main thread. This is because python only delivers signals to the main thread.

It ought to be possible for a LegOS RCX program to send some python script to pylnp for interpretation. This might be usefull for pretty-printing data structures etc.

I've used pylnp as part of a gdb/legOS interface (see here for more info.). It sends messages between gdb connected via a socket & a debug stub running under legOS.

Disclaimer

You are free to use this code in any way you see fit, subject to the LegOS & Python disclaimers & copyrights. I make no representations about the suitability of this software for any purpose. It is provided "AS-IS" without warranty of any kind, either express or implied. So there.

Consulting

I'm available for Python, MQ, Unix & C/C++ consulting assignments. See http://www.open-networks.co.uk and my cv for more details.

Contact Me

Feedback, Questions, Comments, Suggestions & Bugs to lsmithson@open-networks.co.uk. Or see my Home Page.


(c) Copyright L. Smithson 2000. Last updated April 2000
$Id: pylnp.html,v 1.5 2006/01/24 17:24:31 lsmithso Exp $