public Message(byte[] package) { string temp = new ASCIIEncoding().GetString(package); totalmessage = temp; temp = temp.Trim(); if (temp.StartsWith("<") && temp.EndsWith(">")) { temp = temp.Substring(1, temp.Length - 2); string[] strmsg = temp.Split(','); this.stationId = strmsg[0]; this.MsgId = Convert.ToInt32(strmsg[1]); this.MsgType = (MsgType)strmsg[2][0]; this.Info = strmsg[3]; if (strmsg.Length > 4) { this.DutID = strmsg[4]; } string[] strmsg2 = this.Info.Split(':'); if (strmsg2[0] == "E") { this.Info = "E"; this.ErrorInfo = strmsg2[1]; } } }
private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e) { byte[] ReDatas = new byte[comDevice.BytesToRead]; comDevice.Read(ReDatas, 0, ReDatas.Length); String sdata = new ASCIIEncoding().GetString(ReDatas); try { if (sdata.StartsWith("t")) { if (sdata.Length != 21) { //byte[] strBuffer = System.Text.Encoding.ASCII.GetBytes(yourASCIIString); string str = "wrong data length, please be check!"; MessageBox.Show(str); comDevice.Write(str); } if (int.Parse(sdata.Substring(4, 1)) > 8) { string str = "wrong data bit , please be check!"; MessageBox.Show(str); comDevice.Write(str); } if (!cal.InOrNot(sdata.Substring(1, 3))) { string str = "no this data id, please be check!"; MessageBox.Show(str); comDevice.Write(str); } } else if (sdata.StartsWith("T")) { if (sdata.Length != 26) { string str = "wrong data length, please be check!"; MessageBox.Show(str); comDevice.Write(str); } if (int.Parse(sdata.Substring(9, 1)) > 8) { string str = "wrong data bit , please be check!"; MessageBox.Show(str); comDevice.Write(str); } if (!cal.InOrNot(sdata.Substring(1, 8))) { string str = "no this data id, please be check!"; MessageBox.Show(str); comDevice.Write(str); } } else { string str = "wrong data, please be check!"; MessageBox.Show(str); comDevice.Write(str); } } catch { return; } // MessageBox.Show(sdata); if (sdata == null || sdata.Trim() == "") { String current_data = Message_DataBase.get_current_data(); if (current_data != null) { sdata = current_data; } return; } Message_DataBase.set_current_data(sdata); dic = cal.Decode(sdata); //dic长度为0 //MessageBox.Show("dic的长度为:" + dic.Count().ToString()); Font boldFont = new Font(treeGridView1.DefaultCellStyle.Font, FontStyle.Bold); if (this.IsHandleCreated) { this.BeginInvoke(new MethodInvoker(delegate { if (!message.Contains(cal.messagename)) { message.Add(cal.messagename); TreeGridNode node_temp = treeGridView1.Nodes.Add(null, cal.messagename, " "); foreach (KeyValuePair <string, string> kv in dic) { signalname.Add(kv.Key); node.Add(node_temp); node[i] = node_temp.Nodes.Add(null, kv.Key, kv.Value); node[i].ImageIndex = 0; node[i].DefaultCellStyle.Font = boldFont; i++; } } else { foreach (KeyValuePair <string, string> kv in dic) { for (int j = 0; j < signalname.Count(); j++) { if (kv.Key == signalname[j]) { node[j] = node[j].Nodes.Add(null, " ", kv.Value); node[j].ImageIndex = 1; node[j].DefaultCellStyle.Font = boldFont; } } } } /* if (!signalname.Contains(kv.Key)) * { * TreeGridNode node_temp = treeGridView1.Nodes.Add(null, cal.messagename, " "); * signalname.Add(kv.Key); * * //node.Add(new TreeGridNode()); * TreeGridNode node_temp = treeGridView1.Nodes.Add(null, kv.Key, kv.Value); * node.Add(node_temp); * // node[i]=treeGridView1.Nodes.Add(null, kv.Key, kv.Value); * node[i].ImageIndex = 0; * node[i].DefaultCellStyle.Font = boldFont; * i++; * } * else * {*/ })); } // MessageBox.Show("kv.Key"); /* * TreeGridNode node2 = treeGridView1.Nodes.Add(null, "kv.Key"," kv.Value"); * node2.ImageIndex = 0; * node2.DefaultCellStyle.Font = boldFont; * node2 = node2.Nodes.Add(null, "Re: Using DataView filter when binding to DataGridView", "tab"); * node2.ImageIndex = 1;*/ foreach (KeyValuePair <string, string> kv in dic) { // string temp = "\r\n this is here!"; string temp = kv.Key + "\'s physical value is :" + kv.Value; //向数据库中插入数据 DbManager.Ins.ConnStr = "server = localhost; user id = root; password = root; database = cantool"; string sql = @"INSERT into can_message(can_message.signal_name, can_message.signal_value,can_message.time) VALUES (?signal_name, ?signal_value, ?time)"; List <MySqlParameter> Paramter = new List <MySqlParameter>(); Paramter.Add(new MySqlParameter("?signal_name", kv.Key)); Paramter.Add(new MySqlParameter("?signal_value", kv.Value)); Paramter.Add(new MySqlParameter("?time", DateTime.Now.ToLocalTime().ToString())); int insert = DbManager.Ins.ExecuteNonquery(sql, Paramter.ToArray()); byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(temp); // this.AddData(byteArray); } }