如何从AT命令解析PDU字符串? [英] How to Parse PDU String from AT Commands?
本文介绍了如何从AT命令解析PDU字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,您能否分享一下如何获取特定字符串的代码?
这是整个字符串。
AT + CMGL = 4\r\r\\\
+ CMGL:1,2,...,48\r\\\
079136190800101011000BA19084537777F70008AA2200540065007300740020004D0065007300730061006700650020005000440055002E\r\\\
+ CMGL:2,1,,34\r\\\
0791361908005043040C9136493875777700005101519103652311D4F29C0E6A96E7F3F0B90C8212AB2E \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ b'预郎= 文本> 079136190800101011000BA19084537777F70008AA2200540065007300740020004D0065007300730061006700650020005000440055002E
或
<预郎= 文本> 0791361908005043040C9136493875777700005101519103652311D4F29C0E6A96E7F3F0B90C8212AB2E
这是我用过但不起作用的代码。
public ShortMessageCollection ParseMessagesPDU( string input)
{
ShortMessageCollection messages = new ShortMessageCollection();
尝试
{
Regex r = new 正则表达式( @ \ + CMGL:(\d +),(\d +),,(\d + )\\\\\(。*?)\\\\\\ n,RegexOptions.IgnoreCase | RegexOptions.Singleline);
匹配m = r.Match(输入);
while (m.Success)
{
ShortMessage msg = new ShortMessage();
// msg.Index = int.Parse(m.Groups [1] .Value);
msg.Index = m.Groups [ 1 ]。值;
msg.Status = m.Groups [ 2 ]。价值;
msg.Sender = m.Groups [ 3 ]。值;
msg.Alphabet = m.Groups [ 4 ]。价值;
msg.Sent = m.Groups [ 5 ]。值;
msg.Message = m.Groups [ 6 ]。值;
messages.Add(msg);
m = m.NextMatch();
}
}
catch (例外情况)
{
// throw ex;
}
return 条消息;
}
希望你能解决这个问题。
谢谢。
解决方案
用一个 <替换双\
code> \ 。您使用的是C#逐字字符串(@...
),因此,每个字符都按原样使用 - 不需要转义(除外)
必须加倍才能进行转义。另请参阅在C#中转义:字符,字符串,字符串格式,关键字,标识符 [ ^ ]。
干杯
Andi
< blockquote>如果你想要的只是长六角形字符串,这个正则表达式应该有效。
\\\\\\ n [0-9A-F] + \\\\\\ n
或
\\ \\\\ [0-9A-F] {10,} \\\\\\ //至少10个字符
您也可以跳过\\\\ n。
Hello, can you share my the code on how to get only the certain string?
this is the whole string.
AT+CMGL=4\r\r\n+CMGL: 1,2,,48\r\n079136190800101011000BA19084537777F70008AA2200540065007300740020004D0065007300730061006700650020005000440055002E\r\n+CMGL: 2,1,,34\r\n0791361908005043040C9136493875777700005101519103652311D4F29C0E6A96E7F3F0B90C8212AB2E\r\n\r\nOK\r\n
now I wan to parse and get only this.
079136190800101011000BA19084537777F70008AA2200540065007300740020004D0065007300730061006700650020005000440055002E
or
0791361908005043040C9136493875777700005101519103652311D4F29C0E6A96E7F3F0B90C8212AB2E
this is the code that I've used but not working.
public ShortMessageCollection ParseMessagesPDU(string input)
{
ShortMessageCollection messages = new ShortMessageCollection();
try
{
Regex r = new Regex(@"\+CMGL: (\d+),(\d+),,(\d+)\\r\\n(.*?)\\r\\n", RegexOptions.IgnoreCase | RegexOptions.Singleline);
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;
messages.Add(msg);
m = m.NextMatch();
}
}
catch (Exception ex)
{
//throw ex;
}
return messages;
}
Hope you can help me with this problem.
Thank you.
解决方案
Replace the double\
by one\
. You are in a C# verbatim string (@"..."
), so, each character is taken as is - no escaping needed (except for"
which must be doubled for escaping). See also Escaping in C#: characters, strings, string formats, keywords, identifiers[^].
Cheers
Andi
This regex should work if the only thing you want is the long hex-string.
\\r\\n[0-9A-F]+\\r\\n
or
\\r\\n[0-9A-F]{10,}\\r\\n //At least 10 characters
You can probably skip the \r\n as well.
这篇关于如何从AT命令解析PDU字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文