2015-06-02 21 views
0

堆棧溢出神我問你的憐憫......Arduino的串行+蟒蛇火力UTF-8解碼錯誤

我的錯誤:^ [[A ????????????? ???????????????????????????? q ????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ???????????????????C?????????????????????????????? ???????,23:24:05,01/06/2015,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?這是爲什麼發生?

我的Arduino代碼:

通過HTTPS發送數據
#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,而是http。我試圖使用點等更新我的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) 

我缺乏這方面的知識如何修改我的程序。

回答

2

在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.您確定配置了正確的速度嗎?