Re: Servos, Ardunios, and JMRI #arduino

Cliff Anderson


From your original post:

Item 3. It doesn't matter which script is loaded first; the second script will stop the first script's turnouts.

This symptom is consistent with the use of the same name for what should be considered two variables.

In the text you supplied in your most recent post, it is apparent that the variables named below are overwritten by the most recently executed script file:

  • portname
  • portID
  • port
  • baudrate
  • inputStream
  • outputStream
  • class Datatransfer

and perhaps others. Only the constant for baudrate is benign in this case.

With the possible exception of the class name, all of the other variable names are shared by all of the instances of the most recent definition of the Datatransfer class that are created in the lines:

# The following will set up 5 listeners for Turnouts PT1 though PT6 (by username)
for x in range(1,6) :

which appear at the bottom of both script texts. In effect, these instances will each use the most recently set values for each of the above identified variable names.

If these are correct copies of your scripts, It is my understanding that these lines set up duplicate copies of Turnouts PT1 through PT6.

If my understanding of these lines is correct, either script would only connect with what JMRI labels as turnouts with systemName choices PT1 though PT6. Did you in fact receive state change information concerning either set of your yard turnouts but using the same JMRI systemNames?

The original structure of the class definition in does not lend itself to easy modification to multiple input-output connections to separate Arduino devices.

My preference would be to define a more robust class with a separate groupings of instances for each Arduino. But without the hardware resources to test with at least three Arduino devices it is not prudent for me to make such an attempt. In no case would I recommend executing two script files that define copies of the same class, but that is moot here.

In your case, the quick and dirty method to simply modify all of the variable names, other than the baudrate, as you have already done for a few variables might prove to be sufficient. It would not be surprising if you chose this path.

It would be prudent to review the tutorial at and in particular the section 9.2 "Python Scopes and Namespaces."

If indeed your plan is to consider adding numerous other controls with more variations of the script, this process could easily get both tedious and difficult to maintain.

If you wish to respond off list and are willing to put up with perhaps many iterative attempts, I could provide some help.

By the way, if you had made a subfolder in with a unique identifier referencing either this problem or your name, and supplied that link in your post, I believe you would have gotten more responses from a wider source of knowledge. Only by accident did I discover your pasted text marked as "Show quoted text" which usually means previous postings in the same thread.

Cliff in Baja SoCal

Join to automatically receive all group messages.