private void listView_State_Update(ParamList.ParamItem item) { int listIndex = ParamList.FindById(paramList, item.id); string parentValue = null; DataGridView grid = findGridViewById(item.id); if (listIndex < 0) { return; } if (grid == null) { return; } this.Invoke((EventHandler)(delegate { try { listView_State.Items[listIndex].SubItems[2].Text = item.value; for (listIndex = 0; listIndex < grid.Rows.Count; listIndex++) { if (grid.Rows[listIndex].Cells[0].Value.Equals(item.id.ToString()) && !grid.Rows[listIndex].Cells[2].IsInEditMode) { string name = grid.Rows[listIndex].Cells[1].Value.ToString().Split(':')[0]; if (name.Substring(0, 3).Equals("bit") && parentValue != null && parentValue.Length > 0) { int shift; int width = 0; int oldValue = Convert.ToInt32(paramList[listIndex].value); if (name.Substring(3, 1).Equals("[")) { shift = Convert.ToInt32(name.Substring(3, 2)); width = Convert.ToInt32(name.Substring(6, 2)) - shift; } else { shift = Convert.ToInt32(name.Substring(3, 2)); width = 1; } grid.Rows[listIndex].Cells[2].Value = (Convert.ToInt32(parentValue) >> shift) & ((1 << width) - 1); } else { grid.Rows[listIndex].Cells[2].Value = item.value; parentValue = item.value; } } } } catch (Exception) { } })); }
private void MainForm_Load(object sender, EventArgs e) { try { configInNvm = ConfigInfo.GetNvmConfig(); checkBoxShowInHex.Checked = configInNvm.showInHex; checkBoxAutoNewLine.Checked = configInNvm.autoNewLine; checkBoxShowInHex.Checked = configInNvm.showSend; checkBoxShowInHex.Checked = configInNvm.sendInHex; checkBoxShowInHex.Checked = configInNvm.sendNewLine; serialPort.BaudRate = configInNvm.baud; serialPort.DataBits = configInNvm.dataBits; serialPort.StopBits = configInNvm.stopBits; serialPort.Parity = configInNvm.parity; } catch (Exception) { MessageBox.Show("文件信息缺失,读取信息失败,请重装软件", "错误"); } StatusStrip_Init(); tbxCycle.Text = configInNvm.period.ToString(); serialPort.ReadTimeout = 1000000 / serialPort.BaudRate; paramList = ParamList.ParamList_Init(); int i; for (i = 0; i < paramList.Count; i++) { DataGridView grid; ParamList.ParamItem item = paramList[i]; if (item.id < 256) { grid = dataGridView_param; } else if (item.id < 258) { grid = dataGridView_do; } else if (item.id < 260) { grid = dataGridView_di; } else { break; } ListViewItem listItem = new ListViewItem(); listItem.Text = item.id.ToString(); listItem.SubItems.Add(new ListViewItem.ListViewSubItem().Text = item.name); listItem.SubItems.Add(new ListViewItem.ListViewSubItem().Text = item.value); listItem.SubItems.Add(new ListViewItem.ListViewSubItem().Text = item.unit); listItem.SubItems.Add(new ListViewItem.ListViewSubItem().Text = item.description); listView_State.Items.Add(listItem); DataGridViewRow row = new DataGridViewRow(); DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell(); cell.Value = item.id.ToString(); row.Cells.Add(cell); cell = new DataGridViewTextBoxCell(); cell.Value = item.name; row.Cells.Add(cell); cell = new DataGridViewTextBoxCell(); cell.Value = item.value; row.Cells.Add(cell); cell = new DataGridViewTextBoxCell(); cell.Value = item.unit; row.Cells.Add(cell); cell = new DataGridViewTextBoxCell(); cell.Value = item.description; row.Cells.Add(cell); grid.Rows.Add(row); if (item.bitMap.Count > 0) { foreach (ParamList.BitMap bit in item.bitMap) { row = new DataGridViewRow(); cell = new DataGridViewTextBoxCell(); cell.Value = item.id.ToString(); cell.Style.ForeColor = Color.White; cell.Style.Alignment = DataGridViewContentAlignment.BottomRight; row.Cells.Add(cell); cell = new DataGridViewTextBoxCell(); if (bit.bitstart == bit.bitend) { cell.Value = "bit" + bit.bitstart.ToString().PadLeft(2, '0') + ": " + bit.name; } else { cell.Value = "bit[" + bit.bitstart.ToString().PadLeft(2, '0') + "," + bit.bitend.ToString().PadLeft(2, '0') + "]: " + bit.name; } row.Cells.Add(cell); cell = new DataGridViewTextBoxCell(); cell.Value = bit.value; row.Cells.Add(cell); cell = new DataGridViewTextBoxCell(); cell.Value = bit.unit; row.Cells.Add(cell); cell = new DataGridViewTextBoxCell(); cell.Value = bit.description; row.Cells.Add(cell); grid.Rows.Add(row); } } } dataGridView_param.AllowUserToAddRows = false; dataGridView_param.AllowUserToDeleteRows = false; listView_State.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent); listView_State.Columns[0].TextAlign = HorizontalAlignment.Center; listView_State.Columns[1].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent); //listView_State.Columns[1].TextAlign = HorizontalAlignment.Center; //listView_State.Columns[2].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent); listView_State.Columns[2].TextAlign = HorizontalAlignment.Center; listView_State.Columns[3].AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize); listView_State.Columns[3].TextAlign = HorizontalAlignment.Center; listView_State.Columns[4].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent); //listView_State.Columns[4].TextAlign = HorizontalAlignment.Center; serialPort.DataReceived += new SerialDataReceivedEventHandler(serialDataReceive); dataGridView_di.AllowUserToAddRows = false; dataGridView_di.AllowUserToDeleteRows = false; dataGridView_do.AllowUserToAddRows = false; dataGridView_do.AllowUserToDeleteRows = false; UpdateUiThread = new Thread(new ThreadStart(updateUiMethod)); UpdateUiThread.Start(); tabControl1.TabPages.RemoveAt(1); }