C#数据参数保存,总只能保存一组,不得其解!请各位支招
C#数据参数保存,总只能保存一组,不得其解!请各位支招情况是这样
我有三个numericUpDown控件
try--------------------编程问答-------------------- 先UP一个 --------------------编程问答-------------------- 可能会看不懂把
{
if ((numericUpDown6.Value != 0) && (numericUpDown7.Value != 0))
{
clickNum +=1;
int Nums = wsnChildNums*3+5;
byte[] dataByte = new byte[Nums];
dataByte[0] = 0X2B;
dataByte[1] = 0X2B;
dataByte[2] = 0X2B;
dataByte[3] = 0X02;
dataByte[4] =Convert.ToByte( wsnChildNums*3);
label13.ForeColor = Color.Red;
if (clickNum <= wsnChildNums)
{
label13.Text = "请配置第" + clickNum + "个节点,总共" + wsnChildNums + "个节点";
wsnChildId = Int32.Parse(numericUpDown6.Value.ToString());
wsnChildIdNum = Int32.Parse(numericUpDown7.Value.ToString());
}
else
{
label13.Text = "完成配置";
button4.Text = "写入";
}
string IdNum = wsnChildIdNum.ToString("X").PadLeft(4, '0');
switch (clickNum)
{
case 2:
dataByte[5] = Convert.ToByte(wsnChildId);
dataByte[6] = Convert.ToByte(IdNum.Substring(2, 2), 16);
dataByte[7] = Convert.ToByte(IdNum.Substring(0, 2), 16);
break;
case 3:
dataByte[8] = Convert.ToByte(wsnChildId);
dataByte[9] = Convert.ToByte(IdNum.Substring(2, 2), 16);
dataByte[10] = Convert.ToByte(IdNum.Substring(0, 2), 16);
break;
case 4:
dataByte[11] = Convert.ToByte(wsnChildId);
dataByte[12] = Convert.ToByte(IdNum.Substring(2, 2), 16);
dataByte[13] = Convert.ToByte(IdNum.Substring(0, 2), 16);
break;
case 5:
dataByte[14] = Convert.ToByte(wsnChildId);
dataByte[15] = Convert.ToByte(IdNum.Substring(2, 2), 16);
dataByte[16] = Convert.ToByte(IdNum.Substring(0, 2), 16);
break;
}
if (clickNum == wsnChildNums+1)
{
serialPort1.Write(dataByte, 0, Nums);
}
}
else
{
MessageBox.Show("子端机网络号及相对子网容量不能为0", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch
{
}
我在详细的说下:
我有三个numericUpDown控件
numericUpDown1是确定有几组数据
numericUpDown6和numericUpDown7是数组中的数据 --------------------编程问答--------------------
private void button4_Click(object sender, EventArgs e)--------------------编程问答-------------------- 人呢 --------------------编程问答-------------------- clickNum , wsnChildNums 这两个变量是怎么定义的,值是什么?
{
if (serialPort1.IsOpen)
{
try
{
if ((numericUpDown6.Value != 0) && (numericUpDown7.Value != 0))
{
clickNum +=1;
int Nums = wsnChildNums*3+5;
byte[] dataByte = new byte[Nums];
dataByte[0] = 0X2B;
dataByte[1] = 0X2B;
dataByte[2] = 0X2B;
dataByte[3] = 0X02;
dataByte[4] =Convert.ToByte( wsnChildNums*3);
label13.ForeColor = Color.Red;
if (clickNum <= wsnChildNums)
{
label13.Text = "请配置第" + clickNum + "个节点,总共" + wsnChildNums + "个节点";
wsnChildId = Int32.Parse(numericUpDown6.Value.ToString());
wsnChildIdNum = Int32.Parse(numericUpDown7.Value.ToString());
}
else
{
label13.Text = "完成配置";
button4.Text = "写入";
}
string IdNum = wsnChildIdNum.ToString("X").PadLeft(4, '0');
switch (clickNum)
{
case 2:
dataByte[5] = Convert.ToByte(wsnChildId);
dataByte[6] = Convert.ToByte(IdNum.Substring(2, 2), 16);
dataByte[7] = Convert.ToByte(IdNum.Substring(0, 2), 16);
break;
case 3:
dataByte[8] = Convert.ToByte(wsnChildId);
dataByte[9] = Convert.ToByte(IdNum.Substring(2, 2), 16);
dataByte[10] = Convert.ToByte(IdNum.Substring(0, 2), 16);
break;
case 4:
dataByte[11] = Convert.ToByte(wsnChildId);
dataByte[12] = Convert.ToByte(IdNum.Substring(2, 2), 16);
dataByte[13] = Convert.ToByte(IdNum.Substring(0, 2), 16);
break;
case 5:
dataByte[14] = Convert.ToByte(wsnChildId);
dataByte[15] = Convert.ToByte(IdNum.Substring(2, 2), 16);
dataByte[16] = Convert.ToByte(IdNum.Substring(0, 2), 16);
break;
}
if (clickNum == wsnChildNums+1)
{
serialPort1.Write(dataByte, 0, Nums);
}
}
else
{
MessageBox.Show("子端机网络号及相对子网容量不能为0", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch
{
}
}
else
{
MessageBox.Show("端口未打开", "访问被拒绝", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
另外,什么叫只能保存一组? --------------------编程问答-------------------- 有2个button1和button4按钮
button1事件是确定wsnChildNums值
private void button1_Click(object sender, EventArgs e)
{
int wsnChildNums = Int32.Parse(numericUpDown4.Value.ToString());
}
button4按钮点击循环取得numericUpDown6和numericUpDown7中数据
private void button4_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
try
{
if ((numericUpDown6.Value != 0) && (numericUpDown7.Value != 0))
{
clickNum +=1;
int Nums = wsnChildNums*3+5;
byte[] dataByte = new byte[Nums];
dataByte[0] = 0X2B;
dataByte[1] = 0X2B;
dataByte[2] = 0X2B;
dataByte[3] = 0X02;
dataByte[4] =Convert.ToByte( wsnChildNums*3);
label13.ForeColor = Color.Red;
if (clickNum <= wsnChildNums)
{
label13.Text = "请配置第" + clickNum + "个节点,总共" + wsnChildNums + "个节点";
wsnChildId = Int32.Parse(numericUpDown6.Value.ToString());
wsnChildIdNum = Int32.Parse(numericUpDown7.Value.ToString());
}
else
{
label13.Text = "完成配置";
button4.Text = "写入";
}
string IdNum = wsnChildIdNum.ToString("X").PadLeft(4, '0');
switch (clickNum)
{
case 2:
dataByte[5] = Convert.ToByte(wsnChildId);
dataByte[6] = Convert.ToByte(IdNum.Substring(2, 2), 16);
dataByte[7] = Convert.ToByte(IdNum.Substring(0, 2), 16);
break;
case 3:
dataByte[8] = Convert.ToByte(wsnChildId);
dataByte[9] = Convert.ToByte(IdNum.Substring(2, 2), 16);
dataByte[10] = Convert.ToByte(IdNum.Substring(0, 2), 16);
break;
case 4:
dataByte[11] = Convert.ToByte(wsnChildId);
dataByte[12] = Convert.ToByte(IdNum.Substring(2, 2), 16);
dataByte[13] = Convert.ToByte(IdNum.Substring(0, 2), 16);
break;
case 5:
dataByte[14] = Convert.ToByte(wsnChildId);
dataByte[15] = Convert.ToByte(IdNum.Substring(2, 2), 16);
dataByte[16] = Convert.ToByte(IdNum.Substring(0, 2), 16);
break;
}
if (clickNum == wsnChildNums+1)
{
serialPort1.Write(dataByte, 0, Nums);
}
}
else
{
MessageBox.Show("子端机网络号及相对子网容量不能为0", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch
{
}
}
else
{
MessageBox.Show("端口未打开", "访问被拒绝", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
三个numericUpDown控件
numericUpDown1是wsnChildNums值
numericUpDown6和numericUpDown7是数组中的数据
现在好像只能取得一次数据 --------------------编程问答--------------------
private void button1_Click(object sender, EventArgs e)
{
int wsnChildNums = Int32.Parse(numericUpDown4.Value.ToString());
clickNum=0;
}
clickNum 定义的全局变量 --------------------编程问答-------------------- 顶。。。。 --------------------编程问答-------------------- 是只取一次啊。if (serialPort1.IsOpen)
只判断一次啊。如果你用while(serialPort1.IsOpen)
就会循环取了。 --------------------编程问答-------------------- 楼主能不能加点解释在主要语句的后面,还是不明白。
clickNum 是干啥用的
wsnChildNums 又是干啥用的。
IdNum 又是干啥用的。
switch (clickNum)
{
case 2:
dataByte[5] = Convert.ToByte(wsnChildId);
dataByte[6] = Convert.ToByte(IdNum.Substring(2, 2), 16);
dataByte[7] = Convert.ToByte(IdNum.Substring(0, 2), 16);
break;
case 3:
dataByte[8] = Convert.ToByte(wsnChildId);
dataByte[9] = Convert.ToByte(IdNum.Substring(2, 2), 16);
dataByte[10] = Convert.ToByte(IdNum.Substring(0, 2), 16);
break;
case 4:
dataByte[11] = Convert.ToByte(wsnChildId);
dataByte[12] = Convert.ToByte(IdNum.Substring(2, 2), 16);
dataByte[13] = Convert.ToByte(IdNum.Substring(0, 2), 16);
break;
case 5:
dataByte[14] = Convert.ToByte(wsnChildId);
dataByte[15] = Convert.ToByte(IdNum.Substring(2, 2), 16);
dataByte[16] = Convert.ToByte(IdNum.Substring(0, 2), 16);
break;
}
这段是获取数据吗?
button4按钮点击循环取得numericUpDown6和numericUpDown7中数据
请问里面有循环吗?用switch 来循环? --------------------编程问答-------------------- 纯逻辑问题吧。 --------------------编程问答-------------------- 功夫不到家,自己估计没有检查过,只是郁闷为什么老只保存一组数据。switch只是用来判断clickNum然后返回一组相对的数据,根本不会循环去取。
写得也不明白,多少要在后面把重点标记一下,该段是写什么,什么用途 --------------------编程问答-------------------- 看了回复 楼主去试试吧 --------------------编程问答-------------------- 哎,本人比较菜,差个循环来取值
可是循环加哪呢
switch外面试了试,还是一样的情况 --------------------编程问答-------------------- 学习一下. --------------------编程问答-------------------- 你要根据什么去判断循环呢?没看懂。 --------------------编程问答-------------------- --------------------编程问答-------------------- 上了张图,红圈圈的就是用到的地方 --------------------编程问答--------------------
点击写入之后,串口调试工具得到的代码是:2B 2B 2B 02 06 00 00 00 05 06 00
03 04 又被清掉了
我调试中的第一次点击能得到03 04的呢
再点击一次数据没了
只得到最后一次点击的数据 --------------------编程问答--------------------
顶,
还有一般要在代码中的用到的控件,最好取一下名字,numericUpDown6、numericUpDown7这样看着会头晕 --------------------编程问答--------------------
逻辑问题
--------------------编程问答-------------------- CSDN小鸟也比较多滴
byte[] dataByte = new byte[Nums];
定义全局变量 dataByte
这样就OK了
不然的话每次点击 dataByte 又变为空 --------------------编程问答-------------------- 顶
补充:.NET技术 , C#