private void CalulateLocation(UDPData data, out int x, out int y) { if (string.IsNullOrEmpty(data.Loc[0].ReferNo)) { x = y = -1; return; } else { for (int ii = 0; ii < 3; ii++) { for (int jj = 0; jj < _dictReaderLoc.Count; jj++) { if (data.Loc[ii].ReferNo == _dictReaderLoc.Keys.ElementAt(jj)) { if (_dictTag.ContainsKey(jj)) { _dictTag[jj] = data.Loc[ii].Destance; } else { _dictTag.Add(jj, data.Loc[ii].Destance); } break; } } } Point pt = CaculateLocation.OutputResultLocation(_dictTag); x = pt.X; y = pt.Y; } }
private void DealData(UDPData data) { //纳入表格 DataRow dr = dtData.NewRow(); dr["RowNo"] = dtData.Rows.Count + 1; dr["TagNo"] = data.TagNo; dr["ReferNo1"] = data.Loc[0].ReferNo; dr["Distance1"] = data.Loc[0].Destance; dr["ReferNo2"] = data.Loc[1].ReferNo; dr["Distance2"] = data.Loc[1].Destance; dr["ReferNo3"] = data.Loc[2].ReferNo; dr["Distance3"] = data.Loc[2].Destance; dr["ReadDate"] = DateTime.Now; dtData.Rows.Add(dr); VoidDelegate dRefresh = new VoidDelegate(delegate { //this.dgData this.dgData.Refresh(); }); if (this.dgData.InvokeRequired) { this.Invoke(dRefresh); } else { dRefresh(); } if (string.IsNullOrEmpty(data.Loc[2].ReferNo)) { return; } //计算网格坐标 int x, y; CalulateLocation(data, out x, out y); //显示 VoidDelegate dShow = new VoidDelegate(delegate { UC.UCRefPoint ucPoint = null; if (!this.panMap.Controls.ContainsKey("uc" + data.TagNo)) { ucPoint = new PositionManage.UC.UCRefPoint(); ucPoint.Name = "uc" + data.TagNo; ucPoint.PointName = data.TagNo; ucPoint.PointColor = Color.Blue; this.panMap.Controls.Add(ucPoint); } else { var ctrls = this.panMap.Controls.Find("uc" + data.TagNo, false); if (ctrls.Length > 0) { ucPoint = ctrls[0] as UC.UCRefPoint; } else { return; } } int iLocX = 0, iLocY = 0; //四顶点 x + y * 3, x + y * 3 + 1, x + y * 3 + 3, x + y * 3 + 4 if (x == 0) { iLocY = 300; } else { iLocY = 100; } if (y == 0) { iLocX = 350; } else if (y == 1) { iLocX = 290; } else if (y == 2) { iLocX = 220; } else if (y == 3) { iLocX = 150; } else { iLocX = 100; } ucPoint.Location = new Point(iLocX, iLocY); }); if (this.dgData.InvokeRequired) { this.Invoke(dShow); } else { dShow(); } }
/// <summary> /// 监听支线程,循环模式。 /// </summary> private void LoopReceiveData() { byte[] buff = null; System.Net.IPEndPoint ipDes = _ipaddrDes; while (!_IsInit) { try { buff = _udp.Receive(ref ipDes); if (buff != null && buff.Length >= 8) { if (buff[0] != 0xFA || buff[8] != 0xFB) //FA开头有效,中间为FB有效 { continue; } //校验校验和是否正确 byte iSum = 0; bool bVaild = true; for (int ii = 0; ii < 16; ii++) { if (ii == 7) { if (iSum != buff[7]) { bVaild = false; break; } else { iSum += buff[ii]; } } else if (ii == 15) { if (iSum != buff[15]) { bVaild = false; break; } } else { iSum += buff[ii]; } } if (!bVaild) { continue; } //解析数据,并处理 UDPData data = new UDPData(); data.TagNo = Convert.ToString(((buff[1] << 16) + (buff[2] << 8) + buff[3]), 16).PadLeft(6, '0').ToUpper(); data.Loc[0] = new Location() { ReferNo = Convert.ToString(((buff[5] << 8) + buff[6]), 16).PadLeft(4, '0').ToUpper(), Destance = buff[4] }; data.Loc[1] = new Location() { ReferNo = Convert.ToString(((buff[10] << 8) + buff[11]), 16).PadLeft(4, '0').ToUpper(), Destance = buff[9] }; data.Loc[2] = new Location() { ReferNo = Convert.ToString(((buff[13] << 8) + buff[14]), 16).PadLeft(4, '0').ToUpper(), Destance = buff[12] }; DealData(data); } } catch (Exception ex) { continue; } } }