C# 多组充电测试框架

有多个电流电压表通过串口连接到PC,点了对应按钮后,开始计时,到达对应时间后,读取电流电压表。
链接: https://caiyun.139.com/m/i?0E5CJYSahvlql
提取码:TDZa 复制内容打开和彩云手机APP,操作更方便哦

在这里插入图片描述

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

namespace ChargeTest10Slot
{
    public partial class Form1 : Form
    {

        private static bool Slot_01=false;
        private static bool Slot_02 =false;
        private static bool Slot_03 =false;

        System.Timers.Timer 间隔采样时间 = new System.Timers.Timer(1000);//实例化Timer类,设置间隔时间为1000毫秒;
        //private delegate void TextOption();//定义一个委托

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            秒表开始();
        }

        private void 秒表开始()
        {
            间隔采样时间.Elapsed += new System.Timers.ElapsedEventHandler(Run);//到达时间的时候执行事件;
            间隔采样时间.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
            间隔采样时间.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
        }

        public void Run(object source, System.Timers.ElapsedEventArgs e)
        {
            Action();

            //Task task1 = Task.Run(()=> {
            //    Action();
            //});


            //TextOption textOption = new TextOption(Action);
            //textOption.Invoke();
            //this.Invoke(new TextOption(Action));//invok 委托实现跨线程的调用
        }
        private void Action()
        {
            try
            {
                this.BeginInvoke(new Action(() => { label4.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); }));
                更新时间并检查是否读电压表();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void 更新时间并检查是否读电压表()
        {
            更新秒数(Slot_01, SlotLabel_01);
            更新秒数(Slot_02, SlotLabel_02);
            更新秒数(Slot_03, SlotLabel_03);
        }
        private void 更新秒数(bool Slot,Label label)
        {
            if (Slot)
            {
                label.BeginInvoke(new Action(() => {
                    label.Text = (int.Parse(label.Text)+1).ToString();
                }));
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Slot_01)
            {
                Slot_01 = false;
            }
            else
            {
                Slot_01 = true;
            }
            SetSlot(Slot_01, button1,SlotLabel_01);
        }
        private static void SetSlot(bool Slot,Button button,Label label)
        {
            if (Slot)
            {
                button.BeginInvoke(new Action(() => {
                    button.BackColor=Color.Green;
                    button.Text = "运行中……";
                    label.Text = "0";
                    ConfirmForm f = new ConfirmForm("已运行:"+label.Name);
                    f.ShowDialog();
                }));
            }
            else
            {
                button.BeginInvoke(new Action(() => {
                    button.BackColor = Color.Red;
                    button.Text = "停止";
                    ConfirmForm f = new ConfirmForm("已停止:"+label.Name);
                    f.ShowDialog();
                }));
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (Slot_02)
            {
                Slot_02 = false;
            }
            else
            {
                Slot_02 = true;
            }
            SetSlot(Slot_02, button2,SlotLabel_02);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (Slot_03)
            {
                Slot_03 = false;
            }
            else
            {
                Slot_03 = true;
            }
            SetSlot(Slot_03, button3, SlotLabel_03);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值