views:

32

answers:

2

Hi,

I'm using twisted.internet.serialport to have my program be continuously connected to a device on a serial port.

Unfortunately my serial port is just a usb device, which means it can be disconnected or reset by the OS at any time (port 2 disabled by hub (EMI?), re-enabling... ). I see that pyserial has support for this for a few weeks and raises a SerialException.

What I would love to do is try to reconnect to the serial port that just disappeared every few seconds.

So, is there any way how I can tell twisted to notify me about a disconnect? Or should I go ahead and write a threaded wrapper for pyserial?

Thanks

A: 

http://twistedmatrix.com/trac/ticket/3690 may be related. The ticket appears blocked on proper Windows support. I'm not sure if this kind of disconnect event will trigger Twisted's internal connection lost detection code, but I would expect it to (even without a recent version of pyserial). You could probably try out the branch linked from that ticket pretty easily to see if it does what you want, at least. And if so, perhaps you could help get the ticket actually resolved (the 10.2 release is coming up pretty soon, wink wink nudge nudge).

Jean-Paul Calderone
I've read through this earlier and it seemed kind of unrelated, but I just gave their branch a try and it really resolved my problem.
Chris
So, how do I get this merged into the trunk? ;)
Chris
The issue mentioned in http://twistedmatrix.com/trac/ticket/3690#comment:16 (Cannot open serial port: could not open port: (5, 'CreateFile', 'Access is denied.')) seems to be the main blocking point for the ticket. If someone figures out the cause of that and how to fix it, that would probably get the branch pretty close to being merged. Alternatively, the Windows support could be split off into a separate ticket, letting the support be merged for POSIX immediately (which helps you if you don't care about Windows support).
Jean-Paul Calderone
A: 

It seems the only relevant change in branched version is a call to connectionLost() in the protocol.

Until it's fixed in the trunk I use a:

class fixedSerialPort(SerialPort):
    def connectionLost(self, reason):
        SerialPort.connectionLost(self, reason)
        self.protocol.connectionLost(reason)

I tested it with Twisted 10.1 (on ubuntu) and 8.1 (on my trusty debian). Both works fine. No idea about other OSs though.

Chris