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) { SerialPort comDevice = Com.get_com(); if (comDevice != null) { byte[] ReDatas = new byte[comDevice.BytesToRead]; comDevice.Read(ReDatas, 0, ReDatas.Length); String sdata = new ASCIIEncoding().GetString(ReDatas); if (sdata == null || sdata.Trim() == "") { String current_data = Message_DataBase.get_current_data(); if (current_data != null) { sdata = current_data; } else { return; } } Message_DataBase.set_current_data(sdata); Dictionary <string, string> dic = new Dictionary <string, string>(); Calculate cal = new Calculate(); dic = cal.Decode(sdata); if (this.IsHandleCreated) { this.BeginInvoke(new MethodInvoker(delegate { //for (int i = 0; i < 10; i++) //{ //} //aGauge2.Value = count; //count = count + 10; Signal selected_signal = (Signal)this.cbx_signal.SelectedItem; foreach (KeyValuePair <string, string> kv in dic) { // string temp = "\r\n this is here!"; if (kv.Key == selected_signal.signalName) { //MessageBox.Show(kv.Key); //MessageBox.Show(selected_signal.signalName); //MessageBox.Show(kv.Value); //MessageBox.Show(selected_signal.C.ToString()); //MessageBox.Show(selected_signal.D.ToString()); this.aGauge2.MinValue = Convert.ToSingle(selected_signal.C); //取整数 //间隔 int space = (Convert.ToInt32(selected_signal.D + 10)) % 10; if (Convert.ToSingle(selected_signal.D) <= 2) { this.aGauge2.MaxValue = 2; } else { this.aGauge2.MaxValue = Convert.ToSingle(selected_signal.D + 10 - space); } aGauge2.ScaleLinesMajorStepValue = (aGauge2.MaxValue - aGauge2.MinValue) / 10; this.aGauge2.Value = Convert.ToSingle(kv.Value); this.label1.Text = kv.Value; break; } } })); } } else { MessageBox.Show("请设置COM口"); } }
//PLC message convert public MainPLCMessage(byte[] data) { string temp = new ASCIIEncoding().GetString(data); temp = temp.Trim(); string messageState = ""; string errorCode = ""; //useful message no less than message head if (temp.Length >= msgHeadLength) { this.MsgType = (PLCMsgType)temp[0]; //get messagetype this.MsgHead = temp.Substring(0, msgHeadLength); //get message head this.InvalidMessage = false; //vaild message // messageState = temp.Substring(4, errCodeLength); switch (this.MsgType) { case PLCMsgType.Feedback: this.Info = ""; if (this.MsgHead == MoveDUTToStationACK) { this.StationId = Convert.ToInt16(temp.Substring(4, _StationIdLength)); messageState = temp.Substring(6, errCodeLength); if (messageState == "00") { this.isok = true; this.Info = " DUT placed to Station " + StationId; } else if (messageState == "01") { this.isok = false; //error code handle add to this.info } } else if (this.MsgHead == MoveDUTFromStationACK) { this.StationId = Convert.ToInt16(temp.Substring(4, _StationIdLength)); messageState = temp.Substring(6, errCodeLength); if (messageState == "00") { this.isok = true; this.Info = "DUT move away from Station " + StationId; } else if (messageState == "01") { this.isok = false; //error code handle add to this.info } } else if (this.MsgHead == queryPLCStateACK) { messageState = temp.Substring(4, errCodeLength); if (messageState == "00") { this.isok = true; this.PLCIsBusy = false; } else if (messageState == "01") { this.isok = true; this.PLCIsBusy = true; } else if (messageState == "02") { this.isok = false; //error code handle add to this info } } break; case PLCMsgType.Heartbead: this.Info = "Heartbeat"; break; case PLCMsgType.ReportMsg: if (this.MsgHead == DUTIdToServer) { this.isok = true; this.Info = temp.Substring(4, dutIDLength); } break; case PLCMsgType.Ack: this.Info = "Ack"; break; default: break; } } else { this.InvalidMessage = true; this.Info = temp; } }
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); } }
private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e) { SerialPort comDevice = Com.get_com(); if (comDevice != null) { byte[] ReDatas = new byte[comDevice.BytesToRead]; comDevice.Read(ReDatas, 0, ReDatas.Length); String sdata = new ASCIIEncoding().GetString(ReDatas); if (sdata == null || sdata.Trim() == "") { String current_data = Message_DataBase.get_current_data(); if (current_data != null) { sdata = current_data; } else { return; } } Message_DataBase.set_current_data(sdata); Dictionary <string, string> dic = new Dictionary <string, string>(); Calculate cal = new Calculate(); dic = cal.Decode(sdata); if (this.IsHandleCreated) { this.BeginInvoke(new MethodInvoker(delegate { //for (int i = 0; i < 10; i++) //{ //} //aGauge2.Value = count; //count = count + 10; Signal selected_signal = (Signal)this.cbx_signal.SelectedItem; foreach (KeyValuePair <string, string> kv in dic) { if (kv.Key == selected_signal.signalName) { if (this.chart1.Series.Count > 0) { this.chart1.Series[0].Points.Clear(); } dataQueue.Enqueue(Convert.ToDouble(kv.Value)); for (int i = 0; i < dataQueue.Count; i++) { this.chart1.Series[0].Points.AddXY((i + 1), dataQueue.ElementAt(i)); } break; } } })); } } else { MessageBox.Show("请设置COM口"); } }