与arduino的字符串连接
String concatenation with arduino
我在 Arduino Fio 设备上有一个非常短的示例程序 运行。该程序正在发送串行数据。连接的 Xbee 路由器设备正在接收此数据并将其发送到连接到我的笔记本电脑的 Xbee 协调器设备。该程序还从串口读取数据。我可以发送 1
或 0
来打开或关闭 Fio 设备的 LED。
通过从我笔记本上的终端发送 1
或 0
来打开或关闭 LED 效果很好。
但是当我尝试读取 Fio 设备发送的数据时,我得到了这个:
his direction works
is direction works
s direction works
direction works
direction works
irection works
rection works
ection works
ction works
...等等。
但我需要一个字符串 ("This direction works " + counter++;
),如您在以下代码示例中所见。
这是简短的 Arduino 草图:
int incomingByte = 0; // for incoming serial data
int counter = 0;
void setup()
{
Serial.begin(57600);
pinMode(13,OUTPUT);
// blink twice at startup
digitalWrite(13, LOW);
delay(1000);
digitalWrite(13, HIGH); // first blink
delay(50);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH); // second blink
delay(50);
digitalWrite(13, LOW);
}
void loop()
{
// send data only when you receive data:
if (Serial.available() > 0)
{
// read the incoming byte:
incomingByte = Serial.read();
if(incomingByte == '0')
{
digitalWrite(13, LOW);
}
else if(incomingByte == '1')
{
digitalWrite(13, HIGH);
}
// say what you got:
Serial.print("Fio received: ");
Serial.write(incomingByte); // Arduino 1.0 compatibility
Serial.write(10); // send a line feed/new line, ascii 10
}
else
{
String sendData = "This direction works " + counter++;
Serial.println(sendData);
delay(1500);
}
}
我做错了什么?为什么我没有得到:
This direction works 0
This direction works 1
This direction works 2
This direction works 3
...等等?
正如@Elric 和@Olaf 在评论中提到的那样,不可能像我发布的代码那样做到这一点。
再次阅读文档后,我发现了一个 "explanation":
Caution: You should be careful about concatenating multiple variable
types on the same line, as you may get unexpected results. For
example:
int sensorValue = analogRead(A0); String stringOne = "Sensor
value: "; String stringThree = stringOne + sensorValue;
Serial.println(stringThree);
results in "Sensor Value: 402" or whatever the analogRead() result is,
but
int sensorValue = analogRead(A0); String stringThree = "Sensor
value: " + sensorValue; Serial.println(stringThree);
gives unpredictable results because stringThree never got an initial
value before you started concatenating different data types.
首先,您必须初始化 String 变量以将两个连接的字符串分配给它。
我在 Arduino Fio 设备上有一个非常短的示例程序 运行。该程序正在发送串行数据。连接的 Xbee 路由器设备正在接收此数据并将其发送到连接到我的笔记本电脑的 Xbee 协调器设备。该程序还从串口读取数据。我可以发送 1
或 0
来打开或关闭 Fio 设备的 LED。
通过从我笔记本上的终端发送 1
或 0
来打开或关闭 LED 效果很好。
但是当我尝试读取 Fio 设备发送的数据时,我得到了这个:
his direction works
is direction works
s direction works
direction works
direction works
irection works
rection works
ection works
ction works
...等等。
但我需要一个字符串 ("This direction works " + counter++;
),如您在以下代码示例中所见。
这是简短的 Arduino 草图:
int incomingByte = 0; // for incoming serial data
int counter = 0;
void setup()
{
Serial.begin(57600);
pinMode(13,OUTPUT);
// blink twice at startup
digitalWrite(13, LOW);
delay(1000);
digitalWrite(13, HIGH); // first blink
delay(50);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH); // second blink
delay(50);
digitalWrite(13, LOW);
}
void loop()
{
// send data only when you receive data:
if (Serial.available() > 0)
{
// read the incoming byte:
incomingByte = Serial.read();
if(incomingByte == '0')
{
digitalWrite(13, LOW);
}
else if(incomingByte == '1')
{
digitalWrite(13, HIGH);
}
// say what you got:
Serial.print("Fio received: ");
Serial.write(incomingByte); // Arduino 1.0 compatibility
Serial.write(10); // send a line feed/new line, ascii 10
}
else
{
String sendData = "This direction works " + counter++;
Serial.println(sendData);
delay(1500);
}
}
我做错了什么?为什么我没有得到:
This direction works 0
This direction works 1
This direction works 2
This direction works 3
...等等?
正如@Elric 和@Olaf 在评论中提到的那样,不可能像我发布的代码那样做到这一点。
再次阅读文档后,我发现了一个 "explanation":
Caution: You should be careful about concatenating multiple variable types on the same line, as you may get unexpected results. For example:
int sensorValue = analogRead(A0); String stringOne = "Sensor value: "; String stringThree = stringOne + sensorValue;
Serial.println(stringThree);results in "Sensor Value: 402" or whatever the analogRead() result is, but
int sensorValue = analogRead(A0); String stringThree = "Sensor value: " + sensorValue; Serial.println(stringThree);
gives unpredictable results because stringThree never got an initial value before you started concatenating different data types.
首先,您必须初始化 String 变量以将两个连接的字符串分配给它。