[Twisted-Python] Twisted serialport dataReceived() provides fragmented data

Phil Mayers p.mayers at imperial.ac.uk
Thu Aug 29 11:26:36 MDT 2013


On 29/08/13 17:15, Sivakumar Balasubramanian wrote:

> Dear Phil,
> I replaced thye dataReceived function by stringReceived, and ran the code.
> But when I do this, the program does not even enter the stringReceived function.

That implies your protocol isn't an int16string-prefixed one. I assumed 
it was, since you'd inherited from that class.

Obviously you will need to pick a base class that matches the actual 
structure of your protocol.

>
> I also tried a modified version of the programs above with LineReceived
> protcol, and here too I have the same problem of fragmented data from the
> dataReceived function.

Well, *again* you shouldn't be overriding dataReceived if you're 
inheriting from LineReceiver. You should be overriding lineReceived.

To be clear: dataReceived gets raw data. It can, and *will*, get 
incomplete messages, because it doesn't know anything about messages.

If you have a protocol with a message structure, you either need to:

  1. Inherit from an existing base-class which handles the message 
framing, and implement the "<msg>Received" handler specific to that 
protocol.

  2. Or, if there's no base-class matching your message framing, write 
your own - this will entail buffering the data, then extracting messages.

See here:

http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#Whyisprotocol.dataReceivedcalledwithonlypartofthedataIcalledtransport.writewith



More information about the Twisted-Python mailing list