與 Python 的序列通訊
如果你將 Arduino 連線到計算機或 Raspberry Pi,並且想要將資料從 Arduino 傳送到 PC,你可以執行以下操作:
Arduino
void setup() {
// Opens serial port, sets data rate to 9600 bps:
Serial.begin(9600);
}
void loop() {
// Sends a line over serial:
Serial.println("Hello, Python!");
delay(1000);
}
Python:
import serial
ser = serial.Serial('/dev/ttyACM0', 9600) # Start serial communication
while True:
data = ser.readline() # Wait for line from Arduino and read it
print("Received: '{}'".format(data)) # Print the line to the console