首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模拟串口收发数据

模拟串口收发数据
EN

Stack Overflow用户
提问于 2014-01-16 00:20:17
回答 1查看 525关注 0票数 0

我写了这段代码来模拟应用程序的串口读写。

我想每3毫秒发送一次数据,在发送之前,我将数据记录到一个图表和一个文件中。另外,在发送数据之后,将调用DataRecieved函数在图表上描述数据并将数据记录到文件中。

但是当我执行它时,在某些点上会显示错误的结果,而且它也不能每3毫秒执行一次。(有时每6毫秒,...,输出和输入的文件会被附加)

此外,它有时会在我写入文件行中抛出以下错误:

对象引用未设置为对象的实例。

我能做些什么来解决这个问题?

代码语言:javascript
复制
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;
    }

}

附加文件:

data Receiving file

data sending file

time between sendings

EN

回答 1

Stack Overflow用户

发布于 2014-01-16 00:27:36

您将无法从.NET计时器获得3毫秒的分辨率。例如,请参阅Why are .NET timers limited to 15 ms resolution?

你可以使用一个专门的线程来产生Thread.Sleep (精度也不是很高),或者如果精度真的很重要的话,也可以使用Thread.SpinWait

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21142671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档