Skip to content Skip to sidebar Skip to footer

Twisted Serialport Datareceived() Provides Fragmented Data

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 i

Solution 1:

Your protocol subclasses Int16StringReceiver which implements message framing using two byte (16 bit) length prefixes. However, it overrides dataReceived which is the method that implements that framing. This disables the framing and just delivers whatever bytes happen to be read from the connection - in whatever size they happen to be read.

When you subclass Int16StringReceiver, you're meant to override stringReceived instead.

Solution 2:

For most of my bluetooth work, I've used 8-bit integers, so I would recommend using Int8StringReceiver. The LineReceiver protocol waits for an endline sequence which defaults to '\r\n' (and the bluetooth radios I use return '\r') so a mismatch on endline sequence would prevent the code from ever entering.

Have you tried to use a non-Twisted library for debugging? I highly recommend Twisted especially for production environments, but PySerial is a great way to poll serial data. (easy_install pyserial should do the trick.) Try this code:

importserials= serial.Serial('COM20', 115200, timeout=0)
data = s.read(1024)
print repr(data)

Make sure to use timeout=0 since that will make your read non-blocking. This will allow you to examine exactly what kind of data is being outputted by the bluetooth radio.

Finally, depending on what kind of bluetooth radio you are using, Windows might decide to move COM20 around, especially if you are using a USB-connected radio.

Post a Comment for "Twisted Serialport Datareceived() Provides Fragmented Data"