0

I am struggling to read in data from an Arduino and save this data as a csv file I could meddle with in Python later. Right now my code reads.

import serial serial_port = '/dev/ttyUSB0' baud_rate = 9600 file_path = "output.csv" ser = serial.Serial(serial_port,baud_rate) done = False data = [] while done == False: raw_bytes = ser.readline() decoded_bytes = float(raw_bytes.decode("utf-8")) data.append(decoded_bytes) if (len(data) > 10) : done = True import numpy as np np.savetxt(file_path, data, delimiter = ',', fmt='%s')

but I'm running into the error

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf0 in position 1: invalid continuation byte

I want to decode into UTF-8 don't I? What is going wrong? I have checked the Serial Monitor on the Arduino IDE and I am getting correct outputs there. Thanks in advance.

Andrew Hardy
  • 232
  • 1
  • 8
  • 1
    May be this answer helps you: https://stackoverflow.com/questions/5552555/unicodedecodeerror-invalid-continuation-byte – ElDuderino Mar 22 '19 at 03:15
  • Which coding do you use on the Arduino side? Eventually the same you used for coding your sketch. That's probably the same SerialMonitor uses. My Windows still prefers ANSI (CP-1252 8 bit) . However F0 is a strange ð there. – datafiddler Mar 22 '19 at 11:05
  • @datafiddler how do I check this? I'm running a chipkit uC32 with the Arduino IDE on an Ubuntu machine – Andrew Hardy Mar 22 '19 at 14:15

1 Answers1

0

If there's no other way to find out which coding your Arduino IDE uses, you can check/guess the coding on the Arduino side by returning the codes for characters in question via SerialMonitor

 void loop () {
    int c = Serial.read();
    if ( c == -1 ) return;  // nothing available
    Serial.println (c, HEX);  // return the character code in hex notation
 }  

However the characters you use to convert text into a float number should be plain ASCII, so your

float(raw_bytes.decode("utf-8"))

would probably fail anyway.

datafiddler
  • 1,755
  • 3
  • 17
  • 30