// If you know the size of the String you're expecting, you could use a char[]
// instead.
String incomingString;
void setup() {
// Initialize serial communication. This is the baud rate the Arduino
// discusses over.
Serial.begin(9600);
// The incoming String built up one byte at a time.
incomingString = ""
}
void loop() {
// Check if there's incoming serial data.
if (Serial.available() > 0) {
// Read a byte from the serial buffer.
char incomingByte = (char)Serial.read();
incomingString += incomingByte
// Checks for null termination of the string.
if (incomingByte == '\0') {
// ...do something with String...
incomingString = ""
}
}
}
要发送串行数据 --- 并打印 Arduino 打印的数据 --- 您可以使用 Arduino IDE 中的串行监视器。
Mitch's links should point you in the right direction.
A common way of sending and receiving strings from the host computer to the Arduino and back is using the Arduino's Serial library. The Serial library reads and writes a byte at a time over the connection to the computer.
The below code forms a String by appending chars received over the Serial connection:
// If you know the size of the String you're expecting, you could use a char[]
// instead.
String incomingString;
void setup() {
// Initialize serial communication. This is the baud rate the Arduino
// discusses over.
Serial.begin(9600);
// The incoming String built up one byte at a time.
incomingString = ""
}
void loop() {
// Check if there's incoming serial data.
if (Serial.available() > 0) {
// Read a byte from the serial buffer.
char incomingByte = (char)Serial.read();
incomingString += incomingByte
// Checks for null termination of the string.
if (incomingByte == '\0') {
// ...do something with String...
incomingString = ""
}
}
}
To send the serial data --- and to print out the data the Arduino prints --- you can use the Serial Monitor in the Arduino IDE.
发布评论
评论(1)
米奇的链接应该会为您指明正确的方向。
从主机向 Arduino 发送和接收字符串并返回的常见方法是使用 Arduino 的串行库。串行库通过与计算机的连接一次读取和写入一个字节。
下面的代码通过附加通过串行连接接收到的字符来形成一个字符串:
要发送串行数据 --- 并打印 Arduino 打印的数据 --- 您可以使用 Arduino IDE 中的串行监视器。
Mitch's links should point you in the right direction.
A common way of sending and receiving strings from the host computer to the Arduino and back is using the Arduino's Serial library. The Serial library reads and writes a byte at a time over the connection to the computer.
The below code forms a String by appending chars received over the Serial connection:
To send the serial data --- and to print out the data the Arduino prints --- you can use the Serial Monitor in the Arduino IDE.