I am currently having a problem with the pySerial module in Python. My problem relates to connecting and disconnecting to a device. I can successfully connect to my device and communicate with it for as long as I want to, and disconnect from it whenever I desire. However, I am unable to reconnect to the device once the connection has been severed.
Here is the wrapper class that my program uses to interface with the serial port:
import serial, tkMessageBoxclass Controller:
""" Wrapper class for managing the serial connection with the MS-2000. """def __init__(self, settings):self.ser = Noneself.settings = settingsdef connect(self):""" Connect or disconnect to MS-2000. Return connection status."""try:if self.ser == None:self.ser = serial.Serial(self.settings['PORT'],self.settings['BAUDRATE'])print "Successfully connected to port %r." % self.ser.portreturn Trueelse:if self.ser.isOpen():self.ser.close()print "Disconnected."return Falseelse:self.ser.open()print "Connected."return Trueexcept serial.SerialException, e:return Falsedef isConnected(self):'''Is the computer connected with the MS-2000?'''try:return self.ser.isOpen()except:return Falsedef write(self, command):""" Sends command to MS-2000, appending a carraige return. """try:self.ser.write(command + '\r')except Exception, e:tkMessageBox.showerror('Serial connection error','Error sending message "%s" to MS-2000:\n%s' %(command, e))def read(self, chars):""" Reads specified number of characters from the serial port. """return self.ser.read(chars)
Does anybody know the reason why this problem exists and what I could try to do to fix it?