似乎无法正常工作。我已经使用其他程序测试了硬件,并且可以正常工作,但是此代码遇到了间歇性问题,即没有数据返回,因此超时。我试过ReadExisting具有同步和异步的相同结果。与ReadExisting有关,我是否应该使用其他功能?
private StringBuilder serialBuffer = new StringBuilder();
private const string terminationSequence = "\r";
private void DataReceived(object sender, SerialDataReceivedEventArgs e)
{
var data = Serial.ReadExisting();
serialBuffer.Append(data);
var bufferString = serialBuffer.ToString();
int index;
do
{
index = bufferString.IndexOf(terminationSequence, StringComparison.Ordinal);
if (index <= -1) continue;
IncomingData = bufferString.Substring(0, index);
bufferString = bufferString.Remove(0, index + terminationSequence.Length);
} while (index > -1);
serialBuffer = new StringBuilder(bufferString);
}
我放入了一些痕迹,似乎事件没有触发...这是输出,并且事件在超时之前没有开始。