简体   繁体   English

WPF,C#,串行端口通信

[英]WPF, C#, Serial Port Communication

I am using serial port communication, there is a DataReceived Event of Serial Port, in which if the header & footer of received data matches I am executing 2 complex & lengthy functions, here I have used circular buffer for data receive, 我正在使用串行端口通信,有一个串行端口的DataReceived事件,其中如果接收到的数据的页眉和页脚匹配,我正在执行2个复杂而冗长的函数,这里我使用了循环缓冲区进行数据接收,

Out of the 2 functions first function updates a Graph (Area Chart) of 2058 bytes on Canvas & second functions does some complex calculations on 2058 bytes. 在这两个函数中,第一个函数在Canvas上更新2058字节的图形(面积图),第二个函数对2058字节进行一些复杂的计算。 I am receiving these 2058 bytes after every 3 seconds. 我每3秒就会收到这2058个字节。

So my requirement is while I am filling data in buffer on the other side I need to execute these 2 functions on the data that is already in the buffer (as it is circular buffer it contains previously filled data). 所以我的要求是,当我在另一侧将数据填充到缓冲区中时,我需要对缓冲区中已有的数据执行这两个功能(因为它是循环缓冲区,它包含先前填充的数据)。

I am little bit confused here, how to achieve this concurrency. 我在这里有点困惑,如何实现这种并发。 I know some ways, 我知道一些方法

  1. use 'Task' 使用“任务”
  2. use 'Threads' 使用“线程”
  3. use 'async & await' 使用“异步并等待”
  4. use 'Task Parallel Library' 使用“任务并行库”
  5. use 'Background Worker' 使用“后台工作者”
  6. use 'Dispatcher.Invoke()' 使用'Dispatcher.Invoke()'

Currently I am using Dispatcher.Invoke() which takes too much time for UI updates. 当前,我正在使用Dispatcher.Invoke() ,它花费太多时间进行UI更新。 So here time lag happens. 因此,这里会发生时滞。

Please suggest me which approach will be more responsive. 请建议我哪种方法会更灵敏。

Judging from the size of work to be done, and since this is happening every three seconds, I believe that tasks and queueing work on the threadpool are your best solutions. 从要完成的工作量来看,由于每三秒钟就会发生一次,因此我认为线程池上的任务和排队工作是您最好的解决方案。

I would not suggesting spawning a new dedicated thread. 我不建议产生一个新的专用线程。

Depending on how long this process takes and your configuration, I think you may consider having a small number of permanent threads running in a loop and querying a queue of newly received data for processing (ie creating your own threadpool). 根据此过程需要多长时间和您的配置,我认为您可以考虑让少量永久线程循环运行并查询新接收的数据队列以进行处理(即创建自己的线程池)。 This can be accomplished using ConcurrentQueue to post and receive data. 这可以使用ConcurrentQueue来发布和接收数据来完成。 Also you may benefit from considering TPL.Dataflow which can help immensely in situations that require low latency and high performance in multithreaded environment. 另外,您还可以考虑考虑TPL.Dataflow ,它可以在多线程环境中要求低延迟和高性能的情况下提供极大帮助。 Regardless of the solution, you may benefit from investigating the BufferBlock class from Dataflow . 无论采用哪种解决方案,都可以从Dataflow中研究BufferBlock类中受益。

You can use 1-5 for complex work (as those are intended for this scenario). 您可以将1-5用于复杂的工作(因为这种情况适用于此情况)。 You could use 6 to inject your results into the graph, because the purpose of Dispatcher.Invoke is processing work in the UI-Thread and is required for the majority of controls. 您可以使用6将结果注入到图形中,因为Dispatcher.Invoke的目的是在UI-Thread中处理工作,并且对于大多数控件都是必需的。

I hope that helps. 希望对您有所帮助。

I have managed it successfully with timers and background worker synchronization. 我已经通过计时器和后台工作人员同步成功管理了它。

The only important thing I noted is you need a good processor, at least 'core - i3'. 我注意到的唯一重要的事情是您需要一个好的处理器,至少是'core-i3'。

The solutions proposed by Mr.SKleanthous is also acceptable. SKleanthous先生提出的解决方案也是可以接受的。 Thanks. 谢谢。

And thanks Mr. Andreas Müller, yes I got your point. 谢谢安德烈亚斯·穆勒先生,是的,我明白你的意思。

Thank you so much everybody. 非常感谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM