基本信息
源码名称:程控电源(数字电源-电压可自由设定)
源码大小:3.91M
文件格式:.zip
开发语言:C#
更新时间:2019-10-18
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

电压可自由设定,也可直接设置DAC,两步配合操作,输出电压精度可达正负10mv

 


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


namespace Digital_Voltage_Sourse
{
    public partial class Form1 : Form
    {
        bool PowerPress = false;
        bool VoicePress = false;
        float OutputVoltage;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            groupBox2.Enabled = false;
            picBoxPowr.Enabled = false;
            picBoxVoice.Enabled = false;
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            comBoxPort.Items.AddRange(ports);
            comBoxBaudRate.Items.Add("115200");
            comBoxBaudRate.Items.Add("76800");
            comBoxBaudRate.Items.Add("57600");
            comBoxBaudRate.Items.Add("38400");
            comBoxBaudRate.Items.Add("19200");
            comBoxBaudRate.Items.Add("9600");
        }

        bool PowrOn = false;
        private delegate void Delelabel();
        private void pictureBox1_Click(object sender, EventArgs e)
        {

            if (serialPort1.IsOpen & PowerPress)
            {
                PowrOn = (PowrOn == false ? true : false);

                if (PowrOn)
                {
                    groupBox2.Enabled = true;
                    picBoxPowr.Image = Properties.Resources._C1;
                    comBoxVolStep.Items.Clear();
                    comBoxVolStep.Items.Add("1dac");
                    comBoxVolStep.Items.Add("2dac");
                    comBoxVolStep.Items.Add("5dac");
                    timer1.Enabled = true;

                }
                else
                {
                    picBoxPowr.Image = Properties.Resources._C2;
                    groupBox2.Enabled = false;
                    timer1.Enabled = false;
                    comBoxVolStep.Text=null;
                    texBDisVoltage.Text = null;
                }
                PowerPress = false;

            }
        }

        private void picBoxPowr_MouseDown(object sender, MouseEventArgs e)
        {
            picBoxPowr.Size = new Size(91, 97);
            serialPort1.WriteLine("PRESS TASK ID 1\r\n");  
        }

        private void picBoxPowr_MouseUp(object sender, MouseEventArgs e)
        {

            picBoxPowr.Size = new Size(92, 98);
            
        }

        private void picBoxVoice_MouseDown(object sender, MouseEventArgs e)
        {
            picBoxVoice.Size = new Size(20, 19);
            serialPort1.WriteLine("VOICE BUTTON PRESS\r\n");
        }

        private void picBoxVoice_MouseUp(object sender, MouseEventArgs e)
        {
            picBoxVoice.Size = new Size(20, 20);
        }



        bool VoiceOn = true;
        private void picBoxVoice_Click(object sender, EventArgs e)
        {
           if (serialPort1.IsOpen & VoicePress)
            {
                VoiceOn = (VoiceOn == true ? false : true);

                if (VoiceOn)
                {
                    picBoxVoice.Image = Properties.Resources._Audion;
                }
                else
                {
                    picBoxVoice.Image = Properties.Resources._Audiof;
                }
                VoicePress = false;
            }
            
        }

        private void butnconnect_Click(object sender, EventArgs e)
        {
            if (butnconnect.Text == "连接设备")
            {
                try
                {
                    serialPort1.PortName = comBoxPort.Text;//串口号
                    serialPort1.BaudRate = Int32.Parse(comBoxBaudRate.Text);//波特率
                    serialPort1.StopBits = System.IO.Ports.StopBits.One;//停止位
                    serialPort1.DataBits = 8;//数据位
                    serialPort1.Parity = System.IO.Ports.Parity.None;
                    serialPort1.Open();
                }
                catch (Exception)
                {
                    MessageBox.Show("串口连接有误", "Error");
                }
            }
            else if (butnconnect.Text == "断开设备")
            {
                if (PowrOn)
                {
                    MessageBox.Show("请先关闭电源", "提醒");
                }
                else
                  serialPort1.Close();
            }
            if (serialPort1.IsOpen)
            {
                butnconnect.ForeColor = System.Drawing.Color.Red;
                butnconnect.Text = "断开设备";
                picBoxPowr.Enabled = true;
                picBoxVoice.Enabled = true;
                comBoxPort.Enabled = false;
                comBoxBaudRate.Enabled = false;
            }
            else
            {
                texBDisVoltage.Text = null;
                picBoxVoice.Enabled = false;
                picBoxPowr.Enabled = false;
                comBoxPort.Enabled = true;
                comBoxBaudRate.Enabled = true;
                butnconnect.ForeColor = System.Drawing.Color.Black;
                butnconnect.Text = "连接设备";
            }

        }


