【发布时间】:2015-06-12 14:53:10
【问题描述】:
我希望能够在我的 Arduino 上解析以逗号分隔的字符串。 我发送到我的设备文本字符串,如
Somebody,Natalia Le Rose,,
由C#生成
//creating string that will be send to arduino
string message = SongTitleNotification + "," + SongArtistNotification + "," + FacebookNotification + "," + GmailNotification;
//sending message
SerialPort port = new SerialPort("COM4", 9600);
port.Open();
port.Write(message);
port.Close();
它会收到我的草图
#include <LiquidCrystal.h>
#include <string.h>
#include <stdio.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(20, 4);
Serial.begin(9600);
}
void loop()
{
while (Serial.available() > 0) {
lcd.clear();
delay(100);
// look for the next valid integer in the incoming serial stream:
char title = Serial.parseInt();
// do it again:
char artist = Serial.parseInt();
// do it again:
char fb = Serial.parseInt();
char gmail = Serial.parseInt();
lcd.print(title);
lcd.setCursor(0,1);
lcd.print(artist);
lcd.setCursor(0,2);
lcd.print(fb);
lcd.setCursor(0,3);
lcd.print(gmail);
}
delay(3500);
}
但液晶显示器不显示变量。 我该怎么办? :)
【问题讨论】:
-
首先检查您是否收到任何东西。
-
是的,我是。我曾经尝试过 lcd.print(Serial.read);它工作正常
-
你得到的字符串实际上是用逗号分隔的数字?
-
为什么它被标记为
C?它看起来一点也不像C。 -
还有一个问题是
title、artist、fb和gmail被声明为单个字符值,而不是字符串。不是 Arduino 程序员,但我认为您正在寻找类似 @987654331@ 的东西,其中String是 Arduino 开发环境提供的一种类型。见this answer。