问题描述
- 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
或者用静态变量来实现
解决方案二:
参考我的帖子
http://bbs.csdn.net/topics/360140208
解决方案三:
if (SpeedR1.Count > 0)这句是否为真?
解决方案四:
public class MainForm
{
public static List list = new ArrayList();
//方法对list赋值
}
public class SubForm
{
MainForm frm = new MainForm();
//方法取值
private void getList()
{
List<int> list = frm.list;
}
}
解决方案五:
学习学习,遇到过类似的问题
时间: 2025-01-21 09:15:22