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

Sivakumar Balasubramanian siva82kb at gmail.com
Thu Aug 29 09:38:17 MDT 2013


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?




More information about the Twisted-Python mailing list