<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">Using a static or class method might give you the effect you want, but I haven't used those in Python, so I can't speak to their pitfalls<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Mar 8, 2016 at 5:05 PM, Alex Carver <span dir="ltr"><<a href="mailto:agcarver+ale@acarver.net" target="_blank">agcarver+ale@acarver.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 2016-03-08 13:24, Pete Hardie wrote:<br>
> My first thought is not so much Python-related as design related. If you<br>
> want to have only one instance of the wire, you need to make it so that the<br>
> base class functions are either bound to the class, or to an object passed<br>
> in, so you can use the same 'wire' object for each sensor class<br>
><br>
> (I am assuming that you are dealing with just a single one of these buses)<br>
<br>
</span>Yep, only one address. But passing it in made sense. I split off the<br>
serial into a top level class within the imported module and then turned<br>
it into a parameter for the other classes.<br>
<br>
I had hoped for a slightly more "automagic" method where the serial port<br>
was already in place without having to pass it as an argument but that<br>
doesn't appear to be possible. Fortunately I only have to pass it once<br>
when the class is instantiated.<br>
<div class="HOEnZb"><div class="h5"><br>
><br>
><br>
> On Tue, Mar 8, 2016 at 4:12 PM, Alex Carver <<a href="mailto:agcarver%2Bale@acarver.net">agcarver+ale@acarver.net</a>><br>
> wrote:<br>
><br>
>> Ok, so this should be fun since I've spent the past hour and a half on<br>
>> Google and haven't really gotten anywhere.<br>
>><br>
>> I'm writing some code to control data collection modules. Physically,<br>
>> all the modules are hanging off an RS485 bus with a single wire to the<br>
>> computer. Each of the modules has a set of very core commands<br>
>> (specifically for things like fetching the module name, firmware number,<br>
>> and base configuration) and then it has module specific commands that<br>
>> vary depending on the type of module (read voltage inputs, set relays on<br>
>> outputs, etc.) All of the modules share the same protocol for comms.<br>
>><br>
>> So my original thought was to create a class which contained the very<br>
>> bottom stuff, serial I/O/ that would handle the basic serial functions<br>
>> and the protocol. Let's call this data_collectors.<br>
>><br>
>> I then thought to make a subclass of data_collectors that contains the<br>
>> more specific functions mapped to the protocol commands (e.g. I'd have a<br>
>> get_voltage() function for the specific voltage input module and a<br>
>> set_relay() function for a relay output module). The modules each have<br>
>> their own address.<br>
>><br>
>> So in code form (some pseudo code in here, too for brevity):<br>
>><br>
>> <file is my_data_collector_file><br>
>> class data_collector(object):<br>
>><br>
>> serial_port = None<br>
>><br>
>> def open_port(self, tty=''):<br>
>> self.serial_port = serial.open(tty)<br>
>><br>
>> def write_port(...):<br>
>><br>
>> def read_port(...):<br>
>><br>
>> def get_module_name(...):<br>
>> write_port()<br>
>> read_port()<br>
>><br>
>> class voltage_input(data_collector):<br>
>> __init__(self, module_address):<br>
>> self.module_address = module_address<br>
>><br>
>> get_voltage(self, channel):<br>
>> <stuff here including write_port() read_port()><br>
>><br>
>> class relay_output(data_collector):<br>
>> __init__(self, module_address):<br>
>> self.module_address = module_address<br>
>><br>
>><br>
>> set_relay(self, channel, value):<br>
>> <stuff here including value validation, write_port(),<br>
>> read_port()<br>
>><br>
>><br>
>> The goal was to be able to import this block of code and then do the<br>
>> following (somehow):<br>
>><br>
>> import my_data_collector_file as my_dcf<br>
>><br>
>> dcf.open_port(tty=...0) #somehow do this, not sure how<br>
>> gauges = dcf.voltage_input(module_address=1)<br>
>> valves = dcf.relay_output(module_address=2)<br>
>><br>
>><br>
>> Right now, with the same structure I can execute the gauges = and valves<br>
>> = lines but they get independent copies of the base class. I want to<br>
>> share the base class (so the port opens and stays open) among all of the<br>
>> subclasses but have the subclasses behave individually (since they each<br>
>> have their own set of commands that don't overlap).<br>
>><br>
>> I haven't figured out if there's a way to accomplish this even if<br>
>> there's a third call (as above with the dcf.open_port) to just get<br>
>> everything started.<br>
>> _______________________________________________<br>
>> Ale mailing list<br>
>> <a href="mailto:Ale@ale.org">Ale@ale.org</a><br>
>> <a href="http://mail.ale.org/mailman/listinfo/ale" rel="noreferrer" target="_blank">http://mail.ale.org/mailman/listinfo/ale</a><br>
>> See JOBS, ANNOUNCE and SCHOOLS lists at<br>
>> <a href="http://mail.ale.org/mailman/listinfo" rel="noreferrer" target="_blank">http://mail.ale.org/mailman/listinfo</a><br>
>><br>
><br>
><br>
><br>
><br>
><br>
> _______________________________________________<br>
> Ale mailing list<br>
> <a href="mailto:Ale@ale.org">Ale@ale.org</a><br>
> <a href="http://mail.ale.org/mailman/listinfo/ale" rel="noreferrer" target="_blank">http://mail.ale.org/mailman/listinfo/ale</a><br>
> See JOBS, ANNOUNCE and SCHOOLS lists at<br>
> <a href="http://mail.ale.org/mailman/listinfo" rel="noreferrer" target="_blank">http://mail.ale.org/mailman/listinfo</a><br>
><br>
<br>
_______________________________________________<br>
Ale mailing list<br>
<a href="mailto:Ale@ale.org">Ale@ale.org</a><br>
<a href="http://mail.ale.org/mailman/listinfo/ale" rel="noreferrer" target="_blank">http://mail.ale.org/mailman/listinfo/ale</a><br>
See JOBS, ANNOUNCE and SCHOOLS lists at<br>
<a href="http://mail.ale.org/mailman/listinfo" rel="noreferrer" target="_blank">http://mail.ale.org/mailman/listinfo</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature">Pete Hardie<br>--------<br>Better Living Through Bitmaps</div>
</div>