/// <summary> /// 定时器,用来刷新dataGridView数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer1_Tick(object sender, EventArgs e) { int nCount = mMsg.Count(); while (nCount > 0) { if (mMsg.Count == 0) { break; } TagMsg tmsg = mMsg[0]; mMsg.RemoveAt(0); TagMsg msg = tmsg; // mMsg添加数据到mDMsg if (mDMsg.ContainsKey(msg.TagId)) { mDMsg[msg.TagId].Tagmsg = msg; mDMsg[msg.TagId].Count += 1; } else { mDMsg[msg.TagId] = new Temp(msg); } nCount--; } this.Invoke((EventHandler)(delegate { dt.Rows.Clear(); int COUNT = 0; // 遍历mDMsg,把数据添加到dataGridView foreach (var item in mDMsg) { Temp temp = item.Value; TagMsg msg = temp.Tagmsg; // 过滤标签 if (!String.IsNullOrEmpty(this.textBox1.Text)) { try { this.TagShow.Text = "显示单个标签"; int ntDeviceId = Convert.ToInt32(this.textBox1.Text); if (ntDeviceId == item.Key) { DataRow row = dt.NewRow(); row[0] = msg.DeviceId; row[1] = msg.ActiveId; row[2] = msg.TagId; row[3] = msg.TagRssi; row[4] = msg.DeviceRssi; row[5] = msg.State.ToString("X2"); row[6] = msg.ReciveDt.ToString("HH:mm:ss"); row[7] = temp.Count.ToString(); dt.Rows.Add(row); COUNT++; } } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } continue; } else { this.TagShow.Text = "显示所有标签"; DataRow row = dt.NewRow(); row[0] = msg.DeviceId; row[1] = msg.ActiveId; row[2] = msg.TagId; row[3] = msg.TagRssi; row[4] = msg.DeviceRssi; row[5] = msg.State.ToString("X2"); row[6] = msg.ReciveDt.ToString("HH:mm:ss"); row[7] = temp.Count.ToString(); dt.Rows.Add(row); COUNT++; } continue; } this.COUNT.Text = COUNT.ToString(); this.dataGridView1.DataSource = dt.DefaultView; })); }
public Temp(TagMsg msg) { this.Tagmsg = msg; Count = 1; }
/// <summary> /// 接收数据事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) { int nReadBytes = this.serialPort1.BytesToRead; if (nReadBytes == 0) { return; } byte[] bRead = new byte[nReadBytes]; this.serialPort1.Read(bRead, 0, nReadBytes); // 已经读取完成 List <Byte> mRecv = new List <Byte>(); mRecv.AddRange(bRead); #region while (mRecv.Count > 9) { if (mRecv[0] == 0x55 && mRecv[1] == 0xAA) { int dataLength = (mRecv[3] << 8 | mRecv[2]) + 2; if (mRecv.Count >= dataLength) { byte[] data = new byte[dataLength]; mRecv.CopyTo(0, data, 0, dataLength); int nDeviceId = data[5] << 8 | data[4]; switch (data[6]) { case 0x01: // 接收数据 #region " 0x01 " DateTime now = DateTime.Now; int nCount = dataLength / 9 - 1; for (int i = 0; i < nCount; i++) { int nTagId = data[13 + i * 9] << 16 | data[12 + i * 9] << 8 | data[11 + i * 9]; int nDeviceRssi = data[10 + i * 9] << 8 | data[9 + i * 9]; int nActiveId = data[15 + i * 9] << 8 | data[16 + i * 9]; int nTagRssi = data[17 + i * 9]; // set value TagMsg tagMsg = new TagMsg(); tagMsg.ReciveDt = now; tagMsg.State = data[14 + i * 9]; tagMsg.DeviceId = nDeviceId; tagMsg.DeviceRssi = nDeviceRssi; tagMsg.ActiveId = nActiveId; tagMsg.TagId = nTagId; tagMsg.TagRssi = nTagRssi; if (mMsg.Count >= 200) { mMsg.RemoveAt(0); } // 过滤为0的数据 if (tagMsg.ActiveId != 0) { mMsg.Add(tagMsg); } } #endregion break; case 0x02: // 心跳 break; case 0x03: // 配置集中器参数指令 break; case 0x04: // 设置控制器参数指令 break; case 0x05: // 设置主机出厂指令 break; case 0x0B: // 配置基站网关参数指令 break; case 0x0C: // 读取基站网关参数指令 break; default: break; } mRecv.RemoveRange(0, dataLength); continue; } else { // 数据长度不一样就跳出 break; } } else { mRecv.RemoveAt(0); } } // endwhile #endregion }