This article describes how to communicate with a Brainboxes Ethernet IO Module (or Remote IO module) using the Python programming language.

Requirements

  • A computer with a recent version of Python 2 or Python 3 installed
  • The Brainboxes Python Module (download from this page)

Sample Code

Download and unpack the zip file at the top of the page. It contains two files: a module file ‘brainboxes.py’ which defines a class for communicating with Brainboxes Ethernet IO Modules, and a script ‘Brainboxes_AsciiIo_demo.py’ which demonstrates the usage of that class. You can either put ‘brainboxes.py’ in the same directory as the example script, or save it somewhere on Python’s module search path.

Before you run the example script, edit the IP address on line 8 to be the address of a Brainboxes Ethernet IO Module on your network. Also edit the TCP port number if you have changed it from the default of 9500. The output of the program should look something like this:

>C:\Python34\python -u "Brainboxes_AsciiIo_demo.py"
Sending command b'$01M'
	Response was b'!01ED-538'
Sending command b'~**'
	No response expected
Sending command b'@01'
	Response was b'>00B2'
Sending command b'@02'
	No response received!
Sending command b'$016'
	Response was b'!00B200'

The code below is available to download as a zip from the top of the page.

#!/usr/bin/env python

# Example of using brainboxes.AsciiIo class for communication with Brainboxes ED-range products
# Tested with Python 2.7.9 and 3.4.3 on Windows, and 2.7.6 and 3.4.0 on Linux

import brainboxes

with brainboxes.AsciiIo(ipaddr='192.168.0.74', port=9500, timeout=1.0) as io:

	for txmessage in (b'$01M', b'~**', b'@01', b'@02', b'$016'):
		print("Sending command %s" % txmessage)
		if txmessage[1:3] == b'**':
			data = io.command_noresponse(txmessage)
			print(" No response expected")
		else:
			rxdata = io.command_response(txmessage)
			if rxdata is None:
				print(" No response received!")
			else:
				print(" Response was %s" % rxdata)