【问题标题】:Passing the value of char into string to be used for comparing将 char 的值传递给用于比较的字符串
【发布时间】:2015-03-10 22:40:41
【问题描述】:

我正在做一个项目,我将使用 Arduino (ATMEGA328) 和 GSM (SIM900)。

收到的消息存储在inchar 中。我能够在串行监视器上打印inchar 的值,但我想在inchar 中找到一个特定的词。

有什么方法可以将该字符传递给字符串,这样我就可以使用字符串的子字符串函数了吗?

请帮忙。这是我的代码的 sn-p:

#include <SoftwareSerial.h> 
#include <string.h>

char inchar; // Will hold the incoming character from the GSM shield

SoftwareSerial SIM900(2, 3);
//String inchar1;

void setup()
{
  Serial.begin(9600);

  // wake up the GSM shield
  SIM900power(); 
  SIM900.begin(9600);
  delay(20000);
  SIM900.print("AT+CMGF=1\r");
  delay(100);
  SIM900.print("AT+CNMI=2,2,0,0,0\r"); 
  delay(100);
  Serial.println("Ready....");
}

void SIM900power()
{
  digitalWrite(9, HIGH);
  delay(1000);
  digitalWrite(9, LOW);
  delay(7000);
}

 void loop() 
{

if(SIM900.available() >0)
  {
    inchar=SIM900.read();
    Serial.print(inchar);


}
}

这是串行监视器中的输出:

+CMT: "+1234567890","","15/01/13,10:51:29+32"
amessage deleted!

我只想比较+1234567890 并丢弃其他的。有什么办法吗?

【问题讨论】:

  • 也许这会对你有所帮助stackoverflow.com/questions/4728699/…
  • @Jonny Henly inchar 包含串行监视器中输出的所有值。如果我声明字符串 s(1, inchar);在 SIM900.read(); 之后,什么也没有发生。
  • @Jonny Henly 类字符串没有名为 push_back 的成员。它说。我正在使用 Arduino IDE。
  • @Jonny Henly 是的。我喜欢java。但是当我尝试小写时,IDE 不知道,因此重定向我使用字符串而不是字符串。我正在使用 Arduino IDE。
  • @Vonne 哦,我的错,我把 IDE 和 API 搞混了。

标签: c++ arduino gsm


【解决方案1】:

您可能希望使用String 类而不是数组。见这里:http://arduino.cc/en/Reference/StringObject

yourString.index(",") 将允许您定位“,”的第一次出现。

然后你就可以使用yourString.substring(start, stop) 来剪切你的字符串了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    相关资源
    最近更新 更多