Skip to main content
added 224 characters in body
Source Link
Loreno
  • 143
  • 4

//EDIT2 If someone wants to have a look at the whole picture: here's the code of the application: https://1drv.ms/f/s!Atl-jM_xUkz1hf9DOeTqVWi0JKOU0g It's not all of it, I uploaded only 2 files, which are most important

//EDIT2 If someone wants to have a look at the whole picture: here's the code of the application: https://1drv.ms/f/s!Atl-jM_xUkz1hf9DOeTqVWi0JKOU0g It's not all of it, I uploaded only 2 files, which are most important

added 709 characters in body
Source Link
Loreno
  • 143
  • 4

//EDIT I just tried running my application on a Windows Phone - it turns out that it doesn't work as good as on PC. On my Page I have a SyncFusion chart, which displays the data that is sent via bluetooth. It turns out, that on a mobile device displaying the graph (with dynamic data) and receiving data in the background is not so smooth. What should I do about it? I tried using a Timer, instead of running Async() in the Task. Even though my Timer has interval of 20ms it's not enough to display incoming data in time - there's is a noticeable delay (external device sends data at the interval of 400ms, so I don't know why 20ms is not enough for the phone to catch up). Do you have any ideas?

//EDIT I just tried running my application on a Windows Phone - it turns out that it doesn't work as good as on PC. On my Page I have a SyncFusion chart, which displays the data that is sent via bluetooth. It turns out, that on a mobile device displaying the graph (with dynamic data) and receiving data in the background is not so smooth. What should I do about it? I tried using a Timer, instead of running Async() in the Task. Even though my Timer has interval of 20ms it's not enough to display incoming data in time - there's is a noticeable delay (external device sends data at the interval of 400ms, so I don't know why 20ms is not enough for the phone to catch up). Do you have any ideas?

Source Link
Loreno
  • 143
  • 4

Optimizing algorithm of analyzing incoming data

I've been making an app, which gets data from external device (via bluetooth) and displays received data. I'm still pretty new to programming and I'm struggling to finish this app.

I wrote some code, that works well, but I wanted to ask you if my solution can be considered as a good one.

Firstly, some background. External device sends informations in a format like this: "D1x*" - where 'x' can be '1' or '0'. Unfortunately I'm not getting the whole message at once. What I mean by that is the fact, that very often I get messages like: "D1", "1*", and so on. So, in order to interprete the data, I need to wait until the whole frame is received. That's why i created a string called appData.readBuffer. Everytime I receive a message, I add it there. I do it like this:

public void OnMessageReceived(object source, string message)
{
    appData.readBuffer += message;
}

So, that's how I receive the messages. Now, the other part -the one where I interprete the data. In order to display the data almost instantly after it's sent, I created a Task, which runs my Analyze function in an infinite loop:

        Task AnalyzerTask = Task.Run(() =>
        {
            while (true)
            {
                Analyze();
            }
        });

The above code is called at the end of my Page constructor.

Now here's the Analyze method:

   public void Analyze()
    {
            string oneMessage = "";

            if ((appData.readBuffer.Contains("A") || appData.readBuffer.Contains("D")) && appData.readBuffer.Contains("*"))
            {
                int indexOfStar = appData.readBuffer.IndexOf('*');
                int indexOfA = appData.readBuffer.IndexOf('A');
                int indexOfD = appData.readBuffer.IndexOf('D');

                if (indexOfA < indexOfStar || indexOfD < indexOfStar)
                {
                    int startIndex;
                    if (indexOfA < indexOfD)
                        startIndex = indexOfA;
                    else
                        startIndex = indexOfD;

                    if (startIndex < 0)
                        return;
                    oneMessage = appData.readBuffer.Substring(startIndex, indexOfStar + 1);
                    appData.readBuffer = appData.readBuffer.Remove(0, indexOfStar + 1);
                }
            }
            else
                return;

            if (oneMessage == "")
                return;

            if (oneMessage[0] == 'D')
            {
                if (oneMessage[1] == '1')
                {
                    if (oneMessage[2] == '1')
                    {
                        Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => {
                            DI1TB.Text = "1";
                        });  
                    }
                    else if (oneMessage[2] == '0')
                    {
                        Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => {
                            DI1TB.Text = "0";
                        });
                    }
                }
            }
}

I think that it's not really that important to descibe how this function exactly works. It analyzes data and sets Text property of DI1TB TextBlock.

So what I'm asking for is opinions of yours (and maybe some proposals) of what could be improved, what is wrong. As I said, I'm not an experienced programmer and this is just a side project of mine, which I hope will help me to develop some skills. I also hope you could help me with that, instruct me a little.