Arduino与Raspberry Pi的串行接口传输2位数据

时间:2017-05-11 22:31:08

标签: python c++ arduino raspberry-pi3 pyserial

/*
  String to Integer conversion

 Reads a serial input string until it sees a newline, then converts
 the string to a number if the characters are digits.

 The circuit:
 No external components needed.

 created 29 Nov 2010
 by Tom Igoe

 This example code is in the public domain.
 */
#include <LiquidCrystal.h>
LiquidCrystal lcd(23, 22,13,12,11,10);
String inString = "";    // string to hold input

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(19200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
    lcd.begin(20, 4);

  // send an intro:
  //println("\n\nString toInt():");
  //Serial.println();
}

void loop() {
  // Read serial input:
  while (Serial.available() > 0) {
    lcd.setCursor(0,0); 
    lcd.print("welcome");
    int inChar = Serial.read();
    if (isDigit(inChar)) {
      // convert the incoming byte to a char
      // and add it to the string:
      inString += (char)inChar;
    }
    // if you get a newline, print the string,
    // then the string's value:
    if (inChar == '\n') {
      int data = inString.toInt() ;
      lcd.setCursor(0,1); 
      lcd.print("speed: ");
      lcd.println(data);
      if(data > 50){
        lcd.setCursor(0,2); 
        lcd.print("Over speed");
      }
      else{
        lcd.setCursor(0,2); 
        lcd.print("Under speed limit ");
      }

     // Serial.print("Value:");
      //Serial.println(inString.toInt());
      //Serial.print("String: ");
      //Serial.println(inString);
      // clear the string for new input:
      inString = "";
    }
  }
}

Python code for serial interfacing

这个代码在使用串行监视器和Arduino时有效,但是当使用Raspberry Pi运行接口Arduino时,只有#34; welcome&#34; LCD上没有数据(我通过Raspberry Pi传输的数据)(2位数字),可能是由于Raspberry Pi上的Python程序或任何其他原因造成的。

0 个答案:

没有答案