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

namespace Regulator_Teploty
{
    public partial class TempReg : Form
    {
        public string test = " ";
        public string send;
        public string channel;
        public int counter;
      
        public TempReg()
        {
            InitializeComponent();
           
            if (serialPort1.IsOpen == true)
            {
                button1.Text = "Disconnect";
            }
            else
            {
                button1.Text = "Connect";
            }
            if (timer2.Enabled == true)
            {
                status.Text = "measuring...";
            }
            else
            {
                status.Text = "STOP";
                counter = 0;
        
            }
            label1.Text = Convert.ToString(trackBar1.Value);
        }
        private void rbCOM4_CheckedChanged(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM4";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen == true)
            {
                serialPort1.Close();
                timer1.Enabled = false;
                button1.Text = "Connect";
            }
            else
            {
                serialPort1.Open();
                button1.Text = "Disconnect";
                timer1.Enabled = true;
            }
        }

        private void bSend_Click(object sender, EventArgs e)
        {
          
            
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            while (serialPort1.BytesToRead > 0)
            {
                test += Convert.ToChar(serialPort1.ReadByte());
            }

        }

       
        private void timer1_Tick(object sender, EventArgs e)
        {

            if (test != "")
            {
                richTextBox1.AppendText(test);
                test = "";
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
            richTextBox1.SaveFile(saveFileDialog1.FileName);
        }

        private void b_CH1_Click(object sender, EventArgs e)
        {
            serialPort1.Write("0");
        }

        private void b_CH1_Click_1(object sender, EventArgs e)
        {
            serialPort1.Write("1");
        }

        private void b_start_Click(object sender, EventArgs e)
        {
            if (timer2.Enabled == true)
                timer2.Enabled = false;
            else
            {
                timer2.Enabled = true;
            }
            if (timer2.Enabled == true)
            {
                status.Text = "measuring...";
                b_start.Text = "STOP";
            }
            else
            {
                status.Text = "STOP";
                b_start.Text = "START";
            }
        }

        private void b_clear_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            serialPort1.Write(channel);
            counter++;
            if (counter == 60)
            {
                richTextBox1.SaveFile(saveFileDialog1.FileName);
                counter = 0;
            }
            else
            {
            }   
        }

        private void rb_0_5_Click(object sender, EventArgs e)
        {
            timer2.Interval = 500;
        }

        private void rb_1_Click(object sender, EventArgs e)
        {
            timer2.Interval = 1000;
        }

        private void rb_10_Click(object sender, EventArgs e)
        {
            timer2.Interval = 10000;
        }

        private void rb_CH0_CheckedChanged(object sender, EventArgs e)
        {
            channel = "0";
        }

        private void rb_CH1_CheckedChanged(object sender, EventArgs e)
        {
            channel = "1";
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            label1.Text = Convert.ToString(trackBar1.Value * 25); 
            switch(trackBar1.Value)
            {
                case 0: serialPort1.Write("A"); break;
                case 1: serialPort1.Write("B"); break;
                case 2: serialPort1.Write("C"); break;
                case 3: serialPort1.Write("D"); break;
                case 4: serialPort1.Write("E"); break;
            }
            
        }

        

 

       
        
    }
}