如何在c#中将结构数组转换为字节数组

时间:2014-08-18 10:36:11

标签: c#

我面临着将结构数组转换为字节数组的问题。我创建了一个名为'数组的结构数组。我希望将这个结构数组转换为字节数组。我的目的是传递这个数组到serialport .. 这是我的代码

public  struct test
    {
        public string name;
        public byte icode;
        public int method;
        public int wav;
        public int wav2;
        public int unit;
        public int temp;

    }
    test[] array = new test[200];

  private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        char[] c = new char[1000];
        char[] c2 = new char[1000];
        int O = 0;
        string[] ss3 = new string[200];
        string[] s2 = new string[200];
        string[] s3 = new string[200];
        string ss = "";
        string dfgh = "";
        string sd = "";
        string dfg = "";
        int k = 0;
        string[] rr = new string[200];
        //Thread.Sleep(1000);
        ss = serialPort1.ReadExisting();
        s2 = ss.Split('#');
        for (int i = 1; i < 2; i++)
        {
            //s3[i] = s2[i].TrimStart(',');
            string V = s2[i];
            string s = V.Trim(' ');

            if (s == "")
            {
                break;
            }
            // string BBBB = s;
            c = s.ToCharArray();


            for (int l = 0; l < c.Length; l++)
            {
                for (int j = l; j < l + 3; j++)
                {

                    //  O=j;
                    //if (c[j] == "")
                    //    break;
                    if (j == c.Length)
                        break;
                    if (c[j] != ',' && c[j] != '*')
                    {


                        string b = c[j].ToString();
                        //  int v = Convert.ToInt32(b);
                        // char  cc = (char)v;
                        if (b == " ")
                        {
                        }
                        else
                        {
                            dfg += b.ToString();
                            l++;
                        }
                    }
                    else if (c[j] == ',' || c[j] == '*')
                    {
                        if (c[j] == '*')
                        {

                            rr[k] = dfg;
                            k++;
                            dfg = "";
                            rr[k] = "*";
                            k++;
                            count++;
                            CCOUNT++;
                            if (CCOUNT % 2 == 0)
                            {
                                this.Invoke((MethodInvoker)delegate
                                {
                                    timer1.Enabled = true;
                                });
                                break;
                            }
                        }
                        else
                        {
                            rr[k] = dfg;
                            dfg = "";
                            k++;
                            break;
                        }
                    }

                }
                if (dfg == "")
                {

                }
                else
                {
                    rr[k] = dfg;
                    dfg = "";
                    k++;
                }
            }





        }

        test t;


        int index = Convert.ToInt32(rr[0]);
        array[index].wav =Convert.ToByte( index);

        array[index].name = rr[1];

如果有人对此有任何想法请分享..

0 个答案:

没有答案