2

我正在尝试从 gsm 调制解调器中收到的短信回复。

问题是,我不知道如何从这一系列字符串中提取电话号码,以便获取发件人的号码。

我必须提取发件人的号码“09279127639”。

+CMGL: 1,"REC READ","09279127639","","13/04/05,08:24:36+22"

注意:发件人的号码是不同的。这只是一个例子。

提取后,我希望它将其值返回到此代码中,以便我可以自动回复他。

void setup()
{ pinMode(8, OUTPUT);
digitalWrite(8, HIGH);
delay(3000);
digitalWrite(8, LOW);

Serial.begin(9600);
Serial1.begin(9600); //Baud rate of the GSM/GPRS Module 
Serial.print("\r");
Serial1.print("\r");
delay(1000); 

Serial1.print("AT\r");
Serial.print("AT\r");
delay(10000); 
Serial1.print("AT\r");
Serial.print("AT\r");
delay(1000); 

Serial1.print("AT+CMGF=1\r");
Serial.print("AT+CMGF=1\r");
delay(1000);

Serial1.print("AT+CMGS=\"number here\"\r");
Serial.print("AT+CMGS=\"number here\"\r"); //Number to which you want to send the sms
delay(1000);

Serial1.print("This is a message from a GSM modem.\r");
Serial.print("This is a message from a GSM modem.\r"); //The text of the message to be sent
delay(1000);

Serial1.write(0x1A);
Serial.write(0x1A);
delay(1000); 
}
void loop()
{
}

您的回复将不胜感激。谢谢你。

4

4 回答 4

1

我最后一个答案的续集,添加

using System.Text.RegularExpressions;

正则表达式可帮助您将它们切成所需的部分。创建一个类名 ShortMessage.cs 并从这样的变量创建属性

 #region Private Variables
    private string index;
    private string status;
    private string sender;
    private string alphabet;
    private string sent;
    private string message;
    #endregion

    #region Public Properties
    public string Index
    {
        get { return index; }
        set { index = value; }
    }
    public string Status
    {
        get { return status; }
        set { status = value; }
    }
    public string Sender
    {
        get { return sender; }
        set { sender = value; }
    }
    public string Alphabet
    {
        get { return alphabet; }
        set { alphabet = value; }
    }
    public string Sent
    {
        get { return sent; }
        set { sent = value; }
    }
    public string Message
    {
        get { return message; }
        set { message = value; }
    }
    #endregion

使用此方法,您始终可以清晰地获取发件人的电话号码和消息。

于 2013-10-17T09:53:50.437 回答
1

为什么不使用拆分方法

String s = string.Empty;
        s = "REC READ","09279127639","","13/04/05,08:24:36+22";
        string[] str = s.Split(',');
        foreach (string mem in str)
        {
            MessageBox.Show(mem);
        }
于 2013-10-15T10:15:55.050 回答
0
Pattern pattern = Pattern.compile("\\d{11}");// matching 11 digit number, or [0-9]
Matcher matcher = pattern.matcher(input);

干杯

于 2013-10-15T02:14:45.520 回答
-1

为什么不阅读它并使用正则表达式来区分它

Regex r = new Regex(@"\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\r\n(.+)\r\n");
                Match m = r.Match(input);
                while (m.Success)
                {
                    ShortMessage msg = new ShortMessage();
                    //msg.Index = int.Parse(m.Groups[1].Value);
                    msg.Index = m.Groups[1].Value;
                    msg.Status = m.Groups[2].Value;
                    msg.Sender = m.Groups[3].Value;
                    msg.Alphabet = m.Groups[4].Value;
                    msg.Sent = m.Groups[5].Value;
                    msg.Message = m.Groups[6].Value;
                    #region PROCESS DATA
                    string[] p_str = { msg.Index + " (Index) ", msg.Status + " (Status) ", msg.Sender + " (Sender) ", msg.Alphabet + " (Alphabet) ", msg.Sent + " (Sent Date) ", msg.Message + " (Message) " };
                    ProcessData(p_str);
                    #endregion
                    m = m.NextMatch();
                }

ShortMessage 应该是具有这些结构的类。它对我有用

于 2013-10-16T08:45:33.547 回答