[Twisted-Python] Twisted serialport dataReceived() provides fragmented data
![](https://secure.gravatar.com/avatar/5765a5dbea821e9a78638d8a55613a78.jpg?s=120&d=mm&r=g)
I am tyring to implement a python program, using Twisted, to communicate witha bluetooth device. The following is a sample code of what I have implemented: from twisted.internet import protocol, reactor from twisted.internet.serialport import SerialPort from twisted.protocols import basic class DeviceBluetooth(basic.Int16StringReceiver): def connectionMade(self): print 'Connection made!' self.sendString('[01] help\n') def dataReceived(self, data): print"Response: {0}".format(data) print "-----" print "choose message to send: " print "1. Stim on" print "2. Stim off" print "3. Stim status" print "4. Help" # user input ch = input("Choose command :: ") if int(ch) == 1: self.sendString('[02] stim on\n') elif int(ch) == 2: self.sendString('[03] stim off\n') elif int(ch) == 3: self.sendString('[04] stim ?\n') elif int(ch) == 4: self.sendString('[05] help\n') else: reactor.stop() SerialPort(DeviceBluetooth(), 'COM20', reactor, baudrate=115200) reactor.run() When I run the program, sometimes I get a response and other times I do not receive anything. And most of the times long responses are fragmented appear as part of the next message. I have through the hyperterminal to make sure that I get the appropriate response from by bluetooth device. So, the problem has to be with my code. Is there something that I doing wrong in my code?
![](https://secure.gravatar.com/avatar/426d6dbf6554a9b3fca1fd04e6b75f38.jpg?s=120&d=mm&r=g)
On 29/08/13 16:38, Sivakumar Balasubramanian wrote:
Is there something that I doing wrong in my code?
Yes. You are sub-classing Int16StringReceiver, but then breaking things by overriding dataReceived. You should be implementing stringReceived, and you'll get complete strings. dataReceived does not get "messages" - a higher layer needs to buffer and reassemble them, which Int16StringReceiver does, and passes the messages to stringReceived.
![](https://secure.gravatar.com/avatar/5765a5dbea821e9a78638d8a55613a78.jpg?s=120&d=mm&r=g)
Phil Mayers <p.mayers <at> imperial.ac.uk> writes:
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. 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. Thanks. Siva.
![](https://secure.gravatar.com/avatar/426d6dbf6554a9b3fca1fd04e6b75f38.jpg?s=120&d=mm&r=g)
On 29/08/13 17:15, Sivakumar Balasubramanian wrote:
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.
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.da...
![](https://secure.gravatar.com/avatar/426d6dbf6554a9b3fca1fd04e6b75f38.jpg?s=120&d=mm&r=g)
On 29/08/13 16:38, Sivakumar Balasubramanian wrote:
Is there something that I doing wrong in my code?
Yes. You are sub-classing Int16StringReceiver, but then breaking things by overriding dataReceived. You should be implementing stringReceived, and you'll get complete strings. dataReceived does not get "messages" - a higher layer needs to buffer and reassemble them, which Int16StringReceiver does, and passes the messages to stringReceived.
![](https://secure.gravatar.com/avatar/5765a5dbea821e9a78638d8a55613a78.jpg?s=120&d=mm&r=g)
Phil Mayers <p.mayers <at> imperial.ac.uk> writes:
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. 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. Thanks. Siva.
![](https://secure.gravatar.com/avatar/426d6dbf6554a9b3fca1fd04e6b75f38.jpg?s=120&d=mm&r=g)
On 29/08/13 17:15, Sivakumar Balasubramanian wrote:
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.
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.da...
participants (2)
-
Phil Mayers
-
Sivakumar Balasubramanian