c# - 将 gsm 短信添加到文本框或列表框 (WinForm)
问题描述
我有这些代码用于从我的 gsm 调制解调器接收短信。它有效,但我希望在列表框或文本框中查看消息。有人可以帮助我如何做到这一点吗?
public void Read()
{
gsmPort.WriteLine("AT+CMGF=1"); // Set mode to Text(1) or PDU(0)
Thread.Sleep(1000); // Give a second to write
gsmPort.WriteLine("AT+CPMS=\"SM\""); // Set storage to SIM(SM)
Thread.Sleep(1000);
gsmPort.WriteLine("AT+CMGL=\"ALL\""); // What category to read ALL, REC READ, or REC UNREAD
Thread.Sleep(1000);
gsmPort.Write("\r");
Thread.Sleep(1000);
string response = gsmPort.ReadExisting();
if (response.EndsWith("\r\nOK\r\n"))
{
Console.WriteLine(response);
// add more code here to manipulate reponse string.
}
else
{
// add more code here to handle error.
Console.WriteLine(response);
}
这就是我使用这些代码与调制解调器通信的方式。它有效,但仅在控制台上。我想将这些添加到我的 winform
class GSMsms
{
private SerialPort gsmPort = null;
private bool IsDeviceFound { get; set; } = false;
public bool IsConnected { get; set; } = false;
public GSMsms()
{
gsmPort = new SerialPort();
}
public GSMcom[] List()
{
List<GSMcom> gsmCom = new List<GSMcom>();
ConnectionOptions options = new ConnectionOptions();
options.Impersonation = ImpersonationLevel.Impersonate;
options.EnablePrivileges = true;
string connString = $@"\\{Environment.MachineName}\root\cimv2";
ManagementScope scope = new ManagementScope(connString, options);
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_POTSModem");
ManagementObjectSearcher search = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection collection = search.Get();
foreach (ManagementObject obj in collection)
{
string portName = obj["AttachedTo"].ToString();
string portDescription = obj["Description"].ToString();
if (portName != "")
{
GSMcom com = new GSMcom();
com.Name = portName;
com.Description = portDescription;
gsmCom.Add(com);
}
}
return gsmCom.ToArray();
}
解决方案
推荐阅读
- python - 如何将精确格式的表格从 teradata 导出到 excel?
- excel - 我需要将字符串选择转换为范围
- javascript - JavaScript:在 Vue.js 模态中获取 DOM 元素
- asp.net-core - 由于无法启动网站
在 web.config 中 - python - 调用函数时如何传递变量关键字参数
- c# - 为什么下面的位移操作没有丢弃向左移动的位移位?
- python - postgresql 表名中的特殊字符
- react-native - 我似乎无法以正确的方式嵌套导航器
- javascript - Web Worker importScripts DOMException 脚本未能加载
- html - 注释掉 Angular html 模板中的单个指令或属性