Arduino 与 Raspberry Pi 的串行接口传输 2 位数据
Serial Interfacing of Arduino with Raspberry Pi to transmit 2 digit data
/*
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 = "";
}
}
}
此代码在与 Arduino 一起使用串行监视器时有效,但是当 运行 将 Arduino 与 Raspberry Pi 接口时,LCD 上只有 "welcome" 消息,而不是我的数据(2 位数字)通过 Raspberry Pi 转移,可能是由于 Raspberry Pi 上的 Python 程序或任何其他原因。
您正在检查“\n”但从未发送过。所以你永远不会触发
if (inChar == '\n')
/*
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 = "";
}
}
}
此代码在与 Arduino 一起使用串行监视器时有效,但是当 运行 将 Arduino 与 Raspberry Pi 接口时,LCD 上只有 "welcome" 消息,而不是我的数据(2 位数字)通过 Raspberry Pi 转移,可能是由于 Raspberry Pi 上的 Python 程序或任何其他原因。
您正在检查“\n”但从未发送过。所以你永远不会触发
if (inChar == '\n')