python - In pyfirmata, how to set up a handler for string messages? -


how set handler receive messages in pyfirmata arduino uno?

i have following python code:

from logic.moduleclass import module events.eventdispatcherclass import event pyfirmata import arduino, util import pyfirmata  class comm(module):     """     handles communication between python , arduino     attachto: ""     """      name = "communicator"      def __init__(self, port):         super(comm,self).__init__(comm.name)         self.board = arduino(port)         # start iterator thread serial buffer doesn't overflow         = util.iterator(self.board)         it.start()          self.board.add_cmd_handler(pyfirmata.pyfirmata.string_data, self._messagehandler)      def _messagehandler(self, *args, **kwargs):         print args      def update(self):         super(comm,self).update()      def writedata(self,data):         #print data         self.board.send_sysex(pyfirmata.pyfirmata.string_data,data)      def dispose(self):         super(comm,self).dispose()         try:             self.board.exit()         except attributeerror:             print "exit() raised attributeerror unexpectedly!"+self.tostring() 

on arduino, i'm sending string this:

firmata.sendstring("test"); 

i added _messagehandler(self, *args, **kwargs) , i'm getting collection of assume character codes. i'm new python , i'm not sure how can original string send arduino

i have found solution:

to convert string send arduino pyfirmata use following code in python:

def _messagehandler(self, *args, **kwargs):     print util.two_byte_iter_to_str(args) 

that should return string you're expecting. ftw!


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -