【发布时间】:2014-02-21 19:35:05
【问题描述】:
我有一个通过 USB 与 Raspberry Pi 连接的 Arduino。 Python 程序通过 USB 发送一个值,然后 arduino 接收它并打开 LED。 Arduino 正在将模拟值从 A0 发送到 Raspberry 上的 Python 程序。但是 Python 有时会收到 024、24 或 4 而不是 1024。我怎样才能解决这个问题?这是我的代码:
http://www.bitsharr.com/Hsoxw7nG
Arduino 代码:
int led = 13;
char charIn;
int sensorPin = A0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
long previousMillis = 0;
long interval = 1000;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
Serial.begin(9600); //This initializes the USB as a serial port
}
void loop() {
if (Serial.available()) {
delay(5); // warten bis alle Daten da sind
while(Serial.available() > 0) {
charIn =(char) Serial.read();
if (charIn == '1') {
digitalWrite(led,HIGH);
delay(5000);
digitalWrite(led,LOW);
}
}
}
Python 代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from serial import Serial
ser = Serial('/dev/ttyUSB0', 9600)
x=ser.readline()
ser.write('1')
print(x)
def cleanup( str ):
result = ""
for c in str:
if( (c >= "0") and (c <= "9") ):
result += c
return result
print( cleanup(x))
#ser.write("1")
sensorValue = analogRead(sensorPin); //Reads the voltage of the resistor.
Serial.println(sensorValue); //Writes the voltage on the Serial port.
}
【问题讨论】:
标签: usb arduino communication