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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:53:21+00:00 2026-05-28T15:53:21+00:00

In C#, How do you communicate with serial port to read and write data?

  • 0

In C#, How do you communicate with serial port to read and write data?

I a looking for a sample program just to learn the basics of serial port communications in C#.

  • 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-05-28T15:53:22+00:00Added an answer on May 28, 2026 at 3:53 pm

    From the MSDN documentation of System.IO.Ports.SerialPort:

    using System;
    using System.IO.Ports;
    using System.Threading;
    
    public class PortChat
    {
        static bool _continue;
        static SerialPort _serialPort;
    
        public static void Main()
        {
            string name;
            string message;
            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            Thread readThread = new Thread(Read);
    
            // Create a new SerialPort object with default settings.
            _serialPort = new SerialPort();
    
            // Allow the user to set the appropriate properties.
            _serialPort.PortName = SetPortName(_serialPort.PortName);
            _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
            _serialPort.Parity = SetPortParity(_serialPort.Parity);
            _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
            _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
            _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);
    
            // Set the read/write timeouts
            _serialPort.ReadTimeout = 500;
            _serialPort.WriteTimeout = 500;
    
            _serialPort.Open();
            _continue = true;
            readThread.Start();
    
            Console.Write("Name: ");
            name = Console.ReadLine();
    
            Console.WriteLine("Type QUIT to exit");
    
            while (_continue)
            {
                message = Console.ReadLine();
    
                if (stringComparer.Equals("quit", message))
                {
                    _continue = false;
                }
                else
                {
                    _serialPort.WriteLine(
                        String.Format("<{0}>: {1}", name, message) );
                }
            }
    
            readThread.Join();
            _serialPort.Close();
        }
    
        public static void Read()
        {
            while (_continue)
            {
                try
                {
                    string message = _serialPort.ReadLine();
                    Console.WriteLine(message);
                }
                catch (TimeoutException) { }
            }
        }
    
        public static string SetPortName(string defaultPortName)
        {
            string portName;
    
            Console.WriteLine("Available Ports:");
            foreach (string s in SerialPort.GetPortNames())
            {
                Console.WriteLine("   {0}", s);
            }
    
            Console.Write("COM port({0}): ", defaultPortName);
            portName = Console.ReadLine();
    
            if (portName == "")
            {
                portName = defaultPortName;
            }
            return portName;
        }
    
        public static int SetPortBaudRate(int defaultPortBaudRate)
        {
            string baudRate;
    
            Console.Write("Baud Rate({0}): ", defaultPortBaudRate);
            baudRate = Console.ReadLine();
    
            if (baudRate == "")
            {
                baudRate = defaultPortBaudRate.ToString();
            }
    
            return int.Parse(baudRate);
        }
    
        public static Parity SetPortParity(Parity defaultPortParity)
        {
            string parity;
    
            Console.WriteLine("Available Parity options:");
            foreach (string s in Enum.GetNames(typeof(Parity)))
            {
                Console.WriteLine("   {0}", s);
            }
    
            Console.Write("Parity({0}):", defaultPortParity.ToString());
            parity = Console.ReadLine();
    
            if (parity == "")
            {
                parity = defaultPortParity.ToString();
            }
    
            return (Parity)Enum.Parse(typeof(Parity), parity);
        }
    
        public static int SetPortDataBits(int defaultPortDataBits)
        {
            string dataBits;
    
            Console.Write("Data Bits({0}): ", defaultPortDataBits);
            dataBits = Console.ReadLine();
    
            if (dataBits == "")
            {
                dataBits = defaultPortDataBits.ToString();
            }
    
            return int.Parse(dataBits);
        }
    
        public static StopBits SetPortStopBits(StopBits defaultPortStopBits)
        {
            string stopBits;
    
            Console.WriteLine("Available Stop Bits options:");
            foreach (string s in Enum.GetNames(typeof(StopBits)))
            {
                Console.WriteLine("   {0}", s);
            }
    
            Console.Write("Stop Bits({0}):", defaultPortStopBits.ToString());
            stopBits = Console.ReadLine();
    
            if (stopBits == "")
            {
                stopBits = defaultPortStopBits.ToString();
            }
    
            return (StopBits)Enum.Parse(typeof(StopBits), stopBits);
        }
    
        public static Handshake SetPortHandshake(Handshake defaultPortHandshake)
        {
            string handshake;
    
            Console.WriteLine("Available Handshake options:");
            foreach (string s in Enum.GetNames(typeof(Handshake)))
            {
                Console.WriteLine("   {0}", s);
            }
    
            Console.Write("Handshake({0}):", defaultPortHandshake.ToString());
            handshake = Console.ReadLine();
    
            if (handshake == "")
            {
                handshake = defaultPortHandshake.ToString();
            }
    
            return (Handshake)Enum.Parse(typeof(Handshake), handshake);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following is a library for serial communications via PHP: http://www.phpclasses.org/package/3679-PHP-Communicate-with-a-serial-port.html . The problem
I am looking for a way to communicate with RS232 serial COM port on
I have to write a program in Visual C++ 2010 to communicate via Serial
I am just reviewing some code I wrote to communicate with the serial port
I created a class that handles serial port asynchronously. I use it to communicate
From Windows I can communicate with a serial port device using the following commands:
I'm using pyserial to communicate with a embedded devise. ser = serial.Serial(PORT, BAUD, timeout
I'm very new to serial port/windows programming... and need to communicate with a microcontroller
I am trying to communicate with a cash register through the serial port from
I am using a serial port to communicate with a remote diagnostics device. The

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.