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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:04:40+00:00 2026-06-11T17:04:40+00:00

I am sending and receiving data using COM port (serial). I have written the

  • 0

I am sending and receiving data using COM port (serial). I have written the following code. This is actually my first C# project as I am kinda new to it. I am trying to write the received data to the text file on my desktop, the program actually creates the file but writes nothing in it. Similarly, I am able to see the received data on the console but it is not being written to the text file. Any help on what I am doing wrong will be much appreciated.

Thank you. The code is below.

class Program
{
    SerialPort p = new SerialPort("COM7", 300, Parity.None, 8, StopBits.One);
    string sbuffer = string.Empty;
    byte i = 0;
    static void Main(string[] args)
    {
        new Program();
    }

    Program()
    {
        string[] names = SerialPort.GetPortNames();
        Console.WriteLine("Serial ports:");
        foreach (string name in names)
        {
            Console.WriteLine(name);
        }
        Console.WriteLine("Using COM7");

        p.Open();


        string data_ = "$1RB\r";
        Console.WriteLine("Writing data: {0}",data_);
        p.Write(data_);
        p.DataReceived += new SerialDataReceivedEventHandler(p_DataReceived);
        Console.ReadKey();
        p.Close();
    }


    void p_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        Thread.Sleep(5);
        sbuffer += (sender as SerialPort).ReadExisting();
        i++;
        if (i > 9)
        {
            Console.WriteLine(sbuffer);
          System.IO.File.WriteAllText(@"C:\Users\myname\Desktop\WriteText.txt", sbuffer);
            sbuffer = string.Empty;
        }
    }
}

}

  • 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-11T17:04:41+00:00Added an answer on June 11, 2026 at 5:04 pm

    You could use events, or simply use this method and pass your data string to it. It will simply append to the file as long as it exists, or create a new file if it does not. The data written should be identical to whatever output is appearing in your console screen.

        static void WriteOutputToTextFile(string _data)
        {
            string FolderName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);   //set destination as your desktop
            using (StreamWriter SW = new StreamWriter(FolderName + "\\test.txt", true))   //true makes it append to the file instead of overwrite
            {
                SW.WriteLine(_data);
                SW.Close();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the data actual path if I run 2 applications sending/receiving data using
I am sending and receiving data using fsockopen and fwrite. My application receives the
I am using CFMessagePortRef for inter process communication. for receiving data , i have
I'm sending a string of data via CURL using data from post, and receiving
I'm using an AsyncTask to establish a TCP Connection and sending/receiving data through it.
I got success in sending/receiving data using TCP Sockets over LAN but I want
I am using the same UDP socket for sending and receiving data. I am
I have checked the memory whilst sending and receiving data over one connection, and
I am sending HTTP GET request and receiving data here: ssize_t numBytes = recvfrom(sock,
I am testing the sending and receiving programs with the code as The main()

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.