using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace serial_SPEED_TEST
{
    public partial class Form1 : Form
    {
        Byte[] buf = new Byte [4024];
        System.Threading.Thread nt;
        public Form1()
        {
            InitializeComponent();
            nt = new System.Threading.Thread(new System.Threading.ThreadStart(Thread_send));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!serialPort1.IsOpen)
                serialPort1.Open();
            if (nt.ThreadState == System.Threading.ThreadState.Suspended)
                nt.Resume();
            else
                nt.Start();
            timer1.Enabled = true;
        }
        private void Thread_send()
        {
            while (true) {
                if (serialPort1.BytesToWrite < 100)
                {
                    serialPort1.WriteLine("1234567890");
                    System.Threading.Thread.Sleep(1);
                }
                else
                    System.Threading.Thread.Sleep(100);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            nt.Suspend();
            timer1.Enabled = false;
        }

        private void closing(object sender, FormClosingEventArgs e)
        {
            timer1.Enabled = false;
            System.Threading.Thread.Sleep(50);
            if (nt.ThreadState != System.Threading.ThreadState.Suspended)
                nt.Abort();
            serialPort1.Close();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text=serialPort1.ReadExisting().Length.ToString();
        }

    }
}
