Click here to Skip to main content
15,922,533 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I code with C# to drive some of my little machines for hobby. This is the first time I end u with 1500 rows of code so I am wondering which is the correct way to get from point "a" to point "d". What I mean is the following:

I receive data from serial:
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
       {
           try
           {
                 SetText(serialPort1.ReadLine());
           }

           catch (Exception ex)
           {
               SetText(ex.ToString());
           }
       }

I use a delegate to make the data available
delegate void SetTextCallback(string text);

I pass the data to a class which checks for the correctness of the received data format and takes only the relevant portion to be passed to the methods that will elaborate the data:
private void SetText(string text)
        {
            if (this.txtOutput.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.BeginInvoke(d, new object[] { text });
            }
            else
            {
               //txtOutput.AppendText(text + "\r\n"); //Monitor for debugging
                string ResCurrRaw = "", TempResRaw = "", RPMraw = "", CurrMotRaw="", TempAmbRaw=""; 
                string invDatiSerial = text.ToString();
                Stripper strp = new Stripper();
                strp.Distri(invDatiSerial, out ResCurrRaw, out TempResRaw, out RPMraw, out CurrMotRaw, out TempAmbRaw);
               .............



I manage the received data in the class:
class Stripper
    {
        public  void Distri(string incSerialData, out string ResCurr, out string ResTemp, out string RPM, out string MotCurr, out string AmbTemp)
        {
            string subCurrentRes = "";
            string subCurrentMot = "";
            string subTempRes = "";
            string subTempAmb = ""; ;
            string subRPM = "";
            string f = incSerialData;
            char firstChar = f[0];
            char lastChar = f[f.Length - 2];
            bool testFirsChar = (firstChar.Equals('>'));
            bool testLastChar = (lastChar.Equals('<'));
            int messLenght = f.Length;

            if (testFirsChar == true && testLastChar == true && messLenght <= 10)
            {
                if (f[1] == 'I')
                {
                    subCurrentRes = f.Substring(2, 5);
                }

                else if (f[1] == 'M')
                {
                    subCurrentMot = f.Substring(2, 5);
                }

                else if (f[1] == 'T')
                {
                    subTempRes = f.Substring(2, 6);
                }
                else if (f[1] == 'N')
                {
                    subRPM = "1\r";
                }
                else if (f[1] == 'Z')
                {
                    subTempAmb = f.Substring(2, 5);
                }
            }
            ResCurr = subCurrentRes;
            ResTemp = subTempRes;
            RPM = subRPM;
            MotCurr = subCurrentMot;
            AmbTemp = subTempAmb;
        }
    }


At this point I am not sure if I should pass back the parameters, as actually coded, or if I should instead pass those parameters directly to the methods in Form 1 and/or other classes which make sampling for averaging the values and shows them in labels, text boxes, charts, or arrange the same to be used in automatic controls (PID).
Which is the best/correct way to get this done? Pass the parameters back and restart from there or pass them forward? See the image here http://tinypic.com/r/in8vfk/5[^] for more clarity of my question, is it better to code as per flow at the top or the flow at the bottom.
Posted
Updated 9-Jul-13 16:08pm
v4
Comments
Maciej Los 9-Jul-13 17:48pm    
I'm not sure what you're asking for...
Millone 9-Jul-13 22:10pm    
Sorry for the confusing question. Please have a look here http://tinypic.com/r/in8vfk/5[^], which is the better flow for coding, the top one or the bottom one?
Sergey Alexandrovich Kryukov 10-Jul-13 0:15am    
I barely managed to get it, too, but now I think I understand it. Please see my attempt to answer...
—SA

1 solution

It was pretty difficult to understand your concerns. Anyway, by all means, avoid such thing as to "pass those parameters directly to the methods in Form 1". Just the opposite, you should isolate UI from other aspects of functionality as much as possible.

In the code you show, you should have at least three distinct and isolated parts: 1) serial communication, 2) parsing of data coming from serial port (probably, you also have to feed some data to serial communication, but you did not describe this part); 3) UI.

You should abstract those parts from each other. As to the serial communications, in most cases you will need to have it in a separate thread, do avoid mixing timing, logic and keep UI responsive. Why do you use BeginInvoke in your code? Does it mean that you already use a thread? In a single thread, Invoke or BeginInvoke is not required.

Why BeginInvoke, not Invoke? Sort it out. Please see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

Your code has many problems. You long "if" statement is based on a number of hard coded immediate constants 'I', 'M', 'T', etc. This is not supportable. At least declare them explicitly as constants. Do you guarantee that f[1] always exists? What about f[0]? Never use names like serialPort1_DataReceived, Form1 and the like. They violate (good) Microsoft naming conventions. Do yourself a big favor: meed them: no underscore, no numerals. Generally, those auto-generated names are not intended to be used in real code: why do you think you are provided with the refactoring language? Use it to rename all names to something semantic, as soon as you start actually use the names. Also, don't use "", use string.Empty; you will see the convenience of it when you get rid of immediate constants. Finally, your set of many out parameters is inconvenient. Better put them all in some structure/class you could return using one return statement; this would greatly simplify your code. Why are all the parameters are strings? Don't you think you are trying to work with strings representing data instead of data itself? If so, parse all strings to data immediately and discard them.

—SA
 
Share this answer
 
Comments
Maciej Los 10-Jul-13 6:45am    
Wow!
+5!
Sergey Alexandrovich Kryukov 10-Jul-13 10:31am    
Thank you, Maciej.
—SA
Millone 10-Jul-13 16:22pm    
Thank you SA. I started the refactoring and slowly slowly is coming right. Thanks for the suggestions.
Sergey Alexandrovich Kryukov 10-Jul-13 16:24pm    
You are very welcome. Well, better slowly than never. :-)
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900