views:

54

answers:

1

I am using serial port communication 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 i single message... But when want to send sms in bulk opening and closing port everytime is not a good idea... So my question is it possible to use a serial port like session in c#?... When i open a port i want it to be open for 1 hour and then if my time expires i want to close the port and open it the next time... Any suggestion...

+1  A: 

You could create an object and in it's 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);
    }
}
SnOrfus
`For the lifetime of the object, it'll be open.` That's barring any complications with the underlying socket. Assuming a socket will live untainted and uninterrupted for any duration of time is asking for trouble. At the very least, Open/Close should be more dynamic, happening in response to activity on the session object, rather than only at its initialization or disposal. +1, but I would probably offer a more thorough example.
jrista
@Snorfus can you provide me a sample of that..
Pandiya Chendur