serial port - How can i search for string in multiple line string variable? C# -


i save output of command console.

the output can have more 1 line.

how can search specific words in output ?

here current code:

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))     // smth 

an example of output:

 +cmgl: 10,"rec read","0372022244",,"12/02/22,08:08:58+08" 2073692066616374757261207661206669206163686974617461206c612074696d702120446574616c696920696e206d6167617a696e656c6520566f6461666f6e6520736175206c612062616e6361206476732e  ok 

will search lines buff ok ? or first line ? tried , seems can not find "ok" in output

    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();                          {                 p.write("at+cmgl=\"rec unread\" " + "\r");                 buff = p.readexisting();                  if (buff.contains(ok))                     console.writeline("everything 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 serialport).readexisting());         }     } } 

p.readexisting() read operations on serialport.

the msdn documentation says:

reads available bytes, based on encoding, in both stream , input buffer of serialport object.

this means might not data expecting in 1 single call readexisting data might not ba available yet. should loop through , read data serial port before doing contains check.

on side note: using thread.sleep when dealing serial operations not idea. instead of sleeping while waiting data, use serialport.datareceived event read available data.

try correctly read data serial port , rid of sleep

string data = ""; p.datareceived += new serialdatareceivedeventhandler(datareceivedhandler); private static void datareceivedhandler(object sender, serialdatareceivedeventargs e) {    serialport sp = (serialport)sender;    string indata = sp.readexisting();    data += indata; } 

and have timer set check output after 1 second. in timer event handler check data.contains("ok");


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -