<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&#39;t used those in Python, so I can&#39;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">&lt;<a href="mailto:agcarver+ale@acarver.net" target="_blank">agcarver+ale@acarver.net</a>&gt;</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>
&gt; My first thought is not so much Python-related as design related.  If you<br>
&gt; want to have only one instance of the wire, you need to make it so that the<br>
&gt; base class functions are either bound to the class, or to an object passed<br>
&gt; in, so you can use the same &#39;wire&#39; object for each sensor class<br>
&gt;<br>
&gt; (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 &quot;automagic&quot; method where the serial port<br>
was already in place without having to pass it as an argument but that<br>
doesn&#39;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>
&gt;<br>
&gt;<br>
&gt; On Tue, Mar 8, 2016 at 4:12 PM, Alex Carver &lt;<a href="mailto:agcarver%2Bale@acarver.net">agcarver+ale@acarver.net</a>&gt;<br>
&gt; wrote:<br>
&gt;<br>
&gt;&gt; Ok, so this should be fun since I&#39;ve spent the past hour and a half on<br>
&gt;&gt; Google and haven&#39;t really gotten anywhere.<br>
&gt;&gt;<br>
&gt;&gt; I&#39;m writing some code to control data collection modules.  Physically,<br>
&gt;&gt; all the modules are hanging off an RS485 bus with a single wire to the<br>
&gt;&gt; computer.  Each of the modules has a set of very core commands<br>
&gt;&gt; (specifically for things like fetching the module name, firmware number,<br>
&gt;&gt; and base configuration) and then it has module specific commands that<br>
&gt;&gt; vary depending on the type of module (read voltage inputs, set relays on<br>
&gt;&gt; outputs, etc.)  All of the modules share the same protocol for comms.<br>
&gt;&gt;<br>
&gt;&gt; So my original thought was to create a class which contained the very<br>
&gt;&gt; bottom stuff, serial I/O/ that would handle the basic serial functions<br>
&gt;&gt; and the protocol.  Let&#39;s call this data_collectors.<br>
&gt;&gt;<br>
&gt;&gt; I then thought to make a subclass of data_collectors that contains the<br>
&gt;&gt; more specific functions mapped to the protocol commands (e.g. I&#39;d have a<br>
&gt;&gt; get_voltage() function for the specific voltage input module and a<br>
&gt;&gt; set_relay() function for a relay output module).  The modules each have<br>
&gt;&gt; their own address.<br>
&gt;&gt;<br>
&gt;&gt; So in code form (some pseudo code in here, too for brevity):<br>
&gt;&gt;<br>
&gt;&gt; &lt;file is my_data_collector_file&gt;<br>
&gt;&gt; class data_collector(object):<br>
&gt;&gt;<br>
&gt;&gt;         serial_port = None<br>
&gt;&gt;<br>
&gt;&gt;         def open_port(self, tty=&#39;&#39;):<br>
&gt;&gt;                 self.serial_port = serial.open(tty)<br>
&gt;&gt;<br>
&gt;&gt;         def write_port(...):<br>
&gt;&gt;<br>
&gt;&gt;         def read_port(...):<br>
&gt;&gt;<br>
&gt;&gt;         def get_module_name(...):<br>
&gt;&gt;                 write_port()<br>
&gt;&gt;                 read_port()<br>
&gt;&gt;<br>
&gt;&gt; class voltage_input(data_collector):<br>
&gt;&gt;         __init__(self, module_address):<br>
&gt;&gt;                 self.module_address  = module_address<br>
&gt;&gt;<br>
&gt;&gt;         get_voltage(self, channel):<br>
&gt;&gt;                 &lt;stuff here including write_port() read_port()&gt;<br>
&gt;&gt;<br>
&gt;&gt; class relay_output(data_collector):<br>
&gt;&gt;         __init__(self, module_address):<br>
&gt;&gt;                 self.module_address = module_address<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;         set_relay(self, channel, value):<br>
&gt;&gt;                 &lt;stuff here including value validation, write_port(),<br>
&gt;&gt; read_port()<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; The goal was to be able to import this block of code and then do the<br>
&gt;&gt; following (somehow):<br>
&gt;&gt;<br>
&gt;&gt; import my_data_collector_file as my_dcf<br>
&gt;&gt;<br>
&gt;&gt; dcf.open_port(tty=...0)  #somehow do this, not sure how<br>
&gt;&gt; gauges = dcf.voltage_input(module_address=1)<br>
&gt;&gt; valves = dcf.relay_output(module_address=2)<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; Right now, with the same structure I can execute the gauges = and valves<br>
&gt;&gt; = lines but they get independent copies of the base class.  I want to<br>
&gt;&gt; share the base class (so the port opens and stays open) among all of the<br>
&gt;&gt; subclasses but have the subclasses behave individually (since they each<br>
&gt;&gt; have their own set of commands that don&#39;t overlap).<br>
&gt;&gt;<br>
&gt;&gt; I haven&#39;t figured out if there&#39;s a way to accomplish this even if<br>
&gt;&gt; there&#39;s a third call (as above with the dcf.open_port) to just get<br>
&gt;&gt; everything started.<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; Ale mailing list<br>
&gt;&gt; <a href="mailto:Ale@ale.org">Ale@ale.org</a><br>
&gt;&gt; <a href="http://mail.ale.org/mailman/listinfo/ale" rel="noreferrer" target="_blank">http://mail.ale.org/mailman/listinfo/ale</a><br>
&gt;&gt; See JOBS, ANNOUNCE and SCHOOLS lists at<br>
&gt;&gt; <a href="http://mail.ale.org/mailman/listinfo" rel="noreferrer" target="_blank">http://mail.ale.org/mailman/listinfo</a><br>
&gt;&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; Ale mailing list<br>
&gt; <a href="mailto:Ale@ale.org">Ale@ale.org</a><br>
&gt; <a href="http://mail.ale.org/mailman/listinfo/ale" rel="noreferrer" target="_blank">http://mail.ale.org/mailman/listinfo/ale</a><br>
&gt; See JOBS, ANNOUNCE and SCHOOLS lists at<br>
&gt; <a href="http://mail.ale.org/mailman/listinfo" rel="noreferrer" target="_blank">http://mail.ale.org/mailman/listinfo</a><br>
&gt;<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>