我正在通过串口向C#发送文本。此文本包含\ r \ n,它将成为超级终端中的新行。现在我想在C#中读取这个\ r \ n。我怎么能这样做?
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");
}
}
答案 0 :(得分:1)
如果您正在使用ReadLine()
,它将读取到行转义符,这可以是\ r,\ n或\ r \ n \ n \ n \ n因此,使用ReadLine()
命令可以自动检测回车符并从读取字符串中删除它
https://msdn.microsoft.com/en-us/library/system.io.streamreader.readline%28v=vs.110%29.aspx
如果由于某种原因需要保留回车符,则可以使用Read()
命令
https://msdn.microsoft.com/en-us/library/ath1fht8%28v=vs.110%29.aspx