[ale] Anyone familiar with Python?

Alex Carver agcarver+ale at acarver.net
Tue Mar 8 16:12:33 EST 2016


Ok, so this should be fun since I've spent the past hour and a half on
Google and haven't really gotten anywhere.

I'm writing some code to control data collection modules.  Physically,
all the modules are hanging off an RS485 bus with a single wire to the
computer.  Each of the modules has a set of very core commands
(specifically for things like fetching the module name, firmware number,
and base configuration) and then it has module specific commands that
vary depending on the type of module (read voltage inputs, set relays on
outputs, etc.)  All of the modules share the same protocol for comms.

So my original thought was to create a class which contained the very
bottom stuff, serial I/O/ that would handle the basic serial functions
and the protocol.  Let's call this data_collectors.

I then thought to make a subclass of data_collectors that contains the
more specific functions mapped to the protocol commands (e.g. I'd have a
get_voltage() function for the specific voltage input module and a
set_relay() function for a relay output module).  The modules each have
their own address.

So in code form (some pseudo code in here, too for brevity):

<file is my_data_collector_file>
class data_collector(object):

	serial_port = None

	def open_port(self, tty=''):
		self.serial_port = serial.open(tty)

	def write_port(...):

	def read_port(...):

	def get_module_name(...):
		write_port()
		read_port()

class voltage_input(data_collector):
	__init__(self, module_address):
		self.module_address  = module_address

	get_voltage(self, channel):
		<stuff here including write_port() read_port()>

class relay_output(data_collector):
	__init__(self, module_address):
		self.module_address = module_address


	set_relay(self, channel, value):
		<stuff here including value validation, write_port(), read_port()


The goal was to be able to import this block of code and then do the
following (somehow):

import my_data_collector_file as my_dcf

dcf.open_port(tty=...0)  #somehow do this, not sure how
gauges = dcf.voltage_input(module_address=1)
valves = dcf.relay_output(module_address=2)


Right now, with the same structure I can execute the gauges = and valves
= lines but they get independent copies of the base class.  I want to
share the base class (so the port opens and stays open) among all of the
subclasses but have the subclasses behave individually (since they each
have their own set of commands that don't overlap).

I haven't figured out if there's a way to accomplish this even if
there's a third call (as above with the dcf.open_port) to just get
everything started.


More information about the Ale mailing list