c# - 串口读取结果有response还有我给的command,怎么解决?

标签 c# port modem

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

1) 从我的 GUI 发送命令

2) Modem接收到,触发datareceived事件

3) 调制解调器运行命令,回复也触发数据接收事件。

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

例如:

Input: AT+COPS=0

Output:AT+COPS=0OK (OK is modem response)

Input: AT

Output:ATOK (OK is modem response)

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

最佳答案

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

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

ATE0

关于c# - 串口读取结果有response还有我给的command,怎么解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42421272/

相关文章:

c++ - 我如何在 linux 中获取程序的版本

port - 如何更改已发布的 Blazor Server 项目的端口?

spring-boot - 如何在运行时获取 SPRING Boot HOST 和 PORT 地址?

c++ - 在 Linux 上运行的开源软调制解调器?

php - 授予 PHP 访问 COM 端口的权限

c# - 为什么我不能像这样使用空合并运算符?

c# - EasynetQ 总线。订阅未触发 - 抛出 "messageType must not be null"

c# - Java 继承约束

c# - 使电话连接到调制解调器振铃

c# - 映射项目时 AutoMapper 中的事件