c# - 如何使用serialPort.ReadExisting()比较字符串

时间:2016-11-07 03:25:33

标签: c# serial-port string-comparison

有没有办法比较声明为的字符串与从串口接收的数据,例如:

string hello = "hello";
string dataReceived = serialPort1.ReadExisting();  //the incoming data is "hello"
bool comparisonResult = hello.Equals(dataReceived, StringComparison.Ordinal);

if(comparisonResult == true)
{
    //do something
}

提前致谢!

2 个答案:

答案 0 :(得分:0)

是的,有办法

if (serialPort1.ReadExisting() == "hello")
{
  // do something
}

答案 1 :(得分:0)

编辑:找出它为什么不起作用,我需要的是文化敏感的比较

string hello = "hello";
string dataReceived = serialPort1.ReadExisting();  //incoming data is "hello"
int comparisonResult = String.Compare(hello, dataReceived, true);
//if comparisonResult is true, output is 0

if (comparisonResult == 0)
{
     //do something
}