串行端口读取结果包含响应,也包含我给出的命令,如何解决此问题



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

1)从我的gui

发送命令

2)调制解调器接收到它,然后触发数据牵引事件

3)调制解调器运行命令,回复还触发数据序列的事件。

4)然后收到的数据既包含我的给定命令,又包含回复

例如:

输入:at cops = 0

输出:at cops = 0ok(确定是调制解调器响应)

输入:

输出:atok(确定是调制解调器响应)

我在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);

}

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

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

ATE0

最新更新