[Twisted-Python] Twisted Allen-Bradley PLC Protocol
I've successfully gotten twisted to talk to Allen Bradley PLC 5's using Ron Gage's "ABEL" library as a template. Needless to say, the twisted way is cleaner and easier to maintain (in my opinion at least). However, there's a slight twist to the code and I'm trying to think if there's a better way, and the protocol is similar to so many other things (SSL comes to mind). To communicate with AB PLC's, first you initiate a TCP/IP connection. This is text book twisted. Second, you initiate communication using a "link layer" protocol. Once that is established, THEN communication progresses using yet another protocol layer ("PCCC" or "DF-1" or "CSP" depending on what document you are reading). Right now, I simply wrote my "connectionMade" function set self.attached=0 and then initiate a "send connect command" function. The "received connection" function sets self.attached=1. All other commands simply to a reactor.callLater(1,self.func) and float around in the reactor buffer until self.attach==1 returns with good news. It seems to me that what we have here is really a protocol stack, similar in nature to the "LineReceiver" type protocol or perhaps it should be set up as an adapter (component architecture). Then in this case, the packet/protocol gets passed up through the layers and the "link layer" occupies the correct status of being a lower level layer. Or else to support say serial communication on an RS-232 or RS-485 network later, it could be done as a component arrangement. So, does this make any sense? What is the "design pattern" in this case in twisted?
Paul Campbell wrote:
So, does this make any sense? What is the "design pattern" in this case in twisted?
It depends: Twisted really ought to provide some layer-factoring code in the core, but it doesn't, so to some extent you're on your own. However, if there is no official "design pattern", there are certain trends which have been repeating themselves. Twisted's twisted.application.strports and twisted.spread.pb, as well as Divmod's mantissa.q2q, seem to all be taking the approach of: build one, high-level API that allows you to do a complete end-to-end connection conveniently with one line of code, and provide at least some basis for abstracting the components of that line so that they can be plugged in. PB's method is "login", strports has 'service' and 'listen', and Q2Q has service.connectQ2Q.
participants (2)
-
Glyph Lefkowitz
-
Paul Campbell