gpt4 book ai didi

python - Arduino转串口+python转firebase UTF-8解码错误

转载 作者:行者123 更新时间:2023-11-28 02:26:11 25 4
gpt4 key购买 nike

Stack Overflow 大神们求大神指教...

我的错误:^[[A???????????????????????????????????????????? ?q????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ??????????????????????????????????????????C??????? ????????????????????????????,23:24:05,01/06/2015,我们的舰队

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?为什么会这样?

我的 Arduino 代码:

#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();
}

我的 Python 代码通过 https 发送数据。我还收到一条错误消息,指出连接不是 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。您确定您配置的速度正确吗?

关于python - Arduino转串口+python转firebase UTF-8解码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30586884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com