Exemple #1
0
        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口");
            }
        }
Exemple #2
0
        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);
            }
        }