Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以在C#中使用类似于会话的串行端口?_C#_Asp.net_Session_Serial Port - Fatal编程技术网

是否可以在C#中使用类似于会话的串行端口?

是否可以在C#中使用类似于会话的串行端口?,c#,asp.net,session,serial-port,C#,Asp.net,Session,Serial Port,我在webform应用程序中使用串行端口通信: private bool sendSMS(int portNo, string mobNo, string details) { try { SerialPort SerialPort1 = new SerialPort(); SerialPort1.PortName = "COM" + portNo.ToString(); SerialPort1.BaudRate = 9600;

我在webform应用程序中使用串行端口通信:

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;
}
当我发送单个消息时,此方法有效。。。但当我想在批量发送时,每次打开和关闭端口都不是一个好主意。。。在中是否可以使用类似会话的串行端口


当我打开一个端口时,我想让它打开一个小时,然后如果我的时间到期,我想关闭该端口并在下次打开它。。。有可能吗?

您可以创建一个对象,并在其构造函数/初始化中调用
SerialPort.Open()
,并实现调用
SerialPort.Close()
。在对象的生存期内,它将处于打开状态

下面是一些更详细的内容(虽然肯定不是一个完整的解决方案)。一般的想法是将端口连接生存期封装到对象生存期中,这样当对象超出范围/使用范围并被清除时,端口连接也会被清除

@jrista非常强调需要处理任何错误情况。ErrorReceived将有助于这一点,并在必要时在整个代码中进行老式错误处理

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);
    }
}

在对象的生命周期内,它将处于打开状态。
这将阻止底层套接字出现任何复杂情况。假设一个插座在任何一段时间内都不受污染、不受干扰地工作,这是自找麻烦。至少,打开/关闭应该更加动态,响应会话对象上的活动,而不仅仅是在初始化或处理时+1,但我可能会提供一个更全面的例子。