我已经编写了这段代码来模拟应用程序的串口读写。
我想每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;
}
}
附件:
答案 0 :(得分:1)
您将无法从.NET计时器获得3 ms的分辨率。例如,请参阅Why are .NET timers limited to 15 ms resolution?。
如果准确度非常重要,您可以使用专用线程产生Thread.Sleep
(也不是很高的准确度),或者可能Thread.SpinWait
。