Need to read data from serial port. How using MVVM can notify View about the process of working with the serial port? There is no guarantee that the data will come well .. and in the model, for example, the GetData method how to issue if the data is read in a separate method of the timer tick?

  • I anticipate the questions of other participants, so - can you get more information and examples? - UporotayaPanda
  • can't figure out how to work with data stream using MVVM - Mike Waters
  • I'm currently working on a project where reading is via serialPort, in the model I have implemented a method for receiving data. And the timer reading itself is implemented in VM. For example, I have a timer that checks the system time every 10 seconds. Every 10 seconds I call this method and then update the data in the VM. What exactly is the question? What do you mean by data flow? - UporotayaPanda
  • @UporotayaPanda can be an example? - Mike Waters
  • @UporotayaPanda timer should not be in the model? - Mike Waters

1 answer 1

Example:

In the constructor, I create a timer and bind the desired method to it:

_timer = new Timer(); _timer.Interval = 5000; _timer.AutoReset = true; _timer.Elapsed += ReadClock; _timer.Start(); 

Here is the implementation of the method:

 private void ReadClock(object source, ElapsedEventArgs e) { if(SelectedPort != null) { var dateTime = SystemClock.Get(); if (dateTime != null) { Time = dateTime.ToLongTimeString(); Date = dateTime.ToShortDateString(); } } } 

My error trapping is implemented at a lower level, and I do not need to worry about the correctness of the data.

In the SystemClock.Get method, I have read from SerialPort .

  • _timer in viewmodel? - Mike Waters
  • @MikeWaters in my case - yes. I have a library where implemented functionality for working with an external system. And how to use this functionality I install in VM. - UporotayaPanda
  • it turns out Model stores only methods for work and data is obtained in ViewModel? - Mike Waters
  • In this case, yes. At me so from DateTime turns out from system and then necessary necessary is updated. - UporotayaPanda pm
  • @MikeWaters if this does not help to find a solution, then update the question by adding a code with a description, and I will try to help you. - UporotayaPanda