使用 SerialPort 类读取连接设备的响应

发布于 2024-09-18 10:58:59 字数 1636 浏览 3 评论 0原文

我正在使用 C# 的 SerialPort 类尝试向设备发送 AT 命令并获取响应。我已经验证它在超级终端中正常工作,如果我发送 AT 命令,它会返回 OK。但是,在我的控制台应用程序中,如果我发送 AT,它会回复回显 AT。代码如下,任何对我在接收代码中做错了什么的深入了解将不胜感激:

ComPort.DataReceived += new SerialDataReceivedEventHandler(ComPort_DataReceived);

public void Open()
        {
            Console.WriteLine();
            //close port if already open.
            if (ComPort.IsOpen)
            {
                ComPort.Close();
            }
            //setup port.
            ComPort.PortName = ConfigurationManager.AppSettings["PortName"].ToString();
            ComPort.BaudRate = Convert.ToInt32(ConfigurationManager.AppSettings["BaudRate"]);
            ComPort.Parity = Parity.None;
            ComPort.StopBits = StopBits.One;
            ComPort.DataBits = 8;
            ComPort.DtrEnable = true;
            ComPort.RtsEnable = true;
            if (Convert.ToBoolean(ConfigurationManager.AppSettings["HWFlowControlEnabled"]))
            {
                ComPort.Handshake = Handshake.RequestToSend;
            }
            //open port.
            Console.WriteLine("Opening port " + ComPort.PortName + "...");
            ComPort.Open();
            Console.WriteLine("Opened port " + ComPort.PortName);
        }

void ComPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string message = ComPort.ReadExisting();
            Console.WriteLine("RECEIVED: " + message);
            if (message.IndexOf("OK") > -1)
            {
                ReceivedOK = true;
            }
        }

I'm using C#'s SerialPort class to try and send a AT command to a device and get a response back. I've verified it works correctly in HyperTerminal, if I send a command of AT it responds back with OK. However, in my console app, if I send AT, it replies back with an echo AT. The code is below, any insight into what I'm doing wrong in my receiving code would be greatly appreciated:

ComPort.DataReceived += new SerialDataReceivedEventHandler(ComPort_DataReceived);

public void Open()
        {
            Console.WriteLine();
            //close port if already open.
            if (ComPort.IsOpen)
            {
                ComPort.Close();
            }
            //setup port.
            ComPort.PortName = ConfigurationManager.AppSettings["PortName"].ToString();
            ComPort.BaudRate = Convert.ToInt32(ConfigurationManager.AppSettings["BaudRate"]);
            ComPort.Parity = Parity.None;
            ComPort.StopBits = StopBits.One;
            ComPort.DataBits = 8;
            ComPort.DtrEnable = true;
            ComPort.RtsEnable = true;
            if (Convert.ToBoolean(ConfigurationManager.AppSettings["HWFlowControlEnabled"]))
            {
                ComPort.Handshake = Handshake.RequestToSend;
            }
            //open port.
            Console.WriteLine("Opening port " + ComPort.PortName + "...");
            ComPort.Open();
            Console.WriteLine("Opened port " + ComPort.PortName);
        }

void ComPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string message = ComPort.ReadExisting();
            Console.WriteLine("RECEIVED: " + message);
            if (message.IndexOf("OK") > -1)
            {
                ReceivedOK = true;
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹苦笑 2024-09-25 10:58:59

我认为默认是将您的命令回显给您,然后就OK了。首先发送 ATE0 以关闭回显:

http://tigger .cc.uic.edu/depts/accc/network/dialin/modem_codes.html

I think the default is to echo your commands back to you, then the OK. Send an ATE0 first to turn off echo:

http://tigger.cc.uic.edu/depts/accc/network/dialin/modem_codes.html

请恋爱 2024-09-25 10:58:59

默认情况下,设备(我猜是调制解调器)配置为回显所有通信。有 AT 命令可以打开和关闭 echo。此外,还存在多种硬件信令方法来控制数据流。请查看此处了解基本概述。

自从我从事调制解调器通信以来已经有一段时间了(实际上超过 10 年),所以如果我的答案不是 100% 准确,我很抱歉。

By default, the device (a modem I guess) is configured to echo all communication back. There are AT commands to turn echo on and off. Also, several hardware signalling approaches exist to control the flow of data. Have a look here for a basic overview.

It's quite a while (> 10 years actually) since I was doing modem communications, so I'm sorry in case my answer isn't 100% precise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文