[Twisted-Python] client logic

Benjamin Schollnick bschollnick at gmail.com
Thu Oct 9 19:33:35 MDT 2014


I’m sorry if this is a duplicate?  I sent this earlier, but I don’t see it in the archives?

Folks,

I’m writing an client using twisted, and I’m basing it off the Poetry client from http://krondo.com/wp-content/uploads/2009/08/twisted-intro.html.
I am porting a previous pyserial based client, so I’m running into new territory here.

And it’s working fine, there are two “issues" that I am running into.

class WT_Protocol(Protocol):

   tag = ''

   def to_bytes(self, seq):
       """convert a sequence to a bytes type"""
       #
       #   from pyserial.util
       if isinstance(seq, bytes):
           return seq
       elif isinstance(seq, bytearray):
           return bytes(seq)
       elif isinstance(seq, memoryview):
           return seq.tobytes()
       else:
           b = bytearray()
           for item in seq:
               b.append(item)  # this one handles int and str for our emulation and ints for Python 3.x
           return bytes(b)

   def connectionMade(self):
       print "Reseting the network"
       reset = self.to_bytes(packets.network_reset() )
       self.transport.write ( reset )
       self.transport.write ( self.to_bytes(packets.Set_SiteCode(NodeID=255, SC1=0, SC2=0, SC3=0) ))
       self.transport.write ( self.to_bytes(packets.Set_Gain_Packet(NodeID=255, Gain=1) ))    # high gain
       self.transport.write ( self.to_bytes(packets.Set_Protocol_No_Nulls (NetworkID=0, ReceiverID=0, NodeID=1) ))    # high gain
       time.sleep (.5)

Now the client works right now, but I’m concerned that I can’t figure out how to immediately read for a reply packet from the server.

For example, when I send the Set_SiteCode packet, I may receive a reply.  

But how would that occur in the Twisted framework?  In my testing the dataReceived function is not being called, so I can’t trap it there, plus I’m not sure how I would figure that logic.

Can anyone suggest a method to do this?

The second question, as far as I can tell, isn’t very solvable.  I’d like to add a “Press Q to quit” type logic to the code.  But I don’t see a way to do this easily in twisted?  (platform unspecific, but it’s running Mac OS X / Unix right now).

		- Ben
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20141009/ceb99df5/attachment-0002.html>


More information about the Twisted-Python mailing list