[ale] Anyone familiar with Python?
Billy
billybrawner at gmail.com
Tue Mar 8 21:35:22 EST 2016
Hey Ales,
I'm no expert in Python by any means, so if anyone else here sees a
problem with my approach, please speak up. But it seems to me that what
you would want to be doing is creating a single class, and then running
the different functions within it.
In code:
<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(self):
pass
def read_port(self):
pass
def get_module_name(self):
self.write_port()
self.read_port()
def voltage_input(self, channel):
# This would perform whatever tasks you planned on running for
# get_voltage. You could even define whatever functions you
# needed in here, and then call them and return their values
def get_voltage(channel):
# Do what you need to here
return self.write_port(channel)
def other_function(channel)
# Do what you need to here
return self.read_port(channel)
# Used like this, you get back what you return from your
# functions in an array
return [get_voltage(channel), other_function(channel)]
# Any variables you may need can be set from here as well, but
# it's only necessary to do so if you want to set default values
my_variable = 0
def relay_output(self, channel):
# Other stuff here
So then when you go to import your module, you would do something like
this:
import my_data_collector_file as my_dcf
dc = my_dcf.data_collector()
The problem with this bit of code:
gauges = dcf.voltage_input(module_address=1)
valves = dcf.relay_output(module_address=2)
is that you're creating two different
objects, so any variables stored within one of those objects aren't shared
with the other.
By using a single object with those functions within it, you would use
that object to perform the actions you needed, like so:
dc.voltage_input(1)
The variable you set in the class can be accessed here as well as
modified, so you can do things like
dc.voltage_input(dc.my_variable) # my_variable was set to 0 in the class
dc.my_variable = 2
dc.voltage_input(dc.my_variable) # my_variable is now 2
dc.relay_output(dc.my_variable) # my_variable is still 2
If you did something like having voltage_input return values, you can
access those by storing them in a variable either in or outside of the
scope of the object
my_array = dc.voltage_input(123)
dc.new_array = dc.voltage_input
Like I said, I'm no expert, so if anyone sees any errors with what I've
written, please correct them.
I hope this helps though!
On Tue, Mar 08, 2016 at 02:31:36PM -0800, Alex Carver wrote:
> No, there's no module for these devices. The company supports their own
> programming environment (very similar to LabView) and provides the
> protocol reference so that the devices can be used elsewhere but no code
> is offered for anything outside their program.
>
> This is Python 2.7. I can't use 3.0 or above because I'm having to
> integrate this hardware into an existing code base that communicates
> with even more specialized hardware. I know there's a few things in the
> existing code base that won't migrate easily and would require a lot of
> work to rewrite.
>
> On 2016-03-08 14:23, Jay Lozier wrote:
> > Just curious, but is it possible that there might be Python module
> > available to do what you want?
> >
> > Also, which flavor of Python are you using?
> >
> > Jay
> >
> >
> > On 03/08/2016 04:12 PM, Alex Carver wrote:
> >> 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.
> >> _______________________________________________
> >> Ale mailing list
> >> Ale at ale.org
> >> http://mail.ale.org/mailman/listinfo/ale
> >> See JOBS, ANNOUNCE and SCHOOLS lists at
> >> http://mail.ale.org/mailman/listinfo
> >
> > _______________________________________________
> > Ale mailing list
> > Ale at ale.org
> > http://mail.ale.org/mailman/listinfo/ale
> > See JOBS, ANNOUNCE and SCHOOLS lists at
> > http://mail.ale.org/mailman/listinfo
> >
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo
More information about the Ale
mailing list