是否可以在 C# 中使用类似串行端口的会话?
我在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个对象,并在其构造函数/初始化中调用
SerialPort.Open()
并实现 IDisposable 来调用SerialPort.Close()
。在对象的生命周期内,它将是开放的。下面是更详细的内容(尽管绝对不是完整的解决方案)。总体思路是将端口连接生命周期封装到对象生命周期中,以便当对象超出范围/使用并由 GC 那么端口连接也是如此。
@jrista 提出了一个很好的观点,即需要处理任何错误情况。 ErrorReceived 将对此有所帮助,并在必要时在整个代码中进行老式错误处理。
You could create an object and in its constructor/initialization call
SerialPort.Open()
and implement IDisposable to callSerialPort.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.