【发布时间】:2015-09-03 14:43:09
【问题描述】:
我正在尝试将数据从 Python 移动到 Arduino nano,但我在 Arduino 端的控制台上看不到任何内容,但是当 我尝试将数据从 Arduino 移动到 python,效果很好。 按照下面的代码:
arduino 代码
void setup() {
Serial.begin(9600);
}
void loop() {
if(Serial.available() > 0) {
char data = Serial.read();
char str[2];
str[0] = data;
str[1] = '\0'
Serial.print(str);
}
}
python 代码
import serial, time
arduino = serial.Serial('COM3', 9600, timeout=.1)
time.sleep(1) # give the connection a second to settle
arduino.write("Hello from Python!")
while True:
data = arduino.readline()
if data:
print data.rstrip('\n') # strip out the new lines
【问题讨论】:
-
你有分隔符(
'\0') 并且需要关注它。Serial.println和Serial.print没有相同的输出。 -
可以给出更多解释,“focus it”是什么意思
标签: python-2.7 arduino communication