返回参数或使它们可用于下一个功能? [英] Return parameters or make them available to the next function?

查看:92
本文介绍了返回参数或使它们可用于下一个功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C#编写代码来驱动我的一些小机器。这是我第一次用1500行代码结束你,所以我想知道从a点到d点的正确方法是什么。我的意思是以下内容:



我从串口收到数据:

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;
        }
    }





此时我不确定是否应该传回参数,实际上编码,或者我应该将这些参数直接传递给表格1中的方法和/或其他类别,这些类别进行采样以平均值并在标签,文本框,图表中显示它们,或者将它们安排在自动控制中使用(PID)。

完成这项工作的最佳/正确方法是什么?将参数传回并从那里重新启动或向前传递?请点击此处 http://tinypic.com/r/in8vfk/5 [ ^ ]为了更清楚我的问题,是否更好按照顶部的流程或底部的流程进行编码。



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.

推荐答案

很难理解你的担忧。无论如何,无论如何,要避免将这些参数直接传递给表格1中的方法。恰恰相反,您应该尽可能地将UI与功能的其他方面隔离开来。



在您显示的代码中,您应该至少有三个不同且孤立的部分:1)串行通信,2)解析来自串口的数据(可能,你还需要将一些数据提供给串口通信,但你没有描述这部分); 3)UI。



你应该将这些部分相互抽象。至于串行通信,在大多数情况下,您需要将它放在一个单独的线程中,避免混合时序,逻辑并保持UI响应。为什么在代码中使用 BeginInvoke ?这是否意味着你已经使用了一个帖子?在单个线程中,不需要调用 BeginInvoke



为什么 BeginInvoke ,而不是调用?梳理出来。请查看我过去的答案:

Control.Invoke( )与Control.BeginInvoke() [ ^ ],

Treeview扫描仪和MD5的问题 [ ^ ]。



您的代码有很多问题。你很久if语句是基于一些硬编码的立即常量'I','M','T'等。这是不可支持的。至少将它们明确地声明为常量。你保证 f [1] 总是存在吗?那么 f [0] 呢?切勿使用 serialPort1_DataReceived Form1 等名称。它们违反(良好)Microsoft命名约定。做一件大事:给他们注意:没有下划线,没有数字。通常,这些自动生成的名称不适用于实际代码:为什么您认为自己获得了重构语言?只要您开始实际使用名称,就可以使用它将所有名称重命名为语义名称。另外,不要使用,使用 string.Empty ;当你摆脱立即常数时,你会看到它的便利性。最后,你的许多 out 参数的设置是不方便的。最好把它们放在一些结构/类中,你可以使用一个 return 语句返回;这将大大简化您的代码。为什么所有参数都是字符串?您是否认为您正在尝试使用表示数据的字符串而不是数据本身?如果是这样,立即将所有字符串解析为数据并丢弃它们。



-SA
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


这篇关于返回参数或使它们可用于下一个功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