with some rather simple additions, serial could be used together with asyncore (a python builtin method for asynchronously working with socket style connections) and asynchat (which builds on top of it).
asyncore expects a socket-like interfaces; when just trying to plug them together, i found that four functions are required: setblocking, getpeername, recv and send.
all of them can be implemented trivially by aliasing or short lambdas:
serial.Serial.setblocking = lambda self, blocking: self.setTimeout(None if blocking else 0)
serial.Serial.getpeername = lambda self: self.port
serial.Serial.recv = serial.Serial.read
serial.Serial.send = serial.Serial.write
the getpeername seems to be relevant with network sockets where the peers can vanish; some other functions around the corners might still be needed (for disappearing devices and such), but for what i've needed so far, these four lines are all that's needed to integrate with python's own asynchronity framework.
Log in to post a comment.