【问题标题】:Serial port read result contains response but also the command I give, how to solve this?串口读取结果包含响应,也包含我给出的命令,如何解决?
【发布时间】:2017-02-23 16:30:03
【问题描述】:

我正在为调制解调器通信工具编写一个 GUI,能够接收 AT 命令并在调制解调器中执行后从调制解调器返回结果。我使用串口 datareceived 事件来处理接收到的数据,但它不仅包含来自调制解调器的响应,还包含我给出的 AT 命令。现在我想是因为:

1) 从我的 GUI 发送命令

2) Modem 接收,然后触发 datareceived 事件

3) Modem 运行命令,回复也触发 datareceived 事件。

4) 接收到的数据包含我给定的命令和回复

例如:

输入:AT+COPS=0

输出:AT+COPS=0OK(OK 是调制解调器响应)

输入:AT

输出:ATOK(OK 是调制解调器响应)

我在 MSDN 中找不到解决方案。调制解调器中有两个不同的缓冲区吗? 如何解决?

这是我的代码:

void serialPort_DataReceived(object s, SerialDataReceivedEventArgs e)
{

    if (e.EventType != SerialData.Chars)
    {
        return;
    }
    try
    {
        System.Threading.Thread.Sleep(1000);
        string indata = atPort.ReadExisting();
        string status = timeStamp.ToShortDateString() + " " + timeStamp.ToUniversalTime() + "   " + "Read from Modem: " + indata;
        this.Invoke(new MethodInvoker(delegate () { this.listBox_CommandOutput.Items.Add(status); }));
    }
    catch (Exception ex)
    {

    }
}


private void executeCommandLine()
{
    if (this.textBox_CommandLine.Text != "")
    {
        try
        {
            atPort.DiscardInBuffer();
            atPort.DiscardOutBuffer();
            this.atPort.Write(this.textBox_CommandLine.Text.ToString()+"\\r");
            this.listBox_CommandOutput.Items.Add(timeStamp.ToShortDateString() + " " + timeStamp.ToUniversalTime() + "   " + "Write to Modem: " + this.textBox_CommandLine.Text.ToString());

        }
        catch (Exception exc)
        {
            this.listBox_CommandOutput.Items.Add(exc.ToString());
        }
    }
    else MessageBox.Show("Command can't be void.", "COM talk", MessageBoxButtons.OK);

}

【问题讨论】:

    标签: c# port modem


    【解决方案1】:

    问题不在您的代码中。默认情况下,某些调制解调器默认使用 echo 模式:它们会将发送给它们的每个字符回显给发送者(因此您可以像终端一样使用它们)。

    您可以使用 AT 命令禁用回显模式:

    ATE0
    

    【讨论】:

    • 完美,它有效,我没有看到我给出的重复命令。谢谢!
    猜你喜欢
    • 2013-06-08
    • 2021-04-12
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    相关资源
    最近更新 更多