python arduino usb通信

时间:2015-09-03 14:43:10

标签: python-2.7 arduino communication

我目前正在尝试将数据从python移动到我得到的arduino nano

但是我在arduino方面的控制台上看不到任何东西 P.S 当我试图将数据从arduino移动到python我工作得很好

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 =

1 个答案:

答案 0 :(得分:0)

Arduino正在使用print,Python正在使用readLINE .. Readline会读取,直到找到&#39; \ n&#39; arduino永远不会发送&#39; \ n&#39; <#39;

将草图中的\0替换为\n,它应该有效。

使用串行监视器进行测试并查看会发生什么情况总是有用的,如果这不起作用,请尝试使用串行监视器发送一些数据,并让我们知道结果。

相关问题