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:
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:
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:
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.