将gsm短信添加到文本框或列表框(WinForm)

时间:2019-02-06 12:05:18

标签: c# winforms gsm

我有以下代码用于从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();
    }

0 个答案:

没有答案