Arduino到串行+python到firebase的UTF-8解码错误

Arduino to serial +python to firebase UTF-8 decode error

本文关键字:UTF-8 解码 错误 +python Arduino firebase      更新时间:2023-10-16

Stack Overflow gods我请求你的怜悯…

我的错误:^ [[ ????????????????????????????????????????? 问 ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? c ?????????????????????????????????????, 01/06/2015 23:24:05 ourfleet

Traceback (most recent call last):
  File "ourfleet.py", line 33, in <module>
    result = requests.post(firebase_url + '/' + gps_location + '/gps_c.json'+ auth_token, data=json.dumps(data))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte

我正在通过arduino uno串行传递GPS数据。这是UTF-8吗?为什么会发生这种情况?

My Arduino code:

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
/*
   This sample sketch demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

void setup()
{
  Serial.begin(115200);
  ss.begin(GPSBaud);
}
void loop()
{
  // This sketch displays information every time a new sentence is correctly encoded.
  while (ss.available() > 0)
    if (gps.encode(ss.read()))
      displayInfo();
  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    while(true);
  }
}
void displayInfo()
{
    Serial.print(gps.location.lat(), 6);
    Serial.print(F(","));
    Serial.print(gps.location.lng(), 6);
    Serial.println();
}

通过https发送数据的Python代码。我也得到一个错误,说连接不是https,相反,它是http。我已经尝试使用pip等来更新我的Python库,但无济于事。我用的是07 MacBook pro)

import serial
import time
import requests
import json
firebase_url = 'https://ourfleet.firebaseio.com'
auth_token = '0sBNZjz4uQvLteDoCZTDUD3RNOT852aKyqahGzl4'
#Connect to Serial Port for communication
ser = serial.Serial('/dev/tty.wchusbserial410', 9600, timeout=0)
#Setup a loop to send GPS values at fixed intervals
#in seconds
fixed_interval = 10
while 1:
    try:
        #GPS value obtained from Arduino + Ublox
        gps_c = ser.readline()
        #current time and date
        time_hhmmss = time.strftime('%H:%M:%S')
        date_mmddyyyy = time.strftime('%d/%m/%Y')
        #current location name
        gps_location = 'ourfleet'
        print gps_c + ',' + time_hhmmss + ',' + date_mmddyyyy + ',' + gps_location
        #insert record
        data = {'date':date_mmddyyyy,'time':time_hhmmss,'location':gps_c}
        result = requests.post(firebase_url + '/' + gps_location + '/gps_c.json'+ auth_token, data=json.dumps(data))
        #insert record
        print 'Record inserted. Result Code = ' + str(result.status_code) + ',' + result.text
        time.sleep(fixed_interval)
    except IOError:
        print('Error! Something went wrong.')
        time.sleep(fixed_interval)

我不知道如何在这方面修改我的程序。

在您的Python代码中,从串行端口读取的数据的第一个字符是'xff',在这种情况下json.dumps()将抛出如下异常:

>>> json.dumps({'gps_c': 'xffblah'})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/json/__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib64/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib64/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte

从串行端口读取的数据看起来损坏了-它应该是14字节长(6 lat + 6 lon + ',' + 'n'),然而,打印的数据显示它远远长于此。

我怀疑波特率没有正确匹配。在C/c++代码中,您将一个端口设置为155200,另一个端口(SoftwareSerial)设置为9600。Python代码期望9600。您确定您已经配置了正确的速度吗?