简体   繁体   English

读取\\ r在C#中通过串口发送?

[英]Read \r sent by serial port in C#?

I'm sending a text by serial port to C#. 我正在通过串行端口向C#发送文本。 This text contains \\r and it will become a new line in HyperTerminal. 该文本包含\\ r,它将在超级终端中换行。 Now I want to read this \\r in C#. 现在,我想用C#读取\\ r。 How can I do this? 我怎样才能做到这一点?

    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        RxString = serialPort1.ReadLine();

        this.Invoke(new EventHandler(DisplayText));
    }

    private void DisplayText(object sender, EventArgs e)
    {
        string str = RxString;

        if(str == "\r")
        {
            MessageBox.Show("This is a new line");
        }
    }

If you are using ReadLine() it will read to the line escape, this can be \\r, \\n or \\r\\n so by using the ReadLine() command you are automatically detecting your carriage return and removing it from the read string 如果您使用的是ReadLine() ,它将读取到换行符,可以是\\ r,\\ n或\\ r \\ n,因此,通过使用ReadLine()命令,您将自动检测回车符并将其从读取的字符串中删除

https://msdn.microsoft.com/en-us/library/system.io.streamreader.readline%28v=vs.110%29.aspx https://msdn.microsoft.com/en-us/library/system.io.streamreader.readline%28v=vs.110%29.aspx

if for some reason you need to preserve the carriage return then you can use the Read() command 如果由于某种原因需要保留回车符,则可以使用Read()命令

https://msdn.microsoft.com/en-us/library/ath1fht8%28v=vs.110%29.aspx https://msdn.microsoft.com/en-us/library/ath1fht8%28v=vs.110%29.aspx

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

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