C# 将不断读取的串行缓冲区数据传递给另一个类时出现问题
长话短说; 我有一个名为Scope的类。该类包含作用域操作等的所有逻辑。它还启动反向线程,该线程在我的事件不可靠的情况下不断读取串行端口数据:C# 将不断读取的串行缓冲区数据传递给另一个类时出现问题,c#,serial-port,C#,Serial Port,长话短说; 我有一个名为Scope的类。该类包含作用域操作等的所有逻辑。它还启动反向线程,该线程在我的事件不可靠的情况下不断读取串行端口数据: Thread BackgroundReader = new Thread(ReadBuffer); BackgroundReader.IsBackground = true; BackgroundReader.Start(); private void ReadBuffer() { Ser
Thread BackgroundReader = new Thread(ReadBuffer);
BackgroundReader.IsBackground = true;
BackgroundReader.Start();
private void ReadBuffer()
{
SerialPort.DiscardInBuffer();
while (!_stopCapture)
{
int bufferSize = SerialPort.BytesToRead;
byte[] buffer = new byte[bufferSize];
if(bufferSize > 5)
{
SerialPort.Read(buffer, 0, bufferSize);
Port_DataReceivedEvent(buffer, null);
}
Thread.Sleep(_readDelay);
}
CurrentBuffer = null;
}
在Scope类中有一个名为Buffer的公共字段
public byte[] Buffer
{
get
{
return CurrentBuffer;
}
}
这里是在读取新数据时触发的事件
private void Port_DataReceivedEvent(object sender, EventArgs e)
{
//populate buffer
Info(sender, null);
CurrentBuffer = ((byte[])sender);
foreach(byte data in CurrentBuffer)
{
DataBuffer.Enqueue(data);
}
if (DataBuffer.Count() > _recordLength)
{
GenerateFrame(DataBuffer.ToArray());
DataBuffer.Clear(); ;
}
}
为了使代码更易于管理,我将其分为几个类。其中一个类用于搜索当前流中的特定数据模式,并从该数据创建特定对象。此代码的工作方式是发送特定于串行端口的命令,并期望返回帧。如果未收到响应或不正常,则会反复执行发送,直到收到正确的响应,否则将超时。响应应位于当前缓冲区中。这些奇怪的字符串操作是为了调试目的
public class GetAcknowledgedFrame
{
byte[] WritedData;
string lastEx;
string stringData;
public DataFrame WriteAcknowledged(Type SendType, Type ReturnType, JyeScope scope)
{
var stopwatch = new Stopwatch();
stopwatch.Restart();
while (stopwatch.ElapsedMilliseconds < scope.TimeoutTime)
{
try
{
if (SendType == typeof(GetParameters))
{
WriteFrame(new ScopeControlFrames.GetParameters(), scope.SerialPort);
}
else if(SendType == typeof(GetConfig))
{
WriteFrame(new ScopeControlFrames.GetConfig(), scope.SerialPort);
}
else if (SendType == typeof(EnterUSBScopeMode))
{
WriteFrame(new ScopeControlFrames.EnterUSBScopeMode(), scope.SerialPort);
}
return ReturnFrame(ReturnType, scope.Buffer, scope.TimeoutTime);
}
catch (InvalidDataFrameException ex)
{
lastEx = ex.Message;
System.Threading.Thread.Sleep(10);
}
}
stringData = "";
foreach (var data in scope.Buffer)
{
stringData += data + ",";
}
stringData.Remove(stringData.Length - 1);
throw new TimeoutException($"Timeout while waiting for frame acknowledge: " + SendType.ToString() + ", " + ReturnType.ToString() + Environment.NewLine+ "Add. err: "+lastEx);
}
private DataFrame ReturnFrame(Type FrameType, byte[] buffer, int timeoutTime)
{
if (FrameType == typeof(DataFrames.DSO068.CurrConfigDataFrame))
{
DataFrames.DSO068.CurrConfigDataFrame CurrConfig = new DataFrames.DSO068.CurrConfigDataFrame(buffer);
return CurrConfig;
}
else if (FrameType == typeof(DataFrames.DSO112.CurrConfigDataFrame))
{
DataFrames.DSO112.CurrConfigDataFrame CurrParam = new DataFrames.DSO112.CurrConfigDataFrame(buffer);
return CurrParam;
}
else if (FrameType == typeof(CurrParamDataFrame))
{
CurrParamDataFrame CurrParam = new CurrParamDataFrame(buffer);
return CurrParam;
}
else if (FrameType == typeof(DataBlockDataFrame))
{
DataBlockDataFrame CurrData = new DataBlockDataFrame(buffer);
return CurrData;
}
else if (FrameType == typeof(DataSampleDataFrame))
{
DataSampleDataFrame CurrData = new DataSampleDataFrame(buffer);
return CurrData;
}
else if (FrameType == typeof(ScopeControlFrames.ScopeReady))
{
ScopeControlFrames.ScopeReady ready = new ScopeControlFrames.ScopeReady(buffer);
return ready;
}
else
{
throw new InvalidOperationException("Wrong object type");
}
}
private bool WriteFrame(DataFrame frame, IStreamResource port)
{
WritedData = frame.Data;
port.Write(frame.Data, 0, frame.Data.Count());
return true;
}
}
问题是当我将这个线程在后台工作的对象传递给我的助手类时。助手类似乎看不到此对象中更改的数据。当我将助手类的代码与主类分离时,问题就开始了。
我的问题是:
-我知道对象是通过引用传递的,这意味着当对象动态地改变它的状态时,在这种情况下,当接收到新数据时,数据缓冲区应该改变,所有引用该对象的类也会看到这种改变。也许我错过了什么?
-我试着按引用传递数组,数组也是引用类型。但这对我毫无帮助。也许我错过了什么?
我试着把这个类改为static,但没有用。
非常感谢您的帮助。下面的代码
Info(sender, null);
CurrentBuffer = ((byte[])sender);
正在创建名为CurrentBuffer的新引用变量。在重置CurrentBuffer时,在这行代码之前持有指向CurrentBuffer值的引用“指针”的任何其他代码都不会获得CurrentBuffer的新值
Info(sender, null);
CurrentBuffer = ((byte[])sender);