借花献佛---转贴一篇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.
You type the parts after >>>
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
Note that the second argument here (9600) is the baud rate and should match whatever you put in your Arduino sketch.
/dev/ttyACM0 is the name for the USB interface to the Uno, at least itwas for my Uno. The way to discover the port name is to run thefollowing command in the terminal without the Uno plugged in.
ls /dev/tty*
Then plug in your Arduio and run the command again. If there is a new name, then this is the name of your port.
Now lets start a loop listening for messages from the Arduino.
while 1 :
ser.readline()
You will need two hit enter twice after you type the second line. Messages should now start to appear!
You can see in the Blue writing where the Arduino is talking to the Pi.Then some error trace as you press ctrl-C to interrupt the messagescoming from the Arduino.
When you type
ser.write('5')
you should see the LED on the Arduino flash 5 times.
There are many possibilities here, we could put a motor shield or LCD shield onto the Arduino and control it from your Pi.
Watch out for my book 'rogramming the Raspberry Pi' pub. McGraw-Hill, out towards the end of 2012.