python - Twisted serialport dataReceived() provides fragmented data -


i tyring implement python program, using twisted, communicate witha bluetooth device. following sample code of have implemented:

from twisted.internet import protocol, reactor twisted.internet.serialport import serialport 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 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 run program, response , other times not receive anything. , of times long responses fragmented appear part of next message. have through hyperterminal make sure appropriate response bluetooth device. so, problem has code.

is there doing wrong in code?


additional modification/correction

when replace datareceived() function in above code stringreceived(), program never enters function.

i tried above program linereceiver protocol, following:

from twisted.internet import protocol, reactor twisted.internet.serialport import serialport 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 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() 

i have same problem, before, fragmented data datareceived function.

your protocol subclasses int16stringreceiver implements message framing using 2 byte (16 bit) length prefixes. however, overrides datareceived method implements framing. disables framing , delivers whatever bytes happen read connection - in whatever size happen read.

when subclass int16stringreceiver, you're meant override stringreceived instead.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -