private void TreeView3_MouseDown(object sender, MouseEventArgs e) { if ((sender as TreeView) != null) { TreeView3.SelectedNode = TreeView3.GetNodeAt(e.X, e.Y); } try { if (TreeView3.SelectedNode.Parent.Level == 0) { SelectedChildNodeName = TreeView3.SelectedNode.Text.ToString(); selectedParentNodeName = TreeView3.SelectedNode.Parent.Text.ToString(); //读取当前信号的信号名 string SignalName = SelectedChildNodeName; SignalName = SignalName.Remove(0, 3); //读取当前信号所属信息的ID string MessageIDStr = selectedParentNodeName; MessageIDStr = MessageIDStr.Remove(0, 3); int MessageIDInt = int.Parse(MessageIDStr); //遍历数据库,取A,B,和信号值 LINQDataContext context = new LINQDataContext(); var query = from sg in context.SendSignal where sg.Signal_Name == SignalName && sg.ID == MessageIDInt select sg; string ABValue = null; string SignalValue = null; foreach (var item in query) { ABValue = item._A_B_; SignalValue = item.Signal_Value; } int SplitIndexStart = ABValue.IndexOf(","); string AValueStr = ABValue.Substring(1, SplitIndexStart - 1); int SplitIndexEnd = ABValue.IndexOf(")"); string BValueStr = ABValue.Substring(SplitIndexStart + 1, SplitIndexEnd - SplitIndexStart - 1); int AValueInt = int.Parse(AValueStr); int BValueInt = int.Parse(BValueStr); //信号值转换为十进制 int SignalValueDecimal = Convert.ToInt32(SignalValue, 16); //推算出物理值 int physics = AValueInt * SignalValueDecimal + BValueInt; //MessageBox.Show(physics.ToString()); this.digitalGauge1.Text = Convert.ToString(physics); this.arcScaleComponent1.Value = physics; } } catch (Exception) { ; } }
private void SaveValue_Click(object sender, EventArgs e) { //读取当前信号的信号名 string SignalName = Form1.SelectedChildNodeName; SignalName = SignalName.Remove(0, 3); //读取当前信号所属信息的ID string MessageIDStr = Form1.selectedParentNodeName; MessageIDStr = MessageIDStr.Remove(0, 3); int MessageIDInt = int.Parse(MessageIDStr); //第一步把用户输入的物理值转换为十进制 string InputPhysicsValue = InputTextBox.Text.ToString().Trim(); int PhysicsValueInt = int.Parse(InputPhysicsValue); //第二步读取当前信号的(A,B)的值 LINQDataContext context = new LINQDataContext(); var query = from sg in context.SendSignal where sg.Signal_Name == SignalName && sg.ID == MessageIDInt select sg; string ABValue = null; foreach (var item in query) { ABValue = item._A_B_; } int SplitIndexStart = ABValue.IndexOf(","); string AValueStr = ABValue.Substring(1, SplitIndexStart - 1); int SplitIndexEnd = ABValue.IndexOf(")"); string BValueStr = ABValue.Substring(SplitIndexStart + 1, SplitIndexEnd - SplitIndexStart - 1); int AValueInt = int.Parse(AValueStr); int BValueInt = int.Parse(BValueStr); //第三步计算信号值,并且转换为十六进制 int SignalValue = (PhysicsValueInt - BValueInt) / AValueInt; String str = SignalValue.ToString("x8"); MessageBox.Show(str); ////第四步存到数据库中 var query1 = from sg in context.SendSignal where sg.Signal_Name == SignalName && sg.ID == MessageIDInt select sg; foreach (var item in query1) { item.Signal_Value = str; } context.SubmitChanges(); this.Close(); }
private void Form3_Load(object sender, EventArgs e) { LINQDataContext context = new LINQDataContext(); var query = from sm in context.SendMessage select sm; foreach (var item in query) { TreeNode tn = TreeView3.Nodes.Add(item.BO_.ToString().Trim() + item.ID.ToString().Trim()); var query1 = from sg in context.SendSignal where sg.ID == item.ID select sg; foreach (var item1 in query1) { TreeNode tn1 = new TreeNode(item1.SG_.ToString().Trim() + item1.Signal_Name.ToString().Trim()); tn.Nodes.Add(tn1); } } }
private void TreeView4_MouseDown(object sender, MouseEventArgs e) { if ((sender as TreeView) != null) { TreeView4.SelectedNode = TreeView4.GetNodeAt(e.X, e.Y); } try { if (TreeView4.SelectedNode.Level == 0) { this.chartControl1.Series[0].Points.Clear(); //MessageBox.Show("父节点"); //SelectedChildNodeName = TreeView4.SelectedNode.Text.ToString(); //selectedParentNodeName = TreeView4.SelectedNode.Parent.Text.ToString(); ////读取当前信号的信号名 //string SignalName = SelectedChildNodeName; //SignalName = SignalName.Remove(0, 3); ////读取当前信号所属信息的ID string MessageIDStr = TreeView4.SelectedNode.Text.ToString(); MessageIDStr = MessageIDStr.Remove(0, 3); int MessageIDInt = int.Parse(MessageIDStr); ////遍历数据库,取A,B,和信号值 LINQDataContext context = new LINQDataContext(); var query = from sg in context.SendSignal where sg.ID == MessageIDInt select sg; string ABValue = null; string SignalValue = null; // MessageBox.Show("ssssss"); foreach (var item in query) { ABValue = item._A_B_; SignalValue = item.Signal_Value; int SplitIndexStart = ABValue.IndexOf(","); string AValueStr = ABValue.Substring(1, SplitIndexStart - 1); int SplitIndexEnd = ABValue.IndexOf(")"); string BValueStr = ABValue.Substring(SplitIndexStart + 1, SplitIndexEnd - SplitIndexStart - 1); int AValueInt = int.Parse(AValueStr); int BValueInt = int.Parse(BValueStr); //信号值转换为十进制 int SignalValueDecimal = Convert.ToInt32(SignalValue, 16); //推算出物理值 int Y = AValueInt * SignalValueDecimal + BValueInt; // MessageBox.Show(X,Y.ToString()); this.chartControl1.Series[0].Points.Add(new SeriesPoint(item.Signal_Name, Y)); } //int SplitIndexStart = ABValue.IndexOf(","); //string AValueStr = ABValue.Substring(1, SplitIndexStart - 1); //int SplitIndexEnd = ABValue.IndexOf(")"); //string BValueStr = ABValue.Substring(SplitIndexStart + 1, SplitIndexEnd - SplitIndexStart - 1); //int AValueInt = int.Parse(AValueStr); //int BValueInt = int.Parse(BValueStr); ////信号值转换为十进制 //int SignalValueDecimal = Convert.ToInt32(SignalValue, 16); ////推算出物理值 //int physics = AValueInt * SignalValueDecimal + BValueInt; //MessageBox.Show(physics.ToString()); } } catch (Exception) { ; } }
private void Form1_Load(object sender, EventArgs e) { try { SerialPort.PortName = "COM3"; SerialPort.BaudRate = 9600; SerialPort.Parity = Parity.None; SerialPort.StopBits = StopBits.One; SerialPort.DataBits = 8; SerialPort.Handshake = Handshake.None; SerialPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort_DataReceived); //将事件处理函数,挂接到事件DataReceived 中 SerialPort.Open(); } catch (Exception eee) { MessageBox.Show(eee.ToString()); } DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn(); col.HeaderText = "ID"; DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn(); col1.HeaderText = "Name"; DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn(); col2.HeaderText = "DLC"; DataGridViewTextBoxColumn col3 = new DataGridViewTextBoxColumn(); col3.HeaderText = "Data"; col3.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; dataGridView1.Columns.Add(col); dataGridView1.Columns.Add(col1); dataGridView1.Columns.Add(col2); dataGridView1.Columns.Add(col3); //DataGridViewRow dr = new DataGridViewRow(); //dataGridView1.Rows.Add(dr); //dr.Cells[0].Value = "aa"; // int index = dataGridView1.Rows.Add(); // dataGridView1.Rows[index].Cells[0].Value = "aaa"; //try //{ // DataGridViewRow dr = new DataGridViewRow(); // dataGridView1.Rows.Add(dr); // dr.Cells[0].Value = "a"; //} //catch (Exception eee) //{ // MessageBox.Show(eee.ToString()); //} LINQDataContext context = new LINQDataContext(); var query = from sm in context.SendMessage select sm; foreach (var item in query) { SendTextBox.AppendText(item.BO_.ToString()); SendTextBox.AppendText(item.ID.ToString()); SendTextBox.AppendText(item.Message_Name.ToString()); SendTextBox.AppendText(item.Separator.ToString()); SendTextBox.AppendText(item.DLC.ToString()); SendTextBox.AppendText(item.Node_Name.ToString()); SendTextBox.AppendText("\r\n"); string MessageIDStr = item.ID.ToString("x8"); //截取十六进制中的有效字段 int NotZeroIndex = 0; //第一个不为0的位置 foreach (var itemStr in MessageIDStr) { if (itemStr == '0') { NotZeroIndex++; } else { break; } } MessageIDStr = MessageIDStr.Substring(NotZeroIndex, 8 - NotZeroIndex); int index = dataGridView1.Rows.Add(); dataGridView1.Rows[index].Cells[0].Value = MessageIDStr; dataGridView1.Rows[index].Cells[1].Value = item.Message_Name; dataGridView1.Rows[index].Cells[2].Value = item.DLC.ToString(); TreeNode tn = TreeView.Nodes.Add(item.BO_.ToString().Trim() + item.ID.ToString().Trim()); var query1 = from sg in context.SendSignal where sg.ID == item.ID select sg; foreach (var item1 in query1) { TreeNode tn1 = new TreeNode(item1.SG_.ToString().Trim() + item1.Signal_Name.ToString().Trim()); tn.Nodes.Add(tn1); } } }
private void SelectMessageButton_Click(object sender, EventArgs e) { try { if (TreeView.SelectedNode.Level == 0) { string MessageSendChar = "T"; //读取当前信息的ID string MessageIDStr = TreeView.SelectedNode.Text.ToString(); MessageIDStr = MessageIDStr.Remove(0, 3); int MessageIDInt = int.Parse(MessageIDStr); //转换为十六进制 MessageIDStr = MessageIDInt.ToString("x8"); //截取十六进制中的有效字段 int NotZeroIndex = 0; //第一个不为0的位置 foreach (var item in MessageIDStr) { if (item == '0') { NotZeroIndex++; } else { break; } } MessageIDStr = MessageIDStr.Substring(NotZeroIndex, 8 - NotZeroIndex); MessageSendChar += MessageIDStr; LINQDataContext context = new LINQDataContext(); var query = from sm in context.SendMessage where sm.ID == MessageIDInt select sm; int DLC = 0; foreach (var item in query) { DLC = item.DLC; MessageSendChar += DLC.ToString(); } //MessageBox.Show(MessageSendChar); var query1 = from sg in context.SendSignal where sg.ID == MessageIDInt select sg; string SignalValue = null; int count = 0; foreach (var item in query1) { SignalValue = item.Signal_Value; SignalValue = SignalValue.Remove(0, 6); MessageSendChar += SignalValue; count++; } for (int i = 1; i <= DLC - count; i++) { MessageSendChar += "00"; } //MessageBox.Show(MessageSendChar); this.SendTextBox.AppendText(MessageSendChar); this.SendTextBox.AppendText("\r\n"); } else { MessageBox.Show("请选中需要发送的信息!"); } } catch (Exception) { MessageBox.Show("请选中需要发送的信息!"); } }