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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:04:15+00:00 2026-05-15T11:04:15+00:00

I am using serial port communications in my ASP.NET webform application: private bool sendSMS(int

  • 0

I am using serial port communications in my ASP.NET webform application:

private bool sendSMS(int portNo, string mobNo, string details)
{
    try
    {
        SerialPort SerialPort1 = new SerialPort();
        SerialPort1.PortName = "COM" + portNo.ToString();
        SerialPort1.BaudRate = 9600;
        SerialPort1.Parity = Parity.None;
        SerialPort1.DataBits = 8;
        SerialPort1.StopBits = StopBits.One;
        SerialPort1.RtsEnable = true;
        SerialPort1.DtrEnable = true;
        SerialPort1.Encoding.GetEncoder();
        SerialPort1.ReceivedBytesThreshold = 1;
        SerialPort1.NewLine = Environment.NewLine;
        SerialPort1.Open();
        SerialPort1.Write("AT" + SerialPort1.NewLine);
        Sleep(500);
        SerialPort1.Write("AT+CMGF=1" + SerialPort1.NewLine);
        Sleep(500);
        SerialPort1.Write("AT+CMGS=" + (char)34 + mobNo + (char)34 +
                                    SerialPort1.NewLine);
        Sleep(1000);
        SerialPort1.Write(details + (char)26);
        Sleep(2000);
        SerialPort1.Close();
    }
    catch
    {

    }
    return true;
}

This method works when I send in a single message… But when I want to send SMSes in bulk opening and closing port everytime is not a good idea… Is it possible to use a serial port like session in c#?

When I open a port I want it to be open for one hour and then if my time expires I want to close the port and open it the next time… Is it possible?

  • 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-15T11:04:16+00:00Added an answer on May 15, 2026 at 11:04 am

    You could create an object and in its constructor/initialization call SerialPort.Open() and implement IDisposable to call SerialPort.Close(). For the lifetime of the object, it’ll be open.

    Something a little more detailed is below (though definitely not a complete solution). The general idea is to encapsulate the port connection lifetime into the object lifetime so that when the object goes out of scope/use and gets cleaned by the GC then so does the port connection.

    @jrista makes a good point about needing to handle any error conditions. ErrorReceived will help with that, as well as ol’ fashioned error handling throughout this code where necessary.

    public class SerialPortConnection : IDisposable
    {
        private bool disposed;
        public SerialPort Port { get; protected set; }
    
        public SerialPortConnection(int portNo)
        {
            this.Initialize(portNo);
            this.Port.ErrorReceived += this.portError;
            this.Port.Open();
        }
    
        public void Initialize(int portNo)
        {
            this.Port = new SerialPort();
            this.Port.PortName = "COM" + portNo.ToString();
            /* snip */
            this.Port.Encoding.GetEncoder();
            this.Port.ReceivedBytesThreshold = 1;
            this.Port.NewLine = Environment.NewLine;
        }
    
        protected void portError(
            object sender,
            SerialErrorReceivedEventHandler args)
        {
            // Do whatever with the error, maybe need to reopen the socket.
        }
    
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    this.Port.Close();
                    this.Port.Dispose(disposing);
                }
                this.disposed = true;
            }
        }
    
        public void Dispose()
        {
            this.Dispose(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to open and read from a serial port using the System.IO.Ports.SerialPort class.
Using ASP.NET MVC there are situations (such as form submission) that may require a
I'm using socat to create a virtual serial port with: socat PTY,link=/dev/ttySV0,echo=1 PTY,link=/dev/ttySV1,echo=1 The
I'm using RXTX to read data from a serial port. The reading is done
I would like to send a .gif file over a serial port connection using
I want to read and write from serial using events/interrupts. Currently, I have it
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using VS2008, C#, .Net 2 and Winforms how can I make a regular Button
As serial port communication is asynchronous, I figured out early on into my project
I have a linux server (Red Hat 4) with one serial port connection to

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.