Exemplo n.º 1
0
 private void button6_MouseUp(object sender, MouseEventArgs e)
 {
     SendLength = 0;
     RcveLength = 0;
     toolStripStatusLabel1.Text = "发送" + " " + SendLength.ToString("d");
     toolStripStatusLabel2.Text = "接收" + " " + RcveLength.ToString("d");
 }
Exemplo n.º 2
0
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) //串口接收数据
        {
            byte[]        buffer          = new byte[serialPort1.ReadBufferSize];                           //缓存区大小
            StringBuilder MyStringBuilder = new StringBuilder(serialPort1.ReadBufferSize * 2);              //实际大小的二倍存储数据

            if (button1.Text == "打开串口")
            {
                return;//串口未打开
            }
            //三个参数含义:(被写入数据组,缓冲区数组中开始写入的偏移量,要读取的字节数),返回值读取的字节数
            int length = serialPort1.Read(buffer, RcveOffset, buffer.Length - 1);

            if (checkBox2.Checked == true)     //显示数据
            {
                if (checkBox1.Checked == true) //16进制显示模式,要进行转化再显示
                {
                    RcveOffset = 0;            //偏移量为0
                    for (int i = 0; i < length; i++)
                    {
                        MyStringBuilder.Append(String.Format("{0:X2}", Convert.ToInt32(buffer[i])) + " ");
                    }
                    this.Invoke(new SetricrichTextBox1Callback(SetrichTextBox1), MyStringBuilder.ToString());
                    this.Invoke(new SetChart1Callback(SetChart1), MyStringBuilder.ToString());
                }
                else//即RcveMode == 0或checkBox1.Checked == false
                {
                    byte[] hz = new byte[2];
                    int    j  = 0;

                    if (RcveOffset == 1)
                    {
                        buffer[0] = RcveTemp;
                        length   += 1;
                    }
                    for (int i = 0; i < length; i++)
                    {
                        if (buffer[i] < 0x80)
                        {
                            if (buffer[i] == '\n')
                            {
                                MyStringBuilder.Append("\r\n");
                            }
                            else
                            {
                                MyStringBuilder.Append(Convert.ToChar(buffer[i]));
                            }
                            j = 0;
                        }
                        else if (j == 0)
                        {
                            hz[0] = buffer[i];
                            j     = 1;
                        }
                        else
                        {
                            hz[1] = buffer[i];
                            MyStringBuilder.Append(System.Text.Encoding.Default.GetString(hz));
                            j = 0;
                        }
                    }
                    if (j == 1)
                    {
                        RcveOffset = 1;
                        RcveTemp   = buffer[length - 1];
                    }
                    else
                    {
                        RcveOffset = 0;
                    }
                    this.Invoke(new SetricrichTextBox1Callback(SetrichTextBox1), MyStringBuilder.ToString());
                    this.Invoke(new SetChart1Callback(SetChart1), MyStringBuilder.ToString());
                }
            }
            RcveLength += length;                                               //总接收数据计数
            toolStripStatusLabel2.Text = "接收" + " " + RcveLength.ToString("d"); //状态栏显示总接收数
        }
Exemplo n.º 3
0
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            byte[]        buffer          = new byte[serialPort1.ReadBufferSize];
            StringBuilder MyStringBuilder = new StringBuilder(serialPort1.ReadBufferSize * 2);

            if (button1.Text == "打开串口")
            {
                return;
            }
            int length = serialPort1.Read(buffer, RcveOffset, buffer.Length - 1);

            if (checkBox2.Checked == true)
            {
                if (RcveMode == 1)
                {
                    for (int i = 0; i < length; i++)
                    {
                        NodeList.Append(buffer[i]);
                    }
                    this.Invoke(new Action(ReceivedToGraphics));
                }
                else if (checkBox1.Checked == true)
                {
                    RcveOffset = 0;
                    for (int i = 0; i < length; i++)
                    {
                        MyStringBuilder.Append(String.Format("{0:X2}", Convert.ToInt32(buffer[i])) + " ");
                    }
                    this.Invoke(new SetricrichTextBox1Callback(SetrichTextBox1), MyStringBuilder.ToString());
                }
                else
                {
                    byte[] hz = new byte[2];
                    int    j  = 0;

                    if (RcveOffset == 1)
                    {
                        buffer[0] = RcveTemp;
                        length   += 1;
                    }
                    for (int i = 0; i < length; i++)
                    {
                        if (buffer[i] < 0x80)
                        {
                            if (buffer[i] == '\n')
                            {
                                MyStringBuilder.Append("\r\n");
                            }
                            else
                            {
                                MyStringBuilder.Append(Convert.ToChar(buffer[i]));
                            }
                            j = 0;
                        }
                        else if (j == 0)
                        {
                            hz[0] = buffer[i];
                            j     = 1;
                        }
                        else
                        {
                            hz[1] = buffer[i];
                            MyStringBuilder.Append(System.Text.Encoding.Default.GetString(hz));
                            j = 0;
                        }
                    }
                    if (j == 1)
                    {
                        RcveOffset = 1;
                        RcveTemp   = buffer[length - 1];
                    }
                    else
                    {
                        RcveOffset = 0;
                    }
                    this.Invoke(new SetricrichTextBox1Callback(SetrichTextBox1), MyStringBuilder.ToString());
                }
            }
            RcveLength += length;
            toolStripStatusLabel2.Text = "接收" + " " + RcveLength.ToString("d");
        }