型号:艾德克斯程控电源IT6322B
目录
前言
一、明确需要的基本功能
1.输出电压
2.获取电流
二、具体代码
1.底层通信方法
2.逻辑层
3.表示层
总结
前言
因工作需要用到程控电源IT6322B,所以需要了解其基本使用方法
一、基本功能
1.输出电压
2.获取电流
3.界面
二、具体代码
1.底层通信方法
代码如下(示例):
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;namespace IT6322BDemo
{/// <summary>/// 发送指令/// </summary>class ITMiddlePack{private SerialPort ComDevice = new SerialPort(); //实例化串口/// <summary>/// 打开串口/// </summary>/// <param name="SerialPortName">串口号</param>/// <param name="BaudRate">波特率</param>/// <returns></returns>public bool OpenSerialPort(string SerialPortName, int BaudRate){if (!ComDevice.IsOpen){ComDevice.PortName = SerialPortName; //串口号ComDevice.BaudRate = BaudRate; //波特率ComDevice.Parity = Parity.None;ComDevice.StopBits = StopBits.One;ComDevice.DataBits = 8;ComDevice.ReadTimeout = ComDevice.WriteTimeout = 5000; //超时try{ComDevice.Open();if (write(":SYST:REM\r\n")) //远程控制模式{MessageBox.Show("连接成功");return true;}else{ComDevice.Close();return false;}}catch (Exception ex){MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);return false;}}else{try{if (write(":SYST:LOC\r\n")) //面板控制模式{MessageBox.Show("断开成功");ComDevice.Close();return false;}else{return true;}}catch (Exception ex){MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);return false;}}}/// <summary>/// 发送数据/// </summary>/// <param name="data"></param>/// <returns></returns>public bool write(string sendData){if (ComDevice.IsOpen){try{ComDevice.DiscardInBuffer(); //丢弃来自串行的接收缓冲区数据ComDevice.WriteLine(sendData);Thread.Sleep(100);//sendData = ComDevice.ReadLine(); //发送数据没有返回值return true;}catch (Exception ex){MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);return false;}}else{MessageBox.Show("串口未打开", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);return false;}}/// <summary>/// 接收数据/// </summary>/// <param name="sender"></param>/// <param name="e"></param>public bool read(string sendData, ref string strRead){if (ComDevice.IsOpen) //串口是否打开{ComDevice.DiscardInBuffer(); //丢弃来自串行的缓冲区数据ComDevice.WriteLine(sendData);Thread.Sleep(100);strRead = ComDevice.ReadLine();return true;}else{MessageBox.Show("请先打开串口");return false;}}/// <summary>/// 切换面板控制/// </summary>/// <returns></returns>public bool FormClose(){if (ComDevice.IsOpen){if (write(":SYST:LOC\r\n")) //面板控制模式{ComDevice.Close();return true;}else{return false;}}return true;}}
}
2.逻辑层
代码如下(示例):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace IT6322BDemo
{/// <summary>/// 设置指令/// </summary>public class IT6322B{ITMiddlePack ItBasicLibrary = new ITMiddlePack();/// <summary>/// 打开串口/// </summary>/// <param name="SerialPortName">串口号</param>/// <param name="BaudRate">波特率</param>/// <returns></returns>public bool PWR_OpenDown(string SerialPortName, int BaudRate){return ItBasicLibrary.OpenSerialPort(SerialPortName, BaudRate);}/// <summary>/// 设置电压值 SOUR1:VOLT 10mV 设置电压/// </summary>/// <param name="SetVBool">通道前多选框</param>/// <param name="SetVDouble">设置的电压参数值</param>/// <returns></returns>public bool PWR_SetVoltage(bool[] SetVBool, double[] SetVDouble){try{//return ItBasicLibrary.write(":SOUR1:VOLT " + voltageSet.ToString()+ "mV/n");if (SetVBool.Contains(false)){string VCommand = "";for (int i = 0; i < SetVBool.Count(); i++){if (SetVBool[i]){VCommand += ";";VCommand += string.Format(":INST:NSEL {0}", i + 1);VCommand += ";";VCommand += string.Format(":VOLT {0}V", SetVDouble[i]);}}VCommand = VCommand.Substring(1, VCommand.Length - 1);//删除第一个字符return ItBasicLibrary.write(VCommand + "\r\n");}else{string AllVCommand = string.Format(":APP:VOLT {0},{1},{2}", SetVDouble[0], SetVDouble[1], SetVDouble[2]);return ItBasicLibrary.write(AllVCommand + "\r\n");}}catch{return false;}}/// <summary>/// 设置电流值 SOUR1:CURR 30mA 设置电流/// </summary>/// <param name="SetIBool">通道前多选框</param>/// <param name="SetIDouble">设置的电流参数值</param>/// <returns></returns>public bool PWR_SetCurrent(bool[] SetIBool, double[] SetIDouble){try{//return ItBasicLibrary.write(":SOUR1:VOLT " + voltageSet.ToString()+ "mV/n");if (SetIBool.Contains(false)){string IoCommand = "";for (int i = 0; i < SetIBool.Count(); i++){if (SetIBool[i]){IoCommand += ";";IoCommand += string.Format(":INST:NSEL {0}", i + 1); //切换通道IoCommand += ";";IoCommand += string.Format(":CURR {0}A", SetIDouble[i]); //设置电流 A}}IoCommand = IoCommand.Substring(1, IoCommand.Length - 1);//删除第一个字符return ItBasicLibrary.write(IoCommand + "\r\n");//发送命令}else{//设置三个端口电流string AllIoCommand = string.Format(":APP:CURR {0},{1},{2}", SetIDouble[0], SetIDouble[1], SetIDouble[2]);//发送命令return ItBasicLibrary.write(AllIoCommand + "\r\n");}}catch{return false;}}/// <summary>/// 启动输出/// </summary>/// <param name="SetVBool">通道前多选框</param>/// <param name="output">开关0/1 NO/OFF</param>/// <returns></returns>public bool PWR_OutPut(bool[] SetVBool, int output){try{if (SetVBool.Contains(false)){string OutCommand = "";for (int i = 0; i < SetVBool.Count(); i++){if (SetVBool[i]){OutCommand += ";";OutCommand += string.Format(":INST:NSEL {0}", i + 1); //切换端口OutCommand += ";";//控制单个端口输出OutCommand += string.Format(":CHAN:OUTP {0}", output);}}OutCommand = OutCommand.Substring(1, OutCommand.Length - 1);//删除第一个字符return ItBasicLibrary.write(OutCommand + "\r\n");}else{//启动或关闭所有端口string AllOutCommand = string.Format(":OUTP:STAT {0}", output);return ItBasicLibrary.write(AllOutCommand + "\r\n");}}catch{return false;}}/// <summary>/// 获取端口电压/// </summary>/// <param name="value"></param>/// <returns></returns>public bool PWR_GetVoltage(double value){string strRead = "";try{//if (ItBasicLibrary.read(":SOUR1:MEAS:VOLT?/n", ref strRead) == false)if (ItBasicLibrary.read(":MEAS:VOLT:ALL?\r\n", ref strRead) == false){return false;}value = Convert.ToDouble(strRead);return true;}catch{return false;}}/// <summary>/// 获取所有通道电流值/// </summary>/// <param name="value">返回三通道数据</param>/// <returns></returns>public bool PWR_GetCurrent(ref double[] value){try{string strRead = "";if (ItBasicLibrary.read(":MEAS:CURR:ALL?\r\n", ref strRead)) //获取所有端口电流{string[] strVal = null;//字符串转数组strVal = strRead.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);//字符串数组转doubel数组value = Array.ConvertAll<string, double>(strVal, delegate (string s) { return double.Parse(s); });return true;}return false;//}}catch{return false;}}/// <summary>/// 面板控制/// </summary>/// <returns></returns>public bool FormClose(){return ItBasicLibrary.FormClose();}/// <summary>/// 基本命令/// </summary>/// <param name="str"></param>/// <param name="strRead"></param>/// <returns></returns>public bool IDN(string str,ref string strRead){return ItBasicLibrary.read(str+"\r\n", ref strRead);}}
}
3.表示层
代码如下(示例):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;namespace IT6322BDemo
{public partial class ITDemoForm : Form{ITMiddlePack ItBasicLibrary = new IT6322BDemo.ITMiddlePack();IT6322B It6322b = new IT6322B();public ITDemoForm(){InitializeComponent();//设置初始化参数Initialization();}/// <summary>/// 初始化参数/// </summary>void Initialization(){//串口号赋值SearchAnAddSerialToComboBox(serialPort_IT6322B, comboBox_SerialPort);//默认RS232comboBox_OSADev1ConnectType.SelectedIndex = 0;//数据位comboBox_DataBits.Items.Add("5");comboBox_DataBits.Items.Add("6");comboBox_DataBits.Items.Add("7");comboBox_DataBits.Items.Add("8");comboBox_DataBits.SelectedIndex = 3;//停止位comboBox_StopBit.Items.Add("1");comboBox_StopBit.SelectedIndex = 0;//校验位comboBox_Parity.Items.Add("none"); //8个数据位都无校验comboBox_Parity.Items.Add("odd"); //8个数据位都有奇校验comboBox_Parity.Items.Add("even"); //8个数据位都有偶校验comboBox_Parity.SelectedIndex = 0;}/// <summary>/// 通信类型选择,LAN或GPIB 文本与按钮控制/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void comboBox_ComType_SelectedIndexChanged(object sender, EventArgs e){if (comboBox_OSADev1ConnectType.SelectedIndex == 0) //Rs232{comboBox_SerialPort.Enabled = true; //串口号textBox_OSADev1GPIB.Enabled = false;textBox_BaudRate.Enabled = true;comboBox_DataBits.Enabled = true; //数据位comboBox_StopBit.Enabled = true; //停止位comboBox_Parity.Enabled = true; //校验位}else if (comboBox_OSADev1ConnectType.SelectedIndex == 1) //GPIB{textBox_OSADev1GPIB.Enabled = true;comboBox_SerialPort.Enabled = false;textBox_BaudRate.Enabled = false;comboBox_DataBits.Enabled = false; //数据位comboBox_StopBit.Enabled = false; //停止位comboBox_Parity.Enabled = false; //校验位}else //USB{textBox_OSADev1GPIB.Enabled = false;comboBox_SerialPort.Enabled = false;textBox_BaudRate.Enabled = false;comboBox_DataBits.Enabled = false; //数据位comboBox_StopBit.Enabled = false; //停止位comboBox_Parity.Enabled = false; //校验位}}/// <summary>/// 检索串口号/// </summary>/// <param name="MyPort"></param>/// <param name="MyBox"></param>private void SearchAnAddSerialToComboBox(SerialPort MyPort, ComboBox MyBox)//检索串口函数{//将可用的串口号添加到ComboBox.string[] NmberOfport = SerialPort.GetPortNames();//遍历所有for (int i = 0; i < NmberOfport.Length; i++){try{MyPort.PortName = NmberOfport[i];MyBox.Items.Add(NmberOfport[i]);MyPort.Open(); //如果失败,后面代码不执行MyPort.Close(); //关闭MyBox.Text = NmberOfport[i]; //显示最后扫描成功的串口}catch { };}}/// <summary>/// 打开串口/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button_ITLink_Click(object sender, EventArgs e){//int ConnectType = comboBox_OSADev1ConnectType.SelectedIndex; //连接类型//string Dev1GPIB = textBox_OSADev1GPIB.Text; //Gpib地址int SerialPortCount = comboBox_SerialPort.Items.Count; //串口行数string SerialPortName = comboBox_SerialPort.SelectedItem.ToString(); //串口号int BaudRate = Convert.ToInt32(textBox_BaudRate.Text); //波特率if (SerialPortCount <= 0) //串口长度为0{MessageBox.Show("没有发现串口,请检查线路");return;}bool flag = It6322b.PWR_OpenDown(SerialPortName, BaudRate);//连接串口if (flag){button_ITLink.Text = "关闭串口";}else{button_ITLink.Text = "打开串口";}ControlSet(flag);}/// <summary>/// 控件启用/// </summary>void ControlSet(bool flag){button_SendData.Enabled = flag; //控制发送按钮comboBox_SerialPort.Enabled = !flag; //串口号textBox_BaudRate.Enabled = !flag; //波特率comboBox_OSADev1ConnectType.Enabled = !flag; //通信方式comboBox_DataBits.Enabled = !flag; //数据位comboBox_StopBit.Enabled = !flag; //停止位comboBox_Parity.Enabled = !flag; //校验位}/// <summary>/// 启动端口输出/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button_Output_Click(object sender, EventArgs e){//获取选中状态bool[] SetVBool = new bool[]{ checkBox_SGCH1.Checked, checkBox_SGCH2.Checked, checkBox_SGCH3.Checked };if (!SetVBool.Contains(true)){MessageBox.Show("请勾选需要输出的通道");return;}//传bool[]选择端口//bool[0]=T 设置端口1//bool[1]=T 设置端口2//bool[2]=T 设置端口3if (It6322b.PWR_OutPut(SetVBool, 1)){MessageBox.Show("开始输出");timer_ITCurr.Start();}//实时}/// <summary>/// 设置电压/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button_SetV_Click(object sender, EventArgs e){//获取选中状态bool[] SetVBool = new bool[]{ checkBox_SGCH1.Checked, checkBox_SGCH2.Checked, checkBox_SGCH3.Checked };try{//获取设置的电压值double SV1 = Convert.ToDouble(textBox_SV1.Text == "" ? "0" : textBox_SV1.Text);double SV2 = Convert.ToDouble(textBox_SV2.Text == "" ? "0" : textBox_SV2.Text);double SV3 = Convert.ToDouble(textBox_SV3.Text == "" ? "0" : textBox_SV3.Text);double[] SetVDouble = new double[] { SV1, SV2, SV3 };if (SetVDouble.Max() > 3){MessageBox.Show("电压大于3V");return;}if (!SetVBool.Contains(true)){MessageBox.Show("若要设置请勾选通道前多选框");return;}//传bool[]选择端口//bool[0]=T 设置端口1//bool[1]=T 设置端口2//bool[2]=T 设置端口3//传double[]设置电压值//double[0] 设置 端口1的值//double[1] 设置 端口2的值//double[2] 设置 端口3的值if (It6322b.PWR_SetVoltage(SetVBool, SetVDouble)) {MessageBox.Show("设置成功");}}catch (Exception ex){MessageBox.Show(ex.Message);return;}}/// <summary>/// 读取电流 【同时读取三个端口的值】/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button_GetIV_Click(object sender, EventArgs e){//获取选中状态bool[] GetIBool = new bool[]{ checkBox_SGCH1.Checked, checkBox_SGCH2.Checked, checkBox_SGCH3.Checked };//获取设置的电流值double[] SetIDouble = null;if (!GetIBool.Contains(true)){MessageBox.Show("请勾选需读取的通道");return;}//获取文本框TextBox[] textBox = new TextBox[]{textBox_GI1,textBox_GI2,textBox_GI3};//给文本框赋值if (It6322b.PWR_GetCurrent(ref SetIDouble)){for (int i = 0; i < GetIBool.Count(); i++){根据勾选状态来给对应的文本框赋值if (GetIBool[i]){textBox[i].Text = SetIDouble[i].ToString();}}}}/// <summary>/// 关闭端口输出/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button_Close_Click(object sender, EventArgs e){bool[] SetVBool = new bool[]{ checkBox_SGCH1.Checked, checkBox_SGCH2.Checked, checkBox_SGCH3.Checked };if (It6322b.PWR_OutPut(SetVBool, 0)){MessageBox.Show("关闭输出");timer_ITCurr.Stop();}}/// <summary>/// 设置电流/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button_SetI_Click(object sender, EventArgs e){//获取选中状态bool[] SetIBool = new bool[]{ checkBox_SGCH1.Checked, checkBox_SGCH2.Checked, checkBox_SGCH3.Checked };try{//获取设置的电流值double SI1 = Convert.ToDouble(textBox_SI1.Text == "" ? "0" : textBox_SI1.Text);double SI2 = Convert.ToDouble(textBox_SI2.Text == "" ? "0" : textBox_SI2.Text);double SI3 = Convert.ToDouble(textBox_SI3.Text == "" ? "0" : textBox_SI3.Text);double[] SetIDouble = new double[] { SI1, SI2, SI3 };if (SetIDouble.Max() > 0.5){MessageBox.Show("电流大于0.5A");return;}if (!SetIBool.Contains(true)){MessageBox.Show("若要设置请勾选通道前多选框");return;}//传bool[]选择端口//bool[0]=T 设置端口1//bool[1]=T 设置端口2//bool[2]=T 设置端口3//传double[]设置电流值//double[0] 设置 端口1的值//double[1] 设置 端口2的值//double[2] 设置 端口3的值if (It6322b.PWR_SetCurrent(SetIBool, SetIDouble)) //根据多选框设置电流{MessageBox.Show("设置成功");}}catch (Exception ex){MessageBox.Show(ex.Message);return;}}/// <summary>/// 界面关闭前设置为面板控制/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void ITDemoForm_FormClosing(object sender, FormClosingEventArgs e){if (!It6322b.FormClose()){MessageBox.Show("串口关闭失败");return;}}/// <summary>/// 定时器自动读取电流/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void timer_ITCurr_Tick(object sender, EventArgs e){button_GetIV_Click(null, null);}/// <summary>/// *idn?/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button_SendData_Click(object sender, EventArgs e){string str = "";It6322b.IDN(textBox_SendData.Text,ref str);if (textBox_ReceiveData.Text.Length > 0){textBox_ReceiveData.AppendText("\r\n");}textBox_ReceiveData.AppendText(str);}}
}
总结
以上就是今天记录的内容,本文仅仅简单介绍了程控电源的使用,具体通信方法可在艾斯官网查阅Itchate官网