Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8050571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:07:00+00:00 2026-06-05T07:07:00+00:00

I save an output of a command from console. The output can have more

  • 0

I save an output of a command from console.

The output can have more than one line.

How can I search for specific words in all the output ?

Here is my 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)) 
   // do smth

an example of the output:

+CMGL: 10,"REC READ","0372022244",,"12/02/22,08:08:58+08"
2073692066616374757261207661206669206163686974617461206C612074696D702120446574616C696920696E206D6167617A696E656C6520566F6461666F6E6520736175206C612062616E6361206476732E

OK

will this search all the lines from buff for ok ? or just the first line ? i tried it like this and it seems that he can not find “OK” in the 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();

            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());
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:07:01+00:00Added an answer on June 5, 2026 at 7:07 am

    p.ReadExisting() does read operations on a SerialPort.

    The MSDN documentation says:

    Reads all immediately available bytes, based on the encoding, in both
    the stream and the input buffer of the SerialPort object.

    This means that you might not get all the data you are expecting in one single call to ReadExisting as that data might not ba available yet. You should probably loop through and read all the data from the serial port before doing the Contains check.

    On a side note: using Thread.Sleep especially when dealing with serial operations is not a good idea. Instead of sleeping while waiting for the data, use the SerialPort.DataReceived event to read available data.

    Try this to correctly read data from your serial port and get rid of the 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 then have a timer set to check the output after 1 second.
    In the timer event handler you check data.Contains("OK");

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program which outputs various results onto a command line console. How
I'd like to save the output from a Select * FROM table1 in console
I have a command line utility from a third party (it's big and written
I'm using the following to save a CSV file of output from Event Logs
Does anyone know of a way to save the console.log output in Chrome to
When I click the save link for a new blog topic, the console output
I have a small command line tool and after running it, I'd like to
I would like to save output of first command as variable in front of
I have a program which utilizes a 3rd party command line tool to generate
I have set up a cron task and I want to save the output

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.