简体   繁体   English

C#SerialPort.ReadLine与终端串行读取的区别

[英]C# SerialPort.ReadLine difference to terminal serial read

What are the differences between this C# code running inside Unity: 在Unity内部运行的C#代码之间有什么区别:

    SerialPort stream = new SerialPort ( "/dev/cu.usbserial-A9Y59ZJV", 9600 );
    stream.ReadTimeout = 1000;
    stream.Open();

    while( stream.IsOpen )
    {
        try
        {
            Debug.Log( stream.ReadLine() );
        }
        catch (TimeoutException e)
        {
            Debug.LogError (name + " timed out reading from Arduino: " + e);
        }
    }
}

and running the Unix terminal command: 并运行Unix终端命令:

screen /dev/cu.usbserial-A9Y59ZJV 9600 –L

The C# Code throws TimeoutExceptions. C#代码引发TimeoutExceptions。 The Terminal command reads data (it comes in ever second) Terminal命令读取数据(每秒输入一次)

The two are not ran at the same time. 两者不能同时运行。 The unity code is in reality split into Start() and Update() messages properly. 实际上,统一代码正确地分为Start()和Update()消息。

I realised my mistake while writing the question, but am answering incase someone else has the same problem. 我在写问题时意识到自己的错误,但是在回答别人遇到相同问题的情况。

Be careful with the difference between "\\r\\n" and "\\n". 注意“ \\ r \\ n”和“ \\ n”之间的区别。 C#s SerialPort expects different values depending on platform . C#的SerialPort 期望不同的值取决于平台

If you're sending data without the receiving platforms version of a newline, you cannot use ReadLine . 如果您发送的数据没有接收平台的换行符版本,则不能使用ReadLine Use ReadByte instead. 请改用ReadByte

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

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