【问题标题】:Parsing string separated by commas in C - Arduino在 C 中解析用逗号分隔的字符串 - Arduino
【发布时间】: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
  • 还有一个问题是titleartistfbgmail被声明为单个字符值,而不是字符串。不是 Arduino 程序员,但我认为您正在寻找类似 @​​987654331@ 的东西,其中 String 是 Arduino 开发环境提供的一种类型。见this answer

标签: c arduino lcd


【解决方案1】:

要使用分隔符解析字符串,请考虑使用 strtok。您应该创建一个您的 Serial 读入的缓冲区,然后解析它

char buffer[50];
uint8_t bufferReadIndex = 0;
while (Serial.available() > 0) {
    buffer[bufferReadIndex++] = Serial.read();
}
char* stringPtr;
stringPtr = strtok(&buffer, ",");
while (stringPtr != NULL)
{
    Serial.println(stringPtr);
    stringPtr = strtok(NULL, ",");
}

【讨论】:

    猜你喜欢
    • 2021-11-01
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    相关资源
    最近更新 更多