夜光带你走进C# winform&电气单片机(二十八)擅长的领域

夜光序言:

 

 

外貌决定有没有可能在一起,性格决定适不适合在一起,物质决定能不能稳定的在一起,信任决定能不能长久的在一起。

 

 

 

 

 

 

 

 

 

 

正文:

我们之前用单片机可能实现过定时的功能

比如说:让灯多少时间之后关闭


这一点就很厉害


 

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 WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        //device1 hy
        const byte DeviceOpen1 = 0x01;  //定义了一个常量,这个也是C语言通用的
        const byte DeviceClose1 = 0x81;  //夜光:开数据是01,关数据是81
        //device2 hy
        const byte DeviceOpen2 = 0x02;
        const byte DeviceClose2 = 0x82;

        //device3 hy
        const byte DeviceOpen3 = 0x03;
        const byte DeviceClose3 = 0x83;

        bool Button9Statue;

        //SerialPort Write Buffer
        byte[] SerialPortDataBuffer = new byte[1];

        public Form1()
        {
            InitializeComponent();   //夜光:窗口构造
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)   //串口打开就关闭
            {
                
                try
                {
                    serialPort1.Close();
                }
                catch                 //确保万无一失
                {
                    
                }
                ovalShape1.FillColor = Color.Gray;  //未打开,灰色
                button2.Text = "打开串口";
            }
            else
            {
                try
                {
                    serialPort1.PortName = comboBox1.Text;  //端口号
                    serialPort1.Open();
                    ovalShape1.FillColor = Color.Green;  //打开的就是绿色
                    button2.Text = "关闭串口";
                }
                catch
                {
                    MessageBox.Show("串口打开失败", "错误");
                }
            }




        }

        //加载的时候
        private void Form1_Load(object sender, EventArgs e)
        {
            ovalShape1.FillColor = Color.Gray;  //启动的时候,设置颜色
            SearchAndAddSerialToComboBox(serialPort1, comboBox1);
        }


        private void WriteByteToSerialPort(byte data)   //夜光:单字节写入串口
        {
            byte[] Buffer = new byte[2] { 0x00, data };  //夜光:定义数组
            if(serialPort1.IsOpen){         //传送数据的前提是端口已经打开
                try
                {
                    serialPort1.Write(Buffer, 0, 2);
                }                                           //写数据
                catch
                {
                    MessageBox.Show("串口数据发送出错,请检查", "错误");   //错误处理
                }
            }

        }




        private void SearchAndAddSerialToComboBox(SerialPort MyPort, ComboBox MyBox)
        {                                           //将可用端口号添加到ComboBox
            string[] MyString = new string[20];      //最多容纳20个,太多会影响效率
            string Buffer;          //缓存
            MyBox.Items.Clear();                  //清空ComboBox内容
            for (int i = 1;i < 20; i++)           //循环
            {
                try                                             //核心原理是依赖try catch完成遍历
                {
                    Buffer = "COM" + i.ToString();
                    MyPort.PortName = Buffer;
                    MyPort.Open();                     //如果失败,后面的代码不会执行
                    MyString[i - 1] = Buffer;
                    MyBox.Items.Add(Buffer);    //打开成功,添加至下俩列表
                    MyPort.Close();             //关闭
                }
                catch
                {

                }
                MyBox.Text = MyString[0];
            }



        }

        private void button3_Click(object sender, EventArgs e)
        {
            int i = 0;
            try
            {
                i = Convert.ToInt32(textBox1.Text.Substring(0, 2));  //先处理两位数
            }
            catch
            {
                try
                {
                    i = Convert.ToInt32(textBox1.Text.Substring(0, 1));//处理一位数
                }
                catch
                {
                    MessageBox.Show("请输入正确的数字");  //错误提示
                    return;          //退出函数
                }
            }
            if (serialPort1.IsOpen)
            {
                if(i == 0)
                {
                    return;
                }
                else
                {
                    timer1.Interval = i * 1000;
                    timer1.Start();
                    button3.Enabled = false;
                }
            }

            /*WriteByteToSerialPort(DeviceOpen1);          //器件1开*/
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //夜光:这个是定时器关的
            try
            {
                timer1.Stop();
            }
            catch
            {

            }
            button3.Enabled = true;
            WriteByteToSerialPort(DeviceClose1);          //器件1关
        }

        private void button6_Click(object sender, EventArgs e)
        {
            WriteByteToSerialPort(DeviceOpen2);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            WriteByteToSerialPort(DeviceClose2);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            WriteByteToSerialPort(DeviceOpen3);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            WriteByteToSerialPort(DeviceClose3);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SearchAndAddSerialToComboBox(serialPort1, comboBox1);
        }

        private void button9_Click(object sender, EventArgs e)
        {
          


                                      //避免定时器浪费时间和用户
            if (serialPort1.IsOpen)   //串口打开就关闭  
            {

                try
                {
                    serialPort1.Close();
                }
                catch                 //确保万无一失
                {

                }
                ovalShape1.FillColor = Color.Gray;  //未打开,灰色
                button9.BackgroundImage = Properties.Resources.Image2;
                /*                button2.Text = "打开串口";*/
            }
            else
            {
                try
                {
                    serialPort1.PortName = comboBox1.Text;  //端口号
                    serialPort1.Open();
                    button9.BackgroundImage = Properties.Resources._09f9d033140dcdff688d0e67dd1b3e20;
                    ovalShape1.FillColor = Color.Green;  //打开的就是绿色
/*                    button2.Text = "关闭串口";*/
                }
                catch
                {
                    MessageBox.Show("串口打开失败", "错误");
                }
            }
        }

        private void button9_MouseHover(object sender, EventArgs e)
        {
            button9.BackgroundImage = Properties.Resources._09f9d033140dcdff688d0e67dd1b3e20;
        }

        private void button9_MouseLeave(object sender, EventArgs e)
        {
            if (Button9Statue)  //定义一个布尔类型的全局变量Button9Statue
            {
                button9.BackgroundImage = Properties.Resources.Image2;
            }
            else
            {
                button9.BackgroundImage = Properties.Resources.Image3;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            button3.Enabled = true;
            timer1.Stop();
            WriteByteToSerialPort(DeviceClose1);
        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值