Arduino到串行+python到firebase的UTF-8解码错误
Arduino to serial +python to firebase UTF-8 decode error
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。您确定您已经配置了正确的速度吗?
- HEX值到wchar_t字符(UTF-8)的转换
- 无法解码base64+deflate数据
- 带有Protobuf序列化的C++Hazelcast:字符串不是UTF-8格式的
- 转换特殊字符(UTF-8)
- 正在解码MSVC 32位版本的程序集(作业).没有手术做什么
- 使用已使用 java 编码的 openssl 解码数据
- 如何使用 OpenCV 解码在两个 UWP 应用之间发送的图像字节?
- 如何在CPP的给定目录中列出UTF编码的文件名?
- 如何使用 C++将 ISO-2022-KR 编码转换为 UTF-8 编码?
- 在C++中使用 UTF-8 字符串和字符
- 错误:(-210:不支持的格式或格式组合)功能'create'中的硬件视频解码器不支持视频源
- 从原始字节解码协议缓冲区(以 C++为单位)
- C Unicode UTF-8解码字符的问题
- 如何修复"UnicodeDecodeError:"utf-8"编解码器无法解码字节时使用Python C扩展?
- 如何在没有wchar_t的情况下在 c++ 中解码/编码 UTF-8 字符
- 如何解码UTF-8
- 跨平台UTF-8字符文件数据编码/解码
- 解码base64时GtkTextView中出现UTF-8错误
- C++ & Boost:编码/解码 UTF-8
- Arduino到串行+python到firebase的UTF-8解码错误