예제 #1
0
        public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int i = 0;

            if (radioButton4.Checked)
            {
                textBox1.AppendText(serialPort1.ReadExisting());
                string str = serialPort1.ReadExisting();
                Class1.datas = str;
            }
            else if (radioButton3.Checked)
            {
                byte[] data = new byte[serialPort1.BytesToRead];                                //定义缓冲区,因为串口事件触发时有可能收到不止一个字节
                serialPort1.Read(data, 0, data.Length);
                // if (displayer != null)
                //    displayer.AddData(data);
                foreach (byte Member in data)                                                   //遍历用法
                {
                    string str = Convert.ToString(Member, 16).ToUpper();
                    Class1.datas = str;
                    textBox1.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");
                }
            }
            else
            {
                //  serialPort1.r
                byte[] data = new byte[serialPort1.BytesToRead];                                //定义缓冲区,因为串口事件触发时有可能收到不止一个字节
                serialPort1.Read(data, 0, data.Length);
                byte  data1;
                int   t      = data.Length;
                int[] Member = new int[t];
                for (int j = 0; j < 2; j++)
                //  foreach (byte Member in data)                                                   //遍历用法
                {
                    //   Member[j]= data[j];
                    //   string str = Convert.ToString(Member, 10).ToUpper();
                    //  Class1.datas = str;
                    //  int k = 0;
                    //  k = j%2;
                    Member[j] = data[j];
                    textBox1.AppendText(" " + Member[j]);
                    Class1.datas = Convert.ToString(Member[0]);
                    Class1.datat = Convert.ToString(Member[1]);
                    data1        = data[1];

                    switch (Member[1])
                    {
                    case 0: st = "无"; break;

                    case 1: st = "有"; break;

                    default: st = "  "; break;
                    }

                    switch (Member[0])
                    {
                    case 2:; textBox3.Text = st + "光照"; if (displayer != null)
                        {
                            displayer.AddData(data1);
                        }
                        break;

                    case 3: textBox4.Text = st + "障碍"; if (displayer != null)
                        {
                            displayer.AddData2(data1);
                        }
                        break;

                    case 12: textBox5.Text = st + "振动"; if (displayer != null)
                        {
                            displayer.AddData3(data1);
                        }
                        break;
                        // default: textBox3.Text = ""; break;
                    }
                }
            }
        }