微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#可以写但不能读任何串行数据

如何解决C#可以写但不能读任何串行数据

我试图编写一个简单的 WPF 应用程序来读取和写入数据到 Arduino。我可以写数据就好了。但我没有从董事会收到任何回报。在尝试了我能找到的所有解决方案后,我回到了基础并使用 MSFT 示例启动了一个简单的控制台应用程序。我使用空调制解调器模拟器和 Putty 建立了连接。我得到了同样的结果。我可以向 Putty 发送数据,但我在 Putty 中输入的任何内容都没有显示在控制台 APP 中。

这是我正在使用的示例代码。除了在打开端口之前我还添加了 DTR 和 RTS 启用线之外,它与 msft 示例没有变化......请帮忙。

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

public class PortChat
{
    static bool _continue;
    static SerialPort _serialPort;

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

        // Create a new SerialPort object with default settings.
        _serialPort = new SerialPort();

        // Allow the user to set the appropriate properties.
        _serialPort.PortName = SetPortName(_serialPort.PortName);
        _serialPort.Baudrate = SetPortBaudrate(_serialPort.Baudrate);
        _serialPort.Parity = SetPortParity(_serialPort.Parity);
        _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
        _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
        _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

        // Set the read/write timeouts
        _serialPort.ReadTimeout = 500;
        _serialPort.WriteTimeout = 500;
        _serialPort.DtrEnable = true;
        _serialPort.RtsEnable = true;
        _serialPort.open();
        _continue = true;
        readThread.Start();

        Console.Write("Name: ");
        name = Console.ReadLine();

        Console.WriteLine("Type QUIT to exit");

        while (_continue)
        {
            message = Console.ReadLine();

            if (stringComparer.Equals("quit",message))
            {
                _continue = false;
            }
            else
            {
                _serialPort.WriteLine(
                    String.Format("<{0}>: {1}",name,message));
            }
        }

        readThread.Join();
        _serialPort.Close();
    }

    public static void Read()
    {
        while (_continue)
        {
            try
            {
                string message = _serialPort.ReadLine();
                Console.WriteLine(message);
            }
            catch (TimeoutException) { }
        }
    }

    public static string SetPortName(string defaultPortName)
    {
        string portName;

        Console.WriteLine("Available Ports:");
        foreach (string s in SerialPort.GetPortNames())
        {
            Console.WriteLine("   {0}",s);
        }

        Console.Write("COM port({0}): ",defaultPortName);
        portName = Console.ReadLine();

        if (portName == "")
        {
            portName = defaultPortName;
        }
        return portName;
    }

    public static int SetPortBaudrate(int defaultPortBaudrate)
    {
        string baudrate;

        Console.Write("Baud Rate({0}): ",defaultPortBaudrate);
        baudrate = Console.ReadLine();

        if (baudrate == "")
        {
            baudrate = defaultPortBaudrate.ToString();
        }

        return int.Parse(baudrate);
    }

    public static Parity SetPortParity(Parity defaultPortParity)
    {
        string parity;

        Console.WriteLine("Available Parity options:");
        foreach (string s in Enum.GetNames(typeof(Parity)))
        {
            Console.WriteLine("   {0}",s);
        }

        Console.Write("Parity({0}):",defaultPortParity.ToString());
        parity = Console.ReadLine();

        if (parity == "")
        {
            parity = defaultPortParity.ToString();
        }

        return (Parity)Enum.Parse(typeof(Parity),parity);
    }

    public static int SetPortDataBits(int defaultPortDataBits)
    {
        string dataBits;

        Console.Write("Data Bits({0}): ",defaultPortDataBits);
        dataBits = Console.ReadLine();

        if (dataBits == "")
        {
            dataBits = defaultPortDataBits.ToString();
        }

        return int.Parse(dataBits);
    }

    public static StopBits SetPortStopBits(StopBits defaultPortStopBits)
    {
        string stopBits;

        Console.WriteLine("Available Stop Bits options:");
        foreach (string s in Enum.GetNames(typeof(StopBits)))
        {
            Console.WriteLine("   {0}",s);
        }

        Console.Write("Stop Bits({0}):",defaultPortStopBits.ToString());
        stopBits = Console.ReadLine();

        if (stopBits == "")
        {
            stopBits = defaultPortStopBits.ToString();
        }

        return (StopBits)Enum.Parse(typeof(StopBits),stopBits);
    }

    public static Handshake SetPortHandshake(Handshake defaultPortHandshake)
    {
        string handshake;

        Console.WriteLine("Available Handshake options:");
        foreach (string s in Enum.GetNames(typeof(Handshake)))
        {
            Console.WriteLine("   {0}",s);
        }

        Console.Write("Handshake({0}):",defaultPortHandshake.ToString());
        handshake = Console.ReadLine();

        if (handshake == "")
        {
            handshake = defaultPortHandshake.ToString();
        }

        return (Handshake)Enum.Parse(typeof(Handshake),handshake);
    }
}

解决方法

我会使用 SerialPort.DataReceived 而不是在新线程中循环 Read()

创建与 Arduino 的连接时,添加此项。

_serialPort.DataReceived += dataReceived;

然后,添加SerialDataReceivedEventHandler

private void dataReceived(object sender,SerialDataReceivedEventArgs e) 
{
    string message = _serialPort.ReadLine();
    Console.WriteLine(message);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。