Question How to retrieve data while app is running and app in background

miladel

Member
Joined
Aug 16, 2020
Messages
8
Programming Experience
Beginner
Dear All,
In my project, I need to connect to S7 PLC and read data in cycles like every 100ms, then update variables and put them in Database. The app based on UWP and what I need to know is that how to use all the updated values from PLC in all of the code behinds,
what is the best method for my question?
here is a simple part of the code:

code in page1:
       Plc plc = new Plc(CpuType.S7300, "192.168.0.10", 0, 2); // connection to PLC
       DispatcherTimer dispatcherTimer; // Timer to work on cycle
        public void DispatcherTimerSetup()
        {
            dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += dispatcherTimer_Tick;
            dispatcherTimer.Interval = new TimeSpan(1000000);
            dispatcherTimer.Start();
        }
        void dispatcherTimer_Tick(object sender, object e)
        {
            updateDb5();
        }
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            plc.Open(); // start connection to PLC it could be starts with page load or a button
        }

        bool value1; // the value which is necessary to access in all pages 
        private void updateDb5()
        {
            var db5 = new Db5(); // DB5 in PLC
            plc.ReadClass(db5, 5);
            value1 = db5.oneSecBlink; // An example bool in PLC DB
        }

In the code I need to access to the value1 in all of the pages in my project and store it in SQL Database, Also it is necessary to keep connection alive while the app is running in background.
thanks in advance
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,409
Location
Chesapeake, VA
Programming Experience
10+
Do you really realistically have to pull data from the PLC at 10 times a second? If so, are you sure that you can accomplish all your data reads from the PLC, writes to the database, and update the UI all within 100ms? Even though the UWP DispatchTimer is better than the WinForms timer, I'm not quite sure if it can give you that kind of precision guarantee even if you could accomplish all your work in less than 100ms. What if there's some other thing that adds another task into the dispatch timer and that task takes more than 100ms to complete?

My recommendation is doing the UI updates in the UI thread with the dispatch at a more reasonable human reaction rate of about every 300ms. Do you PLC reads and database updates in tight loop running in a dedicated thread where you either use Task.Delay() or do a busy wait where the amount of time you delay or busy wait is based against the current clock or a stopwatch instance.
 

miladel

Member
Joined
Aug 16, 2020
Messages
8
Programming Experience
Beginner
Do you really realistically have to pull data from the PLC at 10 times a second? If so, are you sure that you can accomplish all your data reads from the PLC, writes to the database, and update the UI all within 100ms? Even though the UWP DispatchTimer is better than the WinForms timer, I'm not quite sure if it can give you that kind of precision guarantee even if you could accomplish all your work in less than 100ms. What if there's some other thing that adds another task into the dispatch timer and that task takes more than 100ms to complete?

My recommendation is doing the UI updates in the UI thread with the dispatch at a more reasonable human reaction rate of about every 300ms. Do you PLC reads and database updates in tight loop running in a dedicated thread where you either use Task.Delay() or do a busy wait where the amount of time you delay or busy wait is based against the current clock or a stopwatch instance.
Dear Skydiver,
As you had truly mentioned, it is not a good idea to pull data 10 times a second, I had test this period before, it was working, but in my test there were no database updates. Finally I set the timer to 500ms so I think there would not be a big issue to update the values from PLC and about the database. data must not put in database in each reading. 100ms here is just an example. Actually I have no task and I need to add some async methods. about the UI updates in the UI thread, would you please give an example cause I am beginner and I have no idea how to put it in my code. really thanks.
 
Top Bottom