【问题标题】:c# sending sms from computerc#从电脑发送短信
【发布时间】:2010-06-12 01:00:24
【问题描述】:

我有这个代码:

private SerialPort port = new SerialPort("COM1", 115200, Parity.None, 8, StopBits.One);

                  Console.WriteLine("Incoming Data:");
                  port.WriteTimeout = 5000;
                  port.ReadTimeout = 5000;
                  // Attach a method to be called when there is data waiting in the port's buffer
                  port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

                  // Begin communications
                  port.Open();
                  #region PhoneSMSSetup
                  port.Write("AT+CMGF=1\r\n");
                  Thread.Sleep(500);
                  port.Write("AT+CNMI=2,2\r\n");
                  Thread.Sleep(500);
                  port.Write("AT+CSCA=\"+4790002100\"\r\n");
                  Thread.Sleep(500);
                  #endregion
                  // Enter an application loop which keeps this thread alive
                  Application.Run();

我是从这里得到的:

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_22832563.html

我有一个新的 winforms 空应用程序:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

你能告诉我吗:

  1. 我应该将代码粘贴到哪里?
  2. 如何让代码运行?

我正在向连接到计算机的手机发送 AT 命令

【问题讨论】:

    标签: c# text sms


    【解决方案1】:

    上面的代码示例似乎表明它应该在一个单独的线程中运行(这是有道理的),所以在您的表单中添加一个开始按钮并添加一个Click 事件处理程序。

    我刚刚从here 获取下面的代码并重新格式化了一下。

    在该事件处理程序中编写如下内容:

    ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
    Thread myThread = new Thread(myThreadDelegate);
    myThread.Start();
    

    然后像这样创建一个类:

    public class ThreadWork 
    {
       public static void DoWork()
       {
           // put your top half code in here, the bit that does the actual serial communication
       }
    }
    

    然后添加您的代码所期望的事件处理程序port_DataReceived

    【讨论】:

      【解决方案2】:

      将 SerialPort 从工具箱拖放到表单上。设置其属性并双击 DataReceived。这会处理前 5 行。

      在 Load 事件处理程序中,放置 Open 和 Write 调用,这会处理其余的行。

      在窗体上放置一个 TextBox,将其 MultiLine 属性设置为 True。在 DataReceived 事件处理程序中编写此代码:

          private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) {
              string response = serialPort1.ReadLine();
              this.BeginInvoke(new MethodInvoker(
                  () => textBox1.AppendText(response + "\r\n")
              ));
          }
      

      从那里,您可以修改表单设计以使其更有用。也许你想添加一个按钮,它的 Click 事件再次轮询调制解调器。

      【讨论】:

      • 谢谢你,汉斯,到目前为止,至少我的程序可以编译,但我还有其他问题,正如你在其他帖子中看到的那样
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      • 1970-01-01
      相关资源
      最近更新 更多