2014-11-15 120 views
2

我正在做一個測試項目,使用Raspberry Pi和Arduino盾(Alamode)。 我已經開始了基本的嘗試,準備好pySerial鏈接的細節,只是爲了看到我得到正確的輸出之前,我繼續下一步,從Arduino項目中讀取串行數據(pySerial)

不幸的是,它沒有像我一樣順利跳躍。

該項目編譯剛剛罰款的Arduino和當看着串行監視器我可以看到輸出正常,然後 - 只要我啓動pySerial腳本我開始缺少字符,數字和停止的腳本(聲稱串行鏈接不響應)。

串行鏈接進行得很好,我已經多次確認它,串行監視器不斷向我顯示實時數據。

但由於某些原因,似乎python腳本不能與串行輸出「時間」或「同步」,所以它會隨機剪切字母和字符。

我已經嘗試改變延遲(給它更多的時間)或改變波特率,它似乎沒有幫助,我離放棄和檢查替代解決方案又近了一步。

這是基本的Arduino代碼

// Example testing sketch for various DHT humidity/temperature sensors 
// Written by ladyada, public domain 

#include "DHT.h" 

#define DHTPIN 2  // what pin we're connected to 

// Uncomment whatever type you're using! 
#define DHTTYPE DHT11 // DHT 11 
//#define DHTTYPE DHT22 // DHT 22 (AM2302) 
//#define DHTTYPE DHT21 // DHT 21 (AM2301) 

// Connect pin 1 (on the left) of the sensor to +5V 
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1 
// to 3.3V instead of 5V! 
// Connect pin 2 of the sensor to whatever your DHTPIN is 
// Connect pin 4 (on the right) of the sensor to GROUND 
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor 

// Initialize DHT sensor for normal 16mhz Arduino 
DHT dht(DHTPIN, DHTTYPE); 
// NOTE: For working with a faster chip, like an Arduino Due or Teensy, you 
// might need to increase the threshold for cycle counts considered a 1 or 0. 
// You can do this by passing a 3rd parameter for this threshold. It's a bit 
// of fiddling to find the right value, but in general the faster the CPU the 
// higher the value. The default for a 16mhz AVR is a value of 6. For an 
// Arduino Due that runs at 84mhz a value of 30 works. 
// Example to initialize DHT sensor for Arduino Due: 
//DHT dht(DHTPIN, DHTTYPE, 30); 

void setup() { 
    Serial.begin(115200); 
    Serial.println("DHTxx test!"); 

    dht.begin(); 
} 

void loop() { 
    // Wait a few seconds between measurements. 
    delay(2000); 

    // Reading temperature or humidity takes about 250 milliseconds! 
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) 
    float h = dht.readHumidity(); 
    // Read temperature as Celsius 
    float t = dht.readTemperature(); 
    // Read temperature as Fahrenheit 

    // Check if any reads failed and exit early (to try again). 
    if (isnan(h) || isnan(t)) { 
    Serial.println("Failed to read from DHT sensor!"); 
    return; 
    } 

    Serial.print("Humidity: "); 
    Serial.print(h); 
    Serial.print(" "); 
    Serial.print("Temperature: "); 
    Serial.print(t); 
    Serial.println(" "); 
} 

這是PySerial

from time import sleep 
import serial 
ser = serial.Serial('/dev/ttyS0',115200) 
counter=32 
while True: 
    ser.write(str(chr(counter))) 
    print ser.readline(16384) 
    sleep(.1) 

回答

5

通常情況下,基本的腳本我讀像這樣

#!/usr/bin/python 
# -*- coding: utf-8 -*- 

import serial 
import sys 
import time 

port = "/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_AE01J6GZ-if00-port0" 

baudrate = 115200 

if len(sys.argv) == 3: 
    ser = serial.Serial(sys.argv[1], sys.argv[2]) 
else: 
    print "# Please specify a port and a baudrate" 
    print "# using hard coded defaults " + port + " " + str(baudrate) 
    ser = serial.Serial(port, baudrate) 

# enforce a reset before we really start 
#ser.setDTR(1) 
#time.sleep(0.25) 
#ser.setDTR(0) 

while 1: 
    sys.stdout.write(ser.readline()) 
    sys.stdout.flush() 

明顯的差異串行接口是你在循環中使用sleep(.1)。如果Arduino發送速度很快,這可能會使輸入緩衝區溢出。用我的方法,我從來沒有任何問題。

+0

「謝謝你」早該過期了..這個解決方案解決了我的問題,這確實是溢出問題。 – Amit