|
发表于 2012-10-26 08:15:10
|
显示全部楼层
借花献佛---转贴一篇Raspberry Pi and Arduino()
就不翻译了,很简单的英文
TheRaspberry Pi is creatingquite a storm of interest. I have just got mine and one of the firstthings that I wanted to try was to get it talking to an Arduino over USBusing Python.
.. and you know what? It proved to be a lot easier than I expected. Thisis mainly because, after all, despite its diminutive price tag, the Piis just a Linux box. I got communication working both ways, with theArduino sending 'Hello Pi' to the Pi and at the same time, testing for adigit coming in. When it receives a digit, it flashes the number oftimes indicated by the digit.
Arduino
Let's start with the Arduino end. I used an Arduino Uno and Arduinosoftware version 1.0. I haven't tried an older board, but I suspect theFTDI generation Arduinos before the Uno may have trouble with USB.
Here is the sketch - paste it into a new Arduino IDE window and load it up onto your Arduino using your regular computer.
const int ledPin = 13;
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
Serial.println("Hello Pi");
if (Serial.available())
{
flash(Serial.read() - '0');
}
delay(1000);
}
void flash(int n)
{
for (int i = 0; i < n; i++)
{
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
}
Raspberry Pi
There is a Python library for serial communications called pySerial (http://pyserial.sourceforge.net/) which has history with Arduino. So, I stood on the shoulders of giants and adapted the instructions found http://arduino.cc/playground/Interfacing/Python
Step 1. If you are not reading this page on your Pi, then switch now, so you can copy and paste.
Step 2. Browse to http://sourceforge.net/projects/pyserial/files/ and download http://sourceforge.net/projects/pyserial/files/latest/download?source=files pyserial-2.5.tar.gz (106.3 kB) and save it somewhere convenient. I saved it to the 'other' folder on the Desktop.
Step 3. This is a gziped tar file. Which needs unzipping and untaring.To unzip it open a Terminal, which you will find from the 'start menu'under 'accessories'. Now paste the following commands into it.
cd /home/pi/Desktop/other
gunzip pyserial-2.5.tar.gz
tar - xvf pyserial-2.5.tar
Step 4. Install pySerial, by typing these lines in your terminal window:
cd pyserial-2.5
sudo python setup.py install
Step 5. Run Python 2. You will find this from the menu under Programming - Use Python 2 not 3.
Thats it! Now we just need to write some Python to access the Serial port. So type the commands shown in the transcript below.
|
|