private void ConfBtn_Click(object sender, EventArgs e) { if (nodeinfolist.Items.Count > 0) { if (DestNodeBox.Text == "") { return; } int nodenum = nodeinfolist.Items.Count; int[] dat = new int[1]; SourceDataClass.InitForPack(nodenum * 115 + 6 + 20); dat[0] = 2; SourceDataClass.OutPutIntBit(dat, 8); dat[0] = nodenum * 115 + 6 + 20; SourceDataClass.OutPutIntBit(dat, 12); dat[0] = nodenum; SourceDataClass.OutPutIntBit(dat, 6);//节点数 foreach (string nodename in NodeInfo.Keys) { SourceDataClass.OutPutArrayBit((BitArray)NodeInfo[nodename]); } //加入列表 MainForm.pMainForm.comlistwin.AddCmd("节点" + DestNodeBox.Text.TrimStart('节', '点'), "节点信息表", SourceDataClass.packdata); MainForm.pMainForm.RefreshListStat(); MessageBox.Show("节点信息表已加入命令列表!"); DialogResult = DialogResult.OK; } else { MessageBox.Show("尚未添加任何节点信息!"); DialogResult = DialogResult.Ignore; } }
private void AddToList_Click(object sender, EventArgs e) { if (DestNodeName.Text == "") { return; } int[] dat = new int[1]; SourceDataClass.clear(); SourceDataClass.InitForPack(20 + 16); dat[0] = 142; SourceDataClass.OutPutIntBit(dat, 8); dat[0] = 36; SourceDataClass.OutPutIntBit(dat, 12); BitArray a = new BitArray(16); for (int i = 0; i < 16; i++) { a[i] = CommType.GetItemChecked(i); } SourceDataClass.OutPutArrayBit(a); //加入列表 MainForm.pMainForm.comlistwin.AddCmd(DestNodeName.Text, "通信制式开关", SourceDataClass.packdata); MainForm.pMainForm.RefreshListStat(); MessageBox.Show("通信制式开关命令已加入命令列表!"); }
//打包所有数据区到未加0xAA为止,将有相同目的地址的数据打包在一个块中 private byte[] pack() { NodeFindOut.Clear(); CmdTable.Clear(); //每次打包前清空。 int total = 0; //数据总长度,不包括包头 for (int i = 0; i < CmdNode.Count; i++) { if (CmdTable.ContainsKey(CmdNode[i])) { BitArray ba = (BitArray)CmdTable[CmdNode[i]];//取出已有的数据 BitArray newba = new BitArray(ba.Length + CmdForSend[i].Length); for (int a = 0; a < ba.Length; a++) { newba[a] = ba[a]; } for (int j = 0; j < CmdForSend[i].Length; j++) { newba[ba.Length + j] = CmdForSend[i][j]; } CmdTable[CmdNode[i]] = newba;//将新的数据放进哈希表 } else { CmdTable.Add(CmdNode[i], CmdForSend[i]); NodeFindOut.Add(CmdNode[i]); } total += CmdForSend[i].Length; } //打包协议 int blocknum = CmdTable.Keys.Count;//块数 total += blocknum * 34 + 6; SourceDataClass.InitForPack(total); int[] b = new int[1]; b[0] = blocknum; SourceDataClass.OutPutIntBit(b, 6); for (int i = 0; i < blocknum; i++) { BitArray ba = (BitArray)CmdTable[NodeFindOut[i]];//数据区集合 int blocklen = ba.Length + 34; BitArray blockba = new BitArray(blocklen); b[0] = PackageIndex; PackageIndex++; SourceDataClass.OutPutIntBit(b, 10); b[0] = blocklen; SourceDataClass.OutPutIntBit(b, 12); b[0] = int.Parse(SourceNodeBox.Text.TrimStart('节', '点')); SourceDataClass.OutPutIntBit(b, 6); b[0] = int.Parse(NodeFindOut[i].TrimStart('节', '点')); SourceDataClass.OutPutIntBit(b, 6); SourceDataClass.OutPutArrayBit(ba); } byte[] cmd = new byte[(int)Math.Ceiling(((double)total) / 8)]; SourceDataClass.packdata.CopyTo(cmd, 0); return(cmd); }
private byte[] Pack() { //打包协议 try { //路径安排 if (PathAsignCheck.Checked) { if (Path.Text == "") { PathAsignCheck.Checked = false; throw new Exception("路径安排不能为空"); } string[] nodename = Path.Text.Split(' ', ','); int[] dat = new int[1]; int nodenum = nodename.Length; SourceDataClass.InitForPack(nodenum * 6 + 20); dat[0] = 8; SourceDataClass.OutPutIntBit(dat, 8); dat[0] = nodenum * 6 + 20; SourceDataClass.OutPutIntBit(dat, 12); for (int i = 0; i < nodenum; i++) { dat[0] = int.Parse(nodename[i]); SourceDataClass.OutPutIntBit(dat, 6);//节点 } BitArray ba = SourceDataClass.packdata; balst.Add(ba); } // byte[] bstr = System.Text.Encoding.Default.GetBytes(SourceText.Text); BitArray bta = new BitArray(bstr); SourceDataClass.InitForPack(bta.Length + 20); int[] b = new int[1]; b[0] = 101; SourceDataClass.OutPutIntBit(b, 8); b[0] = bta.Length + 20; SourceDataClass.OutPutIntBit(b, 12); SourceDataClass.OutPutArrayBit(bta); balst.Add(SourceDataClass.packdata); int blocklen = 34 + 6;// if (balst.Count > 0) { for (int i = 0; i < balst.Count; i++) { blocklen += balst[i].Length; } } SourceDataClass.InitForPack(blocklen); b[0] = 1; SourceDataClass.OutPutIntBit(b, 6); BitArray blockba = new BitArray(blocklen); b[0] = ComListForm.PackageIndex; ComListForm.PackageIndex++; SourceDataClass.OutPutIntBit(b, 10); b[0] = blocklen - 6;//仅包括块头定义,块长度不包括块数长度 SourceDataClass.OutPutIntBit(b, 12); b[0] = int.Parse(SourceNodeBox.Text.TrimStart('节', '点')); SourceDataClass.OutPutIntBit(b, 6); b[0] = int.Parse(DestNodeName.Text.TrimStart('节', '点')); SourceDataClass.OutPutIntBit(b, 6); for (int i = 0; i < balst.Count; i++) { SourceDataClass.OutPutArrayBit(balst[i]); } balst.Clear(); byte[] cmd = new byte[(int)Math.Ceiling(((double)blocklen) / 8)]; SourceDataClass.packdata.CopyTo(cmd, 0); //BinaryWriter bw = new BinaryWriter(new FileStream("ping.dat", FileMode.Create)); //bw.Write(cmd); //bw.Close(); return(cmd); } catch (Exception MyEx) { balst.Clear(); MessageBox.Show(MyEx.Message); return(new byte[0]); } }
//将信息加入下面的列表 private void AddBtn_Click(object sender, EventArgs e) { if (NodeNameBox.Text == "") { return; } try { int[] dat = new int[1]; SourceDataClass.clear(); SourceDataClass.InitForPack(115); dat[0] = int.Parse(NodeNameBox.Text.TrimStart('节', '点')); SourceDataClass.OutPutIntBit(dat, 6); dat[0] = Nodetypebox.Text == "静态节点" ? 0 : 1; SourceDataClass.OutPutIntBit(dat, 1); dat[0] = int.Parse(EmitSet.Text); SourceDataClass.OutPutIntBit(dat, 3); dat[0] = Convert.ToInt32(Enum.Parse(typeof(SourceDataClass.DeviceAddr), Set1Box.Text)); SourceDataClass.OutPutIntBit(dat, 8); dat[0] = Convert.ToInt32(Enum.Parse(typeof(SourceDataClass.DeviceAddr), Set2Box.Text)); SourceDataClass.OutPutIntBit(dat, 8); int energy = int.Parse(leftenergy.Text); if (energy < 5) { dat[0] = 0; } else if ((energy >= 5) && (energy < 20)) { dat[0] = 1; } else if ((energy >= 20) && (energy < 35)) { dat[0] = 2; } else if ((energy >= 35) && (energy < 50)) { dat[0] = 3; } else if ((energy >= 50) && (energy < 65)) { dat[0] = 4; } else if ((energy >= 65) && (energy < 80)) { dat[0] = 5; } else if ((energy >= 80) && (energy < 95)) { dat[0] = 6; } else if ((energy >= 95)) { dat[0] = 7; } SourceDataClass.OutPutIntBit(dat, 3); BitArray a = new BitArray(16); for (int i = 0; i < 16; i++) { a[i] = CommType.GetItemChecked(i); } SourceDataClass.OutPutArrayBit(a); if (Lang.Value < 0) { dat[0] = 0x8ffffff + (int)((double)Math.Abs(Lang.Value) * 60 * 10000); } else { dat[0] = (int)((double)Math.Abs(Lang.Value) * 60 * 10000); } SourceDataClass.OutPutIntBit(dat, 28); if (Lat.Value < 0) { dat[0] = 0x8ffffff + (int)((double)Math.Abs(Lat.Value) * 60 * 10000); } else { dat[0] = (int)((double)Math.Abs(Lat.Value) * 60 * 10000); } SourceDataClass.OutPutIntBit(dat, 28); dat[0] = (int)(DepthInput.Value / 0.5); SourceDataClass.OutPutIntBit(dat, 14); if (NodeInfo.ContainsKey(NodeNameBox.Text)) { NodeInfo.Remove(NodeNameBox.Text); } //加入hash表 NodeInfo.Add(NodeNameBox.Text, SourceDataClass.packdata); //替换之前的记录 string log = ParseDataAndDisplay((BitArray)NodeInfo[NodeNameBox.Text], false); if (nodeinfolist.Items.Count == 0)//第一条记录 { nodeinfolist.Items.Add(log); } for (int i = 0; i < nodeinfolist.Items.Count; i++) { if (CheckListNodeName(i, NodeNameBox.Text)) { nodeinfolist.Items[i] = log; break; } if (i == nodeinfolist.Items.Count - 1)//最后一项也比过了,说明当前列表中没有 { nodeinfolist.Items.Add(log); } } SourceDataClass.clear(); } catch (Exception ex) { SourceDataClass.clear(); MessageBox.Show(ex.Message); } }