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;
using System.IO.Ports;

namespace LightAutoApp
{
    public partial class Form1 : Form
    {
      private  SerialPort comm = new SerialPort();
      string strnewdata = "";
      bool step_on;
      bool IsAuto;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            this.comboPortName.Items.AddRange(ports);
            this.comboPortName.SelectedIndex = comboPortName.Items.Count > 0 ? 0 : -1;
            comm.DataReceived += Comm_DataReceived;
            step_on = false;
            IsAuto = false;
        }

        void Comm_DataReceived(object sender ,SerialDataReceivedEventArgs e)
        {
            this.BeginInvoke(new Action(() =>
                {
                    string serialdata = comm.ReadExisting();
                    strnewdata += serialdata;
                    if(strnewdata.LastIndexOf("111111")>=0)
                    {
                        txtlight.Text = "无光照";
                        this.pictureBoxlight.Image = LightAutoApp.Resource1.lightOff;
                        strnewdata = "";
                    }
                    if(strnewdata.LastIndexOf("222222")>=0)
                    {
                        txtlight.Text = "有光照";
                        this.pictureBoxlight.Image = LightAutoApp.Resource1.lightOn;
                        strnewdata = "";
                    }
                }), null);   
        }

        private void buttonOpenCloseCom_Click(object sender, EventArgs e)
        {
              if(comm.IsOpen)
              {
                  comm.Close();
                  txtlight.Text = "";
                  btnStepoff.Enabled = false;
                  btnStepon.Enabled = false;
              }
              else
              {
                  comm.PortName = this.comboPortName.Text;
                  comm.BaudRate = 9600;
                  try
                  {
                      comm.Open();  
                  }
                  catch(Exception ex)
                  {
                      comm = new SerialPort();
                      MessageBox.Show(ex.Message);
                  }

              }

              buttonOpenCloseCom.Text = comm.IsOpen ? "关闭串口" : "打开串口";
              if (step_on)
              {
                  btnStepon.Enabled = false;
                  btnStepoff.Enabled = true;
              }
              else
              {
                  btnStepon.Enabled = true;
                  btnStepoff.Enabled = false;
              }
        }

        private void btnStepon_Click(object sender, EventArgs e)
        {
              if(IsAuto==false&&comm.IsOpen)
              {
                  if(!step_on)
                  {
                      comm.Write("297");
                      System.Threading.Thread.Sleep(500);
                      btnStepon.Enabled = false;
                      btnStepoff.Enabled = true;
                      step_on = true;
                  }
              }
        }

        private void btnStepoff_Click(object sender, EventArgs e)
        {
                if(IsAuto==false&&comm.IsOpen)
                {
                   if(step_on)
                   {
                       comm.Write("2A7");
                       System.Threading.Thread.Sleep(500);
                       btnStepon.Enabled = true;
                       btnStepoff.Enabled=false;
                       step_on=false;
                   }
                }
        }

        private void cbkAutomode_CheckedChanged(object sender, EventArgs e)
        {
            if (cbkAutomode.Checked)
            {
                IsAuto = true;
                timer1.Enabled = true;
                btnStepoff.Enabled = false;
                btnStepon.Enabled = false;
            }
            else
            {
                IsAuto = false;
                timer1.Enabled = false;
                if (step_on)
                {
                    btnStepoff.Enabled = true;
                    btnStepon.Enabled = false;
                }
                else
                {
                    btnStepon.Enabled = true;
                    btnStepoff.Enabled = false;
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if(IsAuto==true&&comm.IsOpen)
            {
                if (txtlight.Text == "无光照")
                {
                    if (!step_on)
                    {
                        comm.Write("297");
                        System.Threading.Thread.Sleep(500);
                        step_on = true;
                        this.pictureBoxstep.Image = LightAutoApp.Resource1.curtainOff;
                    }

                }
                else
                { 
                    if(txtlight.Text=="有光照")
                    {
                      if(step_on)
                      {
                          comm.Write("2A7");
                          System.Threading.Thread.Sleep(500);
                          step_on = false;
                          this.pictureBoxstep.Image = LightAutoApp.Resource1.curtainOn;
                      }
                    }
                }
            }
        }

       
    }
}
