¿ªÔÆÌåÓý

Locked Re: Script Closing Serial Ports Question


 

Hi Dave,
As requested, here is an example of how I close the serial port. Also note that the "return 0" also terminates the script:
if closing_test == 1 :
??? ??? ?print "Script Ends"
??? ??? ?self.inputStream.close()
??? ??? ?self.outputStream.close()
??? ??? ?self.port.close()
??? ??? ?return 0
I started with Bob Jacobsen's SerialPortDevice.py in the JMRI release as a learning tool and template. (Thanks Bob!)
The test above would be in the equivalent of the handle(self)

If you followed this thread before, I was also interested in using a second script to forcibly close the port too.
to accomplish this (again with a great suggestion from Bob!) you create a global variable which can be used to refer to the
open port. So again referring to SerialPortDevice.py by structure, you would have in your original script:
??? def __init__(self, portname) :
??? global extport
??? ...
??? extport = self.port = self.portID.open("JMRI", 50)
??? ...

And here is my ForcePortClose.py? script to close the specific port and terminate:

import jarray
import jmri
import purejavacomm

class SerialCloseM(jmri.jmrit.automat.AbstractAutomaton) :
??? # ctor starts up the serial port
??? def __init__(self, portname) :
??????? global extport
??????? self.portID = purejavacomm.CommPortIdentifier.getPortIdentifier(portname)
??????? extport.close()
??????? return
??? # init() is the place for your initialization
??? def init(self) :
??????? return
??? # handle() is called repeatedly until it returns false.
??? def handle(self) :
????????? return 0
??? def write(self, data) :
??????? return
??? def flush(self) :
??????? self.outputStream.flush()
??????? return
# create one of these; provide the name of the serial port
a = SerialCloseM("COM13")
# set the thread name, so easy to cancel if needed
a.setName("ForcePortClose script")
# start running
a.start();


Also, FYI, what I found is that using the second script to close the port while the first script is still running
will force a portnotopen exception and terminate the first script as well. This, for me, was a desirable side effect!

An educational note: When Bob suggested using a global variable I was, at first, surprised that a global variable would
exist beyond the scope of the script. But one should realize that the global scope is JMRI not just one script. That is,
any script runs inside the global JMRI scope, and my own thinking was myopically focused on a single script.
Score another point for the beauty and the complexity that is JMRI.

I will be publishing an app soon that will incorporate these features and add another that you might find useful too.
More to come...
'Hope this helps. Have fun!? :-)
Best regards,
Geoff Bunza
scalemodelanimation.com

Join [email protected] to automatically receive all group messages.