Note that there are some explanatory texts on larger screens.

plurals
  1. POTwisted serialport dataReceived() provides fragmented data
    text
    copied!<p>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:</p> <pre><code>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() </code></pre> <p>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.</p> <p>Is there something that I doing wrong in my code?</p> <hr> <p><strong>Additional modification/correction</strong></p> <p>When I replace dataReceived() function in the above code by stringReceived(), the program never enters this function.</p> <p>I also tried to above program with the LineReceiver protocol, as the following:</p> <pre><code>from twisted.internet import protocol, reactor from twisted.internet.serialport import SerialPort from twisted.protocols import basic class DeviceBluetooth(basic.LineReceiver): def connectionMade(self): print 'Connection made!' self.sendLine('[01] help') 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.sendLine('[02] stim on') elif int(ch) == 2: self.sendLine('[03] stim off') elif int(ch) == 3: self.sendLine('[04] stim ?') elif int(ch) == 4: self.sendLine('[05] help') else: reactor.stop() SerialPort(DeviceBluetooth(), 'COM20', reactor, baudrate=115200) reactor.run() </code></pre> <p>I have the same problem, as before, with fragmented data from the dataReceived function.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload