我保存從控制檯輸出的命令。如何搜索多行字符串變量中的字符串? C#
輸出可以有多行。
如何搜索所有輸出中的特定單詞?
這裏是我當前的代碼:
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 OK
將這個搜索從BUFF所有線好嗎?或只是第一行?我試圖像這樣,似乎他找不到不上SerialPort
讀操作輸出
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());
}
}
}
[你有什麼試過](http://whathaveyoutried.com)?這並不困難,所以請解釋你嘗試過的以及你卡在哪裏。 – Oded
字符串愛好者,好的; ok =「OK」; p。寫(「AT + CMGL = \」REC UNREAD \「」+「\ r」); System.Threading.Thread.Sleep(1000); buff = p.ReadExisting(); if(buff.Contains(ok)) do smth –
Please _edit_ your question and add the code sample to it。 – Oded