0

I have added a couple of break points in my code to monitor the value of two variables when reached. The application basically receives a stream of data at 9600 baud from serial port generated by a micro controller working at 500 Hz and has to filter the messages according to certain rules "if" and "if else" to strip out header characters and address them to other variables for calculation usage. Here is the code:

 class Stripper
{
 public  void  Distri (string inComing, out string param1, out string param2, out string param3, out string param4)
    {
        string currentRes="";
        string currentMot = "";
        string temperature="";
        string numRPM="";
        string f = inComing;
        if (inComing.Length < 6)
        {
            f = ">123456<";
        }
        char firstChar = f[0];
        char lastChar = f[f.Length - 2];
        bool test1 =(firstChar.Equals('>'));
        bool test2 =(lastChar.Equals('<'));
        int messLenght = f.Length;

        try
        {
            if (test1 == true && test2 == true && messLenght <= 10)
            {
                f = f.Replace("<", "");
                f = f.Replace(">", "");

                if (f[0] == 'I')
                {
                    string _currentRes = f;
                    _currentRes = _currentRes.Replace("I", "");
                    currentRes = _currentRes;
                }

                else if (f[0] == 'M')
                {
                    string _currentMot = f;
                    _currentMot = _currentMot.Replace("M", "");
                    currentMot = _currentMot;
                }

                else if (f[0] == 'T')
                {
                    string _temperature = f;
                    _temperature = _temperature.Replace("T", "");
                    temperature = _temperature;
                }
                else if (f[0] == 'N')
                {
                    string _numRPM = f;
                    _numRPM = _numRPM.Replace("N", "");
                    numRPM = _numRPM;
                }

                else
                { }
            }

            else
            { }
        }
        catch (System.Exception)
        {

            throw;
        }

        param1 = currentRes;
        param2 = temperature;
        param3 = numRPM;
        param4 = currentMot;

        }
       }
      }

The problem I am facing is quite strange and at some point I was completely lost. Basically with the break points active on variable "f" and "inComing" the app was immediately unresponsive and, to make it work, I had to reduce the speed of the stream from serial to 1/100 introducing delays. Without the break points the app can take the full stream of data without any problem. May be this experience could also help other people in similar situations. Looks like the break points are slowing down the process quite dramatically.I do not think it has something to do with the pc I am using being this a monster with 16 Gb RAM and 2.4 Ghz processor i5. I am wondering why this is happening and if there a way to avoid such problem rather than not using the break points?

FeliceM
  • 4,163
  • 9
  • 48
  • 75
  • 2
    you could create an Logfile as alternative to breakpoints – WiiMaxx Jun 17 '13 at 09:47
  • Thanks for the advice. It is for sure a great alternative but does not give a real time vision of what is happening. However, I did not considered it before and I will try it. – FeliceM Jun 17 '13 at 09:50
  • you could also write to your output window in VS but i don't know how to do it i just now you can do it :D – WiiMaxx Jun 17 '13 at 09:52
  • [`Trace.TraceInformation`](http://msdn.microsoft.com/en-us/library/64tdffaz.aspx)? – Uwe Keim Jun 17 '13 at 09:55
  • The posted code is very unlikely to be related to the problem. The typical issue is using the DataReceived event and calling Begin/Invoke too often. Pummeling the UI thread with invoke requests and making it go catatonic when it can't keep up and stops painting and responding to user input. Which you fix by making the DataReceived event doing more work, collecting the entire device response before invoking. Ignoring the SerialPort.Read() return value is also a classic mistake. None of this is visible in the snippet. – Hans Passant Jun 17 '13 at 10:28
  • @HansPassant, thanks Hans. I tought the same but the problem is occurring only if I have break points active as explained in my post. Without the break points the snipet is working fine with the full stream of data. I am looking at the relation betwwen the break point and the problem. To find a solution I need to understand how the breake points interact with the process. – FeliceM Jun 17 '13 at 10:53
  • Breakpoints induce no overhead at all unless you made them conditional. – Hans Passant Jun 17 '13 at 10:56
  • @HansPassant, I accept what you say but is a fact that with or without break points I have a difference and I do not understand why. With the same stream of data in the first case the app freezes in the second case, without break points, it is working fine. – FeliceM Jun 17 '13 at 11:09

1 Answers1

1

I found it on an quick search Debug.WriteLine()
is the thing to write to your VS Output Window

so you will be able to get your realtime values

see also this link

Community
  • 1
  • 1
WiiMaxx
  • 5,322
  • 8
  • 51
  • 89
  • Thank you. Very much appreciated but still I do not understand why the break points are causing such problem. – FeliceM Jun 17 '13 at 10:00
  • maybe based on the "normal" use of it so it tries to to get all information of the Value probably per reflection (but i don't know) – WiiMaxx Jun 17 '13 at 10:04