我从控制台保存命令的输出。
输出可以有多于一行。
如何在所有输出中搜索特定单词?
这是我当前的代码:
string buff, ok;
ok = "OK";
SerialPort p = ...
// p initialization...
p.Write("AT+CMGL=\"REC UNREAD\" " + "\r");
System.Threading.Thread.Sleep(1000);
buff = p.ReadExisting();
if (buff.Contains(ok))
// do smth
输出示例:
+CMGL: 10,"REC READ","0372022244","12/02/22,08:08:58+08" 2073692066616374757261207661206669206163686974617461206C612074696D702120446574616C696920696E206D6167617A696E656C6520566F6461666F6E6520736175206C612062616E6361206476732E 好的
这会搜索 buff 中的所有行吗?还是只是第一行?我这样尝试过,似乎他在输出中找不到“OK”
namespace SerialTest
{
public class Program
{
public static void Main(string[] args)
{
string buff="0";
string ok = "OK";
SerialPort p = new SerialPort("COM28");
p.DataReceived += new SerialDataReceivedEventHandler(p_DataReceived);
p.Open();
string line = "1";
p.Write("AT" + "\r");
buff = p.ReadExisting();
p.Write("AT+CMGF=1"+ "\r" );
buff = p.ReadExisting();
do
{
p.Write("AT+CMGL=\"REC UNREAD\" " + "\r");
buff = p.ReadExisting();
if (buff.Contains(ok))
Console.WriteLine("Everything is OK");
else
Console.WriteLine("NOK");
line = Console.ReadLine();
} while (line != "quit");
p.Close();
}
public static void p_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Console.WriteLine((sender as SerialPort).ReadExisting());
}
}
}