Up and running in minutes with Python!

The following shows an example of a simple script that will automatically locate the Adaura Technologies device based on the specified serial number and send a command to it via PySerial.

See an application of this script on GitHub!
https://github.com/ozonejunkieau/AdauraAttenuator
Credit:
Tristan Steele
UNSW Canberra Space
Australian Defense Force Academy

 

from os import linesep
from serial import Serial
from serial.tools.list_ports import comports

class ComConnection(object):
"""Serial wrapper which can be instantiated using the device's serial number"""

def __init__(self, serial_number=None, command=None, baudrate=9600):
"""
Constructor

Parameters
----------
serial_number: string
The device's serial number
command: string
Command to be sent
baudrate: int
Baud rate such as 9600 or 115200 etc. Default is 9600
"""
self.serial_number = serial_number
self.command = command
self.serial = Serial()
self.serial.baudrate = baudrate

def __del__(self):
"""Destructor"""
try:
self.close()
except:
pass # errors on shutdown

def __str__(self):
return "SRN: {} Command: {}".format(self.serial_number, self.command)

def get_device_name(self, serial_number):
"""
Get full device name/path from serial number

Parameters
----------
serial_number: string
The usb-serial's serial number

Returns
-------
string
Full device name/path, e.g. /dev/ttyUSBx
(on *.nix system or COMx on Windows)

Raises
------
IOError
When not found device with this serial number

"""
serial_numbers = []
for pinfo in comports():
if pinfo.serial_number == serial_number:
return pinfo.device
# save list of serial numbers for user reference
serial_numbers.append(pinfo.serial_number.encode('utf-8'))
raise IOError('Could not find device with provided serial number.'))

def connect(self):
"""
Connect to serial port
"""
# open serial port
try:
device = self.get_device_name(self.serial_number)
self.serial.port = device
# Set RTS line to low logic level
self.serial.rts = False
self.serial.open()
except Exception as ex:
self.handle_serial_error(ex)

def send_command(self):
"""
Send command to serial port
"""
if self.serial.is_open:
try:
# Unicode strings must be encoded
data = self.command.encode('utf-8')
self.serial.write(data)
except Exception as ex:
self.handle_serial_error(ex)
else:
raise IOError('Try to send data when the connection is closed')

def receive_command(self):
"""
Read back data
"""
numLines=0
if self.serial.is_open:
while True:
response = self.serial.readline()
numLines = numLines + 1
return(response)
if(self.numLines >= 10):
break

def close(self):
"""
Close all resources
"""
self.serial.close()

def handle_serial_error(self, error=None):
"""
Serial port error
"""
# terminate connection
self.close()
# forward exception
if isinstance(error, Exception):
raise error # pylint: disable-msg=E0702

The following is a script that utilizes the object and send a command "info" to a connected device with serial number "R200000"


from serialpy import ComConnection
from threading import Thread
from time import sleep

# Initialize an instance
com = ComConnection(serial_number='R200000', command='info', baudrate=9600)

def read():
while True:
data = com.receive_command()
if data:
print data
sleep(1)

def write():

# If you pass an invalid serial-number, an exception will be thrown
# This will inform that no device with provided serial number is found.
# The exception message also lists the COM device's serial numbers
# currently plugged on your system
com.connect()

# If you try to call send_command() before call connect()
# an exception will be raised to inform you are trying to
# send command in a closed connection
com.send_command()
sleep(1)

if __name__ == '__main__':
t = Thread(target=read, args=())
# Make sure this thread will be killed when main program exits
t.setDaemon(True)
t.start()
write()