程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 軟件開發-C# 多窗口之間動態傳值問題

軟件開發-C# 多窗口之間動態傳值問題

編輯:編程綜合問答
C# 多窗口之間動態傳值問題

我在做一個串口接收數據並處理的小軟件,在主窗口接收並處理後在TextBox上輸出,為了直觀的顯示數據變化,所以再開一個窗口顯示波形。
我采用public類傳遞數值,為了方便測試,我還順便傳了一個隨機數,結果每次隨機數傳過去了,串口的數據沒有傳過去。
把隨機數生成放在接收事件中一樣傳遞不過去。

傳遞參數的Public類。

public int[] tranTest()
{
        Random rd = new Random();

        int[] TranArr = new int[4];
        if (SpeedR1.Count > 0)
        {
                TranArr[0] = SpeedR1[SpeedR1.Count - 1];
                TranArr[1] = SpeedR2[SpeedR2.Count - 1];
                TranArr[2] = SpeedSU[SpeedSU.Count - 1];
        }
        TranArr[3] = rd.Next(0, 400);
        return TranArr;
}

數據接收和處理

int count = 0;
Byte[] TFrame = new Byte[20];
int CountData = 0;
Byte Flag = 0;

private List<int> SpeedR1 = new List<int>();
private List<int> SpeedR2 = new List<int>();
private List<int> SpeedSU = new List<int>();
private List<int> SpeedAc = new List<int>();
private List<int> Reli = new List<int>();

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{

        Byte[] data = new Byte[serialPort1.BytesToRead];
        Byte[] Cdata = new Byte[1024*10];
        string RecieveData="";
        serialPort1.Read(data, 0, data.Length);  //讀取串口數據

        for (int i = 0; i < data.Length; i++)
        {
                //RecieveData = RecieveData + BitConverter.ToString(data[i], 16) + " ";
                Cdata[CountData++] = data[i];

        }

        RecieveData =  BitConverter.ToString(data).Replace("-"," ")+" ";

        Thread DealData = new Thread(new ThreadStart(dealStr));
        if (CountData >= 15)
        {
                int Temp=0;
                for (int j = 0; j < CountData - 2; j++)
                {
                        if (Cdata[j] == 0xF3 && Cdata[j + 1] == 0xF4)
                        {
                                Flag = 1;
                                Temp = j;
                                break;
                        }
                }
                if (Flag == 1)
                {
                        for (int t = 0; t < 15 && Temp + t < CountData; t++)
                                TFrame[count++] = Cdata[Temp + t];
                        if (count >= 15)
                        {
                                count = 0;
                                Flag = 0;
                                DealData.Start();
                        }

                }
                CountData = 0;
        }

        TxtRec.Invoke(new Action(() => { TxtRec.AppendText(RecieveData); }));

}




public void dealStr()
{
        if (TFrame[2] == 1)
                SpeedR1.Add(TFrame[3] * 256 + TFrame[4]);
        else
                SpeedR1.Add((TFrame[3] * 256 + TFrame[4]) * (-1));

        if (TFrame[5] == 1)
                SpeedR2.Add(TFrame[6] * 256 + TFrame[7]);
        else
                SpeedR2.Add((TFrame[6] * 256 + TFrame[7]) * (-1));

        if (TFrame[8] == 1)
                SpeedSU.Add(TFrame[9] * 256 + TFrame[10]);
        else
                SpeedSU.Add((TFrame[9] * 256 + TFrame[10]) * (-1));

        if (TFrame[11] == 1)
                SpeedAc.Add(TFrame[12]);
        else
                SpeedAc.Add(TFrame[12] * (-1));

        Reli.Add (TFrame[13]);

        //tranTest();

        string Speed1 = SpeedR1[SpeedR1.Count-1].ToString();
        if (Speed1[0] != '0')
        {
                int Count1 = Speed1.Length;
                Speed1 = Speed1.Insert(Count1 - 2, ".");
        }
        string Speed2 = SpeedR2[SpeedR2.Count-1].ToString();
        if (Speed2[0] != '0')
        {
                int Count1 = Speed2.Length;
                Speed2 = Speed2.Insert(Count1 - 2, ".");
        }

        string Speed_S = SpeedSU[SpeedSU.Count-1].ToString();
        if (Speed_S[0] != '0')
        {
                int Count1 = Speed_S.Length;
                Speed_S = Speed_S.Insert(Count1 - 2, ".");
        }
        string Acc_Speed = SpeedAc[SpeedAc.Count-1].ToString();
        if (Acc_Speed[0] != '0')
        {
                int Count1 = Acc_Speed.Length;
                if(Count1==1)
                        Acc_Speed = Acc_Speed.Insert(Count1 - 1, "0.");
                else
                        Acc_Speed = Acc_Speed.Insert(Count1 - 1, ".");
        }
        string Reliability = Reli[Reli.Count-1].ToString() + "%";



        R1Speed.Invoke(new Action(() => { R1Speed.Clear(); R1Speed.AppendText(Speed1); }));
        R2Speed.Invoke(new Action(() => { R2Speed.Clear(); R2Speed.AppendText(Speed2); }));
        SumSpeed.Invoke(new Action(() => { SumSpeed.Clear(); SumSpeed.AppendText(Speed_S); }));
        AccSpeed.Invoke(new Action(() => { AccSpeed.Clear(); AccSpeed.AppendText(Acc_Speed); }));
        ReBility.Invoke(new Action(() => { ReBility.Clear(); ReBility.AppendText(Reliability); }));

}

窗口2中獲取數據類有個定時器,定時獲取。

MainForm frm = new MainForm();
int[] DrawL1 = new int[1024 * 1024];
int[] DrawL2 = new int[1024 * 1024];
int[] DrawL3 = new int[1024 * 1024];
int[] TestDraw = new int[1024 * 1024];
int DCount=0;

private void GetData()
{

        int[] Tran=frm.tranTest();
        DrawL1[DCount] = Tran[0];
        DrawL2[DCount] = Tran[1];
        DrawL3[DCount] = Tran[2];
        TestDraw[DCount] = Tran[3];

        DCount++;
}

一直在做C的嵌入式編程,現在要自己寫一些測試軟件和配套軟件所以剛開始學C#,求各位大神指點。

最佳回答:


參考這個

C#多窗口之間傳值的兩種方法—實例介紹

http://blog.sina.com.cn/s/blog_3d4c358f0100kezv.html

或者用靜態變量來實現

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved