GSM通信,交叉线程

时间:2013-08-19 17:21:41

标签: c# multithreading serial-port at-command

我在C#编程方面有点新鲜,但不知怎的,我设法通过使用serialPort获得了一项需要通过GSM(SMS)通信获得高超技能和知识的项目。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace SMSget
{
    public partial class SMSLogPanel : UserControl
    {
        #region default constructor
        public SMSLogPanel()
        {
            InitializeComponent();
            serialPort1 = new SerialPort();
            serialPort1.DataBits = 8;
            serialPort1.DtrEnable = true;
            serialPort1.Encoding.Equals("iso-8859-1");
            serialPort1.Handshake = Handshake.RequestToSend;
            serialPort1.Parity = Parity.None;
            serialPort1.WriteTimeout = 300;
            serialPort1.StopBits = StopBits.One;
            checkLink();
        }
    #endregion
    #region checking communication and setting user controls...
    private void checkLink()
    {
        GetValues value = new GetValues();
        string com = value.getPort();
        int baud = value.getBaud();
        int timeot = value.getTimeout();
        serialPort1.PortName = com;
        serialPort1.BaudRate = baud;
        serialPort1.ReadTimeout = timeot;

        serialPort1.Open();
        if (serialPort1.IsOpen)
        {
            label1.Visible = true;
        }
        else
        {
            MessageBox.Show("Komunikacija sa modemom se ne može uspostaviti, molimo postavite novu konfiguraciju...!");
            this.Controls.Clear();
            SMSConfigPanel cfg = new SMSConfigPanel();
            cfg.Show();
            this.Controls.Add(cfg);
        }
        serialPort1.Close();
    }
    #endregion
    #region setiranje timer-a...
    private void timer1_Tick(object sender, EventArgs e)
    {
        timer1.Stop();
        execCommand();
        timer1.Start();
    }
    #endregion
    #region seting handler and executing AT commands
    public void execCommand()
    {
        serialPort1.DataReceived += new SerialDataReceivedEventHandler(getResponse);
        serialPort1.Open();
        //prazni se buffer da se ne pokupe neke vrijednosti koje ne trebaju...
        serialPort1.DiscardInBuffer();

        if (!serialPort1.IsOpen)
        {
            MessageBox.Show("Modem nije spojen, molimo provjerite konfiguraciju...!");
            timer1.Stop();
        }
        serialPort1.Write("AT+CMGF=1" + (char)(13));
        serialPort1.Write("AT+CMGL=\"ALL\"" + (char)(13));
    }
    public void getResponse(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort serPort = (SerialPort)sender;
        string input = serPort.ReadExisting();
        if (input.Contains("ERROR"))
        {
            //textBox2.Text = "";
        }
        else if (input.Contains("+CMTI:"))
        {
            serialPort1.Write("AT+CMGF=1" + (char)(13));
            serialPort1.Write("AT+CMGL=\"ALL\"" + (char)(13));
        }
        else if (input.Contains("+CMGL:"))
        {
            textBox1.Text = input;
        }
        else
        {
            return;
        }
        serialPort1.Close();
    }
    #endregion
}

}

我认为我设法在这些区域创建了错误(没有打开/关闭serialPort1端口,getResponse(对象发送者,SerialDataReceivedEventArgs e)中的输入数据,由于交叉线程问题,使用单独的线程无法传递到textBox1。 。,并且可能错误的AT命令用于读取已收到的已发送的消息......)

如果有人能帮助我,我将非常感激......

谢谢你,并致以最诚挚的问候。

2 个答案:

答案 0 :(得分:0)

跨线程异常的原因:

您正在尝试更新除创建它之外的线程上的GUI元素。这是不允许的。

<强>解决方案:

避免跨线程异常。您应该像这样更新您的代码

this.Invoke((MethodInvoker) delegate
    {
        // update your textbox here

    });

答案 1 :(得分:0)

serialPort1.Write("AT+CMGF=1" + (char)(13));
serialPort1.Write("AT+CMGL=\"ALL\"" + (char)(13));

这不正确。您必须读取并解析每个命令后从调制解调器返回的响应,例如

之类的东西
serialPort1.Write("AT+CMGF=1" + (char)(13));
waitForFinalResponse(serialPort1);
serialPort1.Write("AT+CMGL=\"ALL\"" + (char)(13));
waitForFinalResponse(serialPort1);

或者可能将serialPort1.Write和waitForFinalResponse组合成sendATCommand函数。有关详细信息,另请参阅thisthis答案。