1

I am trying to get some data from an Arduino, and I cannot decode the data that is coming from it. I have searched for some information and I found these answers for example:

Introduction to Unicode

Unicode string to String in python

The Arduino is sending numbers (data) in a 8-bit codification (UTF-8). I tried a lot of different codes and the best decode that I have got is this:

The data that I have got from decoding

I am using SublimeText 2 to write my code, and this is what the console shows me when I use print. I need to decode the data, so I can use it later to draw a matplotlib figure.

This last code that I wrote gave me the output shown above:

class readData(QWidget):

  def __init__(self):
    super(readData, self).__init__()

    self.resize(300, 100)

    self.btn = QPushButton("Close", self)
    self.btn.setGeometry(150, 50, 100, 30)

    self.btn_2 = QPushButton("Search Data", self)
    self.btn_2.setGeometry(50, 50, 100, 30)

    self.btn.clicked.connect(self.close)
    self.btn_2.clicked.connect(self.searchData)

def searchData(self):
    arduinoData = serial.Serial('com7', 9600) #We open port com7

    while True:
        print "Searching for data"
        while(arduinoData.inWaiting() == 0): #We wait for the data
            print "There is no data"            

        print "Reading and converting data"
        arduinoString = str(arduinoData.readline())
        ardString = unicode(arduinoString, errors = "ignore")
        print "This is the data: "
        print type(arduinoString)
        print ""
        print arduinoString
        print type(ardString)

def close(self):
    #WE CLOSE THE WINDOW AND THE PORT

I open a simple QWidget to show two buttons: one to start searching for data and show it, and another to close the window and the port. This is the simple window:

enter image description here

How must I decode (or encode, I really don`t know now) to show the numbers that I need? What am I doing wrong? I hope you can help me.

Community
  • 1
  • 1
Pablo Flores
  • 667
  • 1
  • 13
  • 33

1 Answers1

1

A String is essentially a sequence of chars. Every char can be represented by one or more bytes. This mapping from a 'byte - (1 or more)' to a 'char' is the 'transformation format'. There are several conventions out there:

  • UTF-8
  • UTF-16
  • ASCII

When you receive some bytes from your Arduino, you need to tell Python what convention you follow. Here are some examples:

    # Receive data example
    rawData = arduino.readLine()
    myString = rawData.decode('utf-8')
    print(myString)

    # Transmit data example
    myString = "Hello world"
    rawData = myString.encode('utf-8')
    arduino.sendLine(rawData)

I hope this was helpful :-)

K.Mulier
  • 8,069
  • 15
  • 79
  • 141
  • I have just read about this. Thank you again. I'm having another problem now. Idk why it does not read anything now. It seems like it stops working and fails after `print "Reading and converting data"` – Pablo Flores Apr 19 '16 at 21:05
  • 1
    It seems like `arduinoData.readline()` is hanging. But it is difficult to say why. Maybe the arduino stops sending any data? – K.Mulier Apr 19 '16 at 21:08
  • 1
    I'm sorry Pablo, I wish I could help you out with this. But I haven't yet worked myself with Arduino serial link to Python. I do have experience with TCP/UDP communication with PIC microcontrollers (and reading the data on the PC with a Python program) – K.Mulier Apr 19 '16 at 21:12
  • No problem. Thank you a lot for all your help. – Pablo Flores Apr 19 '16 at 21:18
  • You're welcome. If the program keeps hanging everytime you call the `arduinoData.readline()` function, you can maybe start a new Stackoverflow Question about this topic? I'm sure there are lots of Arduino people out there willing to help you ;-) – K.Mulier Apr 19 '16 at 21:21
  • Hi Pablo. Did you find the solution to your issue? Kind greetings, Kristof – K.Mulier May 16 '16 at 15:43