如何与serialport . readeexisting()比较字符串

本文关键字:比较 字符串 readeexisting serialport | 更新日期: 2023-09-27 18:16:41

是否有一种方法可以比较声明为的字符串与从串行端口接收的数据,例如:

string hello = "hello";
string dataReceived = serialPort1.ReadExisting();  //the incoming data is "hello"
bool comparisonResult = hello.Equals(dataReceived, StringComparison.Ordinal);
if(comparisonResult == true)
{
    //do something
}

提前感谢!

如何与serialport . readeexisting()比较字符串

是有办法的

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

编辑:找到了它不起作用的原因,我需要的是文化敏感的比较

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
}