简体   繁体   English

Visual C#2015串行端口-读取数据

[英]Visual C# 2015 Serial Port - reading data

I am trying to figure out what is wrong with my code. 我试图找出我的代码出了什么问题。 Serial modem is working. 串行调制解调器正在工作。 Just testing with ATI command to see the input, but I see nothing. 只是使用ATI命令进行测试以查看输入,但是我什么也看不到。 What could be wrong? 有什么事吗 I am using a thread to read the data from serial buffer. 我正在使用一个线程从串行缓冲区读取数据。

using System;
using System.IO.Ports;
using System.Threading;

namespace SerialPortProgram
{
    public class PortChat
    {
        static bool _continue = true;
        static SerialPort port = new SerialPort("COM1", 115200, Parity.None, 8, StopBits.One);

        public static void Main()
        {
            string message;
            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            Thread readThread = new Thread(Read);

            port.Open();
            port.WriteLine("ATI");
            Console.WriteLine("Type QUIT to exit");
            readThread.Start();
            while (_continue)
            {
                message = Console.ReadLine();

                if (stringComparer.Equals("quit", message))
                {
                    _continue = false;
                }
                else
                {
                    port.WriteLine(message);
                }
            }
            readThread.Join();
            port.Close();
            port.Dispose();
        }
        public static void Read()
        {
            Console.WriteLine("incoming...");
            Console.WriteLine(port.ReadExisting());
        }
    }
}

The SerialPort.ReadExisting() method doesn't block. SerialPort.ReadExisting()方法不会阻止。 So, your code starts a thread, which immediately calls ReadExisting() , which immediately returns an empty string, and then that thread exits. 因此,您的代码启动了一个线程,该线程立即调用ReadExisting() ,该线程立即返回一个空字符串,然后该线程退出。 It won't ever see the echo from the modem of whatever command you enter; 无论输入什么命令,它都不会看到调制解调器的回声。 you can't type that fast. 您不能快速输入。

You should not be using an explicit thread here anyway. 无论如何,您不应该在这里使用显式线程。 Instead, handle the SerialPort.DataReceived event, and call ReadExisting() when the event is raised. 而是,处理SerialPort.DataReceived事件,并在引发该事件时调用ReadExisting()

Even better, use the BaseStream property to get a Stream object, with which you can use the standard XXXAsync() methods, which work with async / await and allow you to write much more readable, easy-to-understand code. 更好的是,使用BaseStream属性获取一个Stream对象,您可以使用该对象使用标准的XXXAsync()方法,该方法可与async / await并允许您编写更具可读性,易于理解的代码。 Even a console program can benefit from this approach, and if and when you move to a GUI API, it will work even better. 即使是控制台程序也可以从这种方法中受益,并且当您转移到GUI API时,它将运行得更好。

Thank you very much Peter Duniho, with your input I changed my code. 非常感谢Peter Duniho,根据您的输入,我更改了代码。 Now it works like a charm..! 现在它就像一个魅力..!

using System;
using System.IO.Ports;
using System.Threading;

namespace SerialPortProgram
{
    public class PortChat
    {
        static bool _continue = true;
        static SerialPort port = new SerialPort("COM1", 115200, Parity.None, 8, StopBits.One);

        public static void Main()
        {
            port.Handshake = Handshake.None;

            string message;
            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

            port.Open();
            port.WriteLine("ATI");
            Console.WriteLine("Type QUIT to exit");
            while (_continue)
            {
                message = Console.ReadLine();

                if (stringComparer.Equals("quit", message))
                {
                    _continue = false;
                }
                else
                {
                    port.WriteLine(message);
                }
            }
            port.Close();
            port.Dispose();
        }
        private static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // Show all the incoming data in the port's buffer
            Console.WriteLine(port.ReadExisting());
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM