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 8055051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:16:55+00:00 2026-06-05T08:16:55+00:00

I am trying to check some values some AT commands ran on a GSM

  • 0

I am trying to check some values some AT commands ran on a GSM modem.
I am stuck at the point where when trying to check if the output of a command contains some values is true or not.

Using the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Timers;


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();
            Console.WriteLine("buff: \"{0}\"\nok: \"{1}\"", buff, ok);
            p.Write("AT+CMGF=1"+ "\r" );
            buff = p.ReadExisting();
            Console.WriteLine("buff: \"{0}\"\nok: \"{1}\"", buff, ok);
            do
            {
                p.Write("AT+CMGL=\"REC UNREAD\" " + "\r");
                buff = p.ReadExisting(); 
                Console.WriteLine("buff: \"{0}\"\nok: \"{1}\"", buff, ok);
                /*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());
        }
    }
}

I get this output:

enter image description here

Why is it that sometimes buff is empty and is displayed later ?

  • 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-05T08:16:56+00:00Added an answer on June 5, 2026 at 8:16 am

    There are calls to ReadExisting() that are superfluous.
    You should not be reading your serial port output in the main method, let the event handler do the work for you.
    Also, you should append what you read in the event handler to the buff variable so that you can all the data received when you check your buffer.

    Try this instead:

    public class Program
    {
        public static void Main(string[] args)
        {
            ModemReader r = new ModemReader();
            r.StartReading();
        }
        class ModemReader{
            string buff="";
            public void StartReading()
            {
                string ok = "OK";
                SerialPort p = new SerialPort("COM28");
                p.DataReceived += new SerialDataReceivedEventHandler(p_DataReceived);
                p.Open();
                string line = "1";
                p.Write("AT" + "\r");
                Console.WriteLine("buff: \"{0}\"\nok: \"{1}\"", buff, ok);
                p.Write("AT+CMGF=1"+ "\r" );
                Console.WriteLine("buff: \"{0}\"\nok: \"{1}\"", buff, ok);
                do
                {
                    // Clear the buffer here
                    buff = "";  
    
                    p.Write("AT+CMGL=\"REC UNREAD\" " + "\r");
                    Console.WriteLine("buff: \"{0}\"\nok: \"{1}\"", buff, ok);
                    if (buff.Contains(ok))
                        Console.WriteLine("Everything is OK");
                    else
                        Console.WriteLine("NOK");
                    line = Console.ReadLine();
                } while (line != "quit");
                p.Close();
            }
            public void p_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                string s = (sender as SerialPort).ReadExisting();
                buff += s;
                Console.WriteLine(s);
            }
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to check in jQuery if a div contains some text, and then
I was trying to run my application and check for some output on the
I am trying to check that my function returns Some(x) testedFunc() |> should be
I am trying to do some style-check/translation work for Java programs. First I would
I am trying to check some checkboxes based on it value. In my code,
I am trying to insert some values in the table the query is below:
I'm trying to change some php code so that i get my values from
I am new to python and I am trying to import some values from
I'm trying to parse a string into smaller ones, extracting some values and then
I am trying to get some values from a List and then create a

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.