我编写了这段代码来模拟应用程序的串行端口读写。
我想每 3 毫秒发送一次数据,在发送之前我将数据记录到图表和文件中。同样在发送数据后,调用 DataRecieved 函数以在图表上描绘数据并将数据记录到文件中。
但是当我执行它时,它会在某些时候显示错误的结果,而且它也不能每 3 毫秒执行一次。(有时每 6 毫秒,...,输出和输入的文件已附加)
有时它也会在我写入文件的行上抛出此错误:
你调用的对象是空的。
我能做些什么来解决它?
class SignalControllerSimulator
{
public SignalControllerSimulator(SignalReaderSimulator reader, SignalWriterSimulator writer, LineSeries PitchInputLine, LineSeries RollInputLine, LineSeries YawInputLine, LineSeries PitchOutputLine, LineSeries RollOutputLine, LineSeries YawOutputLine)
{
....
//do some initialization
SentFileLogger = new WriteFileLogger(SentFolderName);
RecFileLogger = new ReadFileLogger(RecFolderName);
SentFileLogger.Open(true);
RecFileLogger.Open(true);
rampTime = SenarioTime = SineTime = StepTime = 320;//1000ms
reader.DataReceived += DataReceived;
}
#region readSection
ObservableCollection<ChartItem> PitchInputItems = new ObservableCollection<ChartItem>();
ObservableCollection<ChartItem> RollInputItems = new ObservableCollection<ChartItem>();
ObservableCollection<ChartItem> YawInputItems = new ObservableCollection<ChartItem>();
int PitchIndex = 1; int RollIndex = 1; int YawIndex =1 ;
public void DataReceived(ReadSignal signal)
{
this.PitchInputLine.Dispatcher.Invoke(new Action(() =>
{
PitchInputItems.Add(new ChartItem(signal.PitchLocation, PitchIndex++));
RollInputItems.Add(new ChartItem(signal.RollLocation, RollIndex++));
YawInputItems.Add(new ChartItem(signal.YawLocation, YawIndex++));
PitchInputLine.ItemsSource = PitchInputItems;
RollInputLine.ItemsSource = RollInputItems;
YawInputLine.ItemsSource = YawInputItems;
}));
RecFileLogger.Write(true, signal.PitchLocation, signal.RollLocation, signal.YawLocation, DateTime.Now.ToString("h:m:s:fff"));
}
public void Stop()
{
...
}
#endregion
#region writeSection
public void StartSendingLocations()
{
EndIndex = setEndIndex();
timer = new System.Timers.Timer(interval);
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Start();
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (Index>=EndIndex)
{
Stop();
return;
}
...
// some switch case and function calling...
CreateCommand();
//setting reader settings
//log to file the data sent:
SentFileLogger.Write(true, writer.WSignal.PitchLocation, writer.WSignal.PitchAngularVelocity, writer.WSignal.RollLocation,
writer.WSignal.RollAngularVelocity, writer.WSignal.YawLocation, writer.WSignal.YawAngularVelocity,
DateTime.Now.ToString("h:m:s:fff"));
SignalWriter_DataSent(writer.WSignal);
TimeWriter.WriteLine("end:------------>" + DateTime.Now.ToString("h:m:s:fff"));
TimeWriter.WriteLine();
reader.ThreadMain(reader.RSignal);
Index++;
}
ObservableCollection<ChartItem> PitchOutputItems = new ObservableCollection<ChartItem>();
ObservableCollection<ChartItem> RollOutputItems = new ObservableCollection<ChartItem>();
ObservableCollection<ChartItem> YawOutputItems = new ObservableCollection<ChartItem>();
int PitchIndex1 = 1; int RollIndex1 = 1; int YawIndex1 = 1;
void SignalWriter_DataSent(WriteSignal signal)
{
RollInputLine.Dispatcher.Invoke(new Action(() =>
{
PitchOutputItems.Add(new ChartItem(signal.PitchLocation, PitchIndex1++)); //PitchOutputItems.Add(new ChartItem(signal.PitchLocation, interval * PitchIndex1++));
RollOutputItems.Add(new ChartItem(signal.RollLocation,RollIndex1++)); //RollOutputItems.Add(new ChartItem(signal.RollLocation, interval * RollIndex1++));
YawOutputItems.Add(new ChartItem(signal.YawLocation,YawIndex1++)); //YawOutputItems.Add(new ChartItem(signal.YawLocation, interval * YawIndex1++));
PitchOutputLine.ItemsSource = PitchOutputItems;
RollOutputLine.ItemsSource = RollOutputItems;
YawOutputLine.ItemsSource = YawOutputItems;
}));
}
private int setEndIndex()
{
return EndTime / interval;
}
}
附加的文件: