Using Python With The Xtralien X100

Overview

This tutorial gives an introduction to controlling the Xtralien X100 using Python and assumes a working knowledge of basic Python usage, including how to install Python packages onto your system.

Communicating with the X100

When communicating with the X100 board over either USB or Network connection, the most important thing to remember is that everything is simply an ASCII string so you can easily read and interpret the communications.

If you want to connect to the X100 board over USB you would be best using the PySerial module, which you can install through pip or easy_install.

Life is even easier if you would prefer to connect over the network connection as all you will need is already provided along with Python in the form of the socket library.

If you simply want to just get up and start then you can also simply install the xtralien python module, as it will install all of the tools you might want to easily connect.

For beginners to Python we recommend using the library to communicate with the Xtralien X100 because it will help you get started without having to worry about any format-specific issues.

Using the Library

Firstly, install the libraries by running one of the following commands.

$ pip install xtralien

or, if you have installed Anaconda

$ conda install xtralien

After this you should be able to use the xtralien module to find and command all of the boards in your network at once. This is possible using a simple script, like below.

# import the library
import xtralien

# Create a connection to the device
device = xtralien.Device('COM1')

# Print hello world
print(device.cloi.hello(format='auto'))

This should print a list of all the CLOI-based devices available on the network. If this doesn't work, or if you are on a large network it may be better to simply add a device manually. The xtralien library defines some classes that encapsulate additional helper functions so that you, as a user have to worry about less, such as converting the strings to the correct encoding.

An example of this is below, using the example of a device located at 192.168.2.33, like you can see in the image.

# import the library
import xtralien

# Create the device, with a socket connection
device = xtralien.Device("192.168.2.33", 8888)

# send a command to test the device works
response = device.command("cloi hello", wait=True)

# print the response, which should be "Hello World"
print(response)

Using a network connection

Note: It is also easy to simply use the xtralien module to connect, as there is a utility to scan the network automatically

Connecting over a network connection is nice and easy using the socket library. All you need to do is create a TCP connection to port 8888 on the IP address of the board.

An example of this might be 192.168.2.33, like seen above, in which case you should be connecting using a command like below.

# Import the required library
import socket

# Create the initial socket
connection = socket.socket()

# Connect the local socket to the one on the board
connection.connect(("192.168.2.33", 8888))

# Send a "cloi hello" command
connection.send("cloi hello")

# Make sure that we got "Hello World!" back
response = connection.recv(576)
if response == "Hello World\n":
    print("Success")
else:
    print("Not an xtralien board")

Using the above code you should be able to connect to an xtralien board at 192.168.2.33 on port 8888, and if you see Success printed out on the screen then you have sent a CLOI command, well done.

Using a serial connection

Note: On Windows the COM port can change upon reset, however this can be usually fixed by resetting the board either 1 or 2 more times. This does not appear to happen on UNIX-based machines.

Python provides no serial library by defualt, so you can install PySerial instead, which is community-provided code under the Python Software Foundation License, which is an Open-Source approved license.

To install PySerial simply run

$ pip install pyserial

or if you run Anaconda on your system run

$ conda install pyserial

Using it is almost as simple as using the built-in socket library, with the addition of having to know which serial port you need to select.

# Import the required library
import serial

# Create a Serial Connection on COMX (where X is the port)
# Note: This will be different on different platforms,
#       the one shown is Microsoft Windows
connection = serial.Serial('COMX')

# Send a "cloi hello" command
connection.write(b"cloi hello")

# Read the respons
response = connection.read(12)
if repsonse == b"Hello World\n":
    print("Success")
else:
    print("Not an xtralien board")

The above code will connect to the serial/COM port COMX and test the board to make sure it is an xtralien board. If everything is plugged in and connected then running the above code you can check if you are using the correct COM port.