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

发布于 2024-09-05 23:33:36 字数 1389 浏览 2 评论 0原文

我在我的 ASP.NET 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;
}

此方法适用于我发送一条消息...但是当我想批量发送 SMSes 时每次关闭端口不是一个好主意...是否可以在

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮云落日 2024-09-12 23:33:36

您可以创建一个对象,并在其构造函数/初始化中调用 SerialPort.Open() 并实现 IDisposable 来调用 SerialPort.Close()。在对象的生命周期内,它将是开放的。

下面是更详细的内容(尽管绝对不是完整的解决方案)。总体思路是将端口连接生命周期封装到对象生命周期中,以便当对象超出范围/使用并由 GC 那么端口连接也是如此。

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

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文