实例介绍
【实例简介】
电压可自由设定,也可直接设置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 = "";
}
}
}
标签: 数控电源
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论