        //串口接收函数
        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string RevData;
            try
            {
                RevData = serialPort1.ReadLine();
                RevData = RevData.Substring(0, RevData.IndexOf('\r'));
                if (RevData.CompareTo("Task even process ok") == 0)
                {
                    PowerPress = true;//电源按键按下了
                }
                else if (RevData.CompareTo("Voice button process ok") == 0)
                {
                    VoicePress = true;//声音按键按下了
                }
                else if (RevData.Contains("voltage="))
                {
                    int Dat1,Dat2;
                    Dat1 = RevData.Length;
                    Dat2 = RevData.IndexOf('=') 1;

                    RevData = RevData.Substring(Dat2, Dat1 - Dat2);
                    
                    OutputVoltage = Convert.ToSingle(RevData);
                    OutputVoltage /= 1000;

 
                }
            }
            catch (Exception)
            {
                ;
            }
            serialPort1.DiscardInBuffer();
            if(PowrOn)
               this.Invoke(new Delelabel(Char_Feedback));
        }

        private void Char_Feedback()
        {
            if (OutputVoltage == 0) texBDisVoltage.Text = null;
            else texBDisVoltage.Text = OutputVoltage.ToString("f3")   "V";
        }
        
        
        
        
        
        //定时器
        private void timer1_Tick(object sender, EventArgs e)
        {
            serialPort1.WriteLine("GET OUTPUT VOLTAGE\r\n");
        }

        private void butn3v3_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("SET VOLTAGE TO 03300\r\n");
        }

        private void butn5v0_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("SET VOLTAGE TO 05000\r\n");
        }

        private void butn12v0_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("SET VOLTAGE TO 12000\r\n");
        }

        private void butn15v0_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("SET VOLTAGE TO 15000\r\n");
        }

        private void butnSetting_Click(object sender, EventArgs e)
        {
            int InputVoltage;
            if (mtexBvoltageInput.Text == "") MessageBox.Show("设定值不能为空", "ERROR");
            else
            {

                InputVoltage = Convert.ToInt16(mtexBvoltageInput.Text);
                if ((InputVoltage > 19000) || (InputVoltage < 3200) || (mtexBvoltageInput.Text.First() == '0'))
                {
                    if (mtexBvoltageInput.Text.First() == '0') MessageBox.Show("设定值首位不能为0", "ERROR");
                    else MessageBox.Show("设定值超出范围\r\n3200~19000", "ERROR");
                }
                else
                {
                    string dat;
                    dat = mtexBvoltageInput.Text;
                    if (dat.Length == 4) serialPort1.WriteLine("SET VOLTAGE TO 0"   dat   "\r\n");
                    else serialPort1.WriteLine("SET VOLTAGE TO "   dat   "\r\n");

                }
                mtexBvoltageInput.Text = null;
            }
        }



        private void maskedTextBox1_Click(object sender, EventArgs e)
        {
           this.mtexBvoltageInput.Select(0,0);

        }

        private void butnVolAdd_Click(object sender, EventArgs e)
        {
            if (comBoxVolStep.Text == "")
            {
                MessageBox.Show("输入参数不能为空", "Error");
            }
            else
            {
                string advalue;
                advalue = comBoxVolStep.Text.Substring(0, 1);
                serialPort1.WriteLine("DAC VALUE ADD "   advalue   "\r\n");
            }
        }

        private void butnVolReduce_Click(object sender, EventArgs e)
        {
            if (comBoxVolStep.Text == "")
            {
                MessageBox.Show("输入参数不能为空", "Error");
            }
            else
            {
                string advalue;
                advalue = comBoxVolStep.Text.Substring(0, 1);
                serialPort1.WriteLine("DAC VALUE REDUCE "   advalue   "\r\n");
            }
        }

        private void comBoxPort_Click(object sender, EventArgs e)
        {
            comBoxPort.Text = "";
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            comBoxPort.Items.Clear();
            comBoxPort.Items.AddRange(ports);
        }

        private void comBoxBaudRate_Click(object sender, EventArgs e)
        {
            comBoxBaudRate.Text = "";
        }
    }
}