C# winform使用NI-VISA驱动来控制IT6522数控电源

本文参考NI的《C#与Visual Basic .Net仪器控制指南》

IT6522是ITECH公司的一款数控电源,提供USB,RS232,485,GPIB,以太网等接口,

在之前的文章《winform串口通过SCPI协议与数控电源M8811通信》使用的是串口,

本文介绍如何使用USB来控制数控电源。

USB协议很复杂,厂家也没有提供相应的dll,甚至连驱动都没有,幸好有NI-VISA

首先,http://www.ni.com/download/ni-visa-16.0/6184/en/建议您下载完整版的NI-VISA

安装完成之后,打开NI MAX,插上usb,打开数控电源,然后在面板上设置usb通信


这样复制VISA资源名称,备用。

打开VS2008或者VS2010,(VS2013似乎不行)

添加引用


然后上代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NationalInstruments.VisaNS;

namespace USBPowerTester
{
    class DPit6700 : IDigitalPower
    {
        #region IDigitalPower 成员

        public bool OpenDevice()
        {
            bool flag = true;
            try
            {
                mbSession = (MessageBasedSession)ResourceManager.GetLocalManager().Open(resourceName);
            }
            catch 
            {
                flag = false;
            }
            return flag;
        }

        public void SetVoltage(double voltage)
        {
            string str = "VOLT " + voltage.ToString("0.0000");
            mbSession.Write(str);
        }

        public void SetCurrent(double current)
        {
            string str = "CURR " + current.ToString("0.0000");
            mbSession.Write(str);
        }

        public double GetSetVoltage()
        {
            double vol = 0;
            vol = GetDoubleCmd("VOLT?");
            return vol;
        }

        public double GetSetCurrent()
        {
            double cur = 0;
            cur = GetDoubleCmd("CURR?");
            return cur;
        }

        public double GetOutVoltage()
        {
            double vol = 0;
            vol = GetDoubleCmd("MEAS:VOLT?");
            return vol;
        }

        public double GetOutCurrent()
        {
            double cur = 0;
            cur = GetDoubleCmd("MEAS:CURR?");
            return cur;
        }

        public void SetOutput(bool open)
        {
            if (open)
                mbSession.Write("OUTP 1");
            else
                mbSession.Write("OUTP 0");
        }

        public bool GetOutputStatus()
        {
            bool ret = false;
            string str = mbSession.Query("OUTP?");
            if (str == "1\n")
                ret = true;
            else
                ret = false;
            return ret;
        }

        public void CloseDevice()
        {
            try
            {
                mbSession.Dispose();
            }
            catch { }
        }

        #endregion

        private MessageBasedSession mbSession;
        private const string resourceName = "USB0::0xFFFF::0x6500::60026501068742XXXX::INSTR";

        private double GetDoubleCmd(string cmd)
        {
            double ret = 0;
           
            try
            {
                string str = mbSession.Query(cmd);
                ret = Convert.ToDouble(str);
            }
            catch { }

            return ret;
        }
    }
}
接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace USBPowerTester
{
    interface IDigitalPower
    {
        bool OpenDevice();
        void SetVoltage(double voltage);
        void SetCurrent(double current);
        double GetSetVoltage();
        double GetSetCurrent();
        double GetOutVoltage();
        double GetOutCurrent();
        void SetOutput(bool open);
        bool GetOutputStatus();
        void CloseDevice();
    }
}


这上面的SCPI指令需要参考厂家提供的编程指南(随机光盘中有)


  • 8
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 22
    评论
C# Winform应用程序中使用Entity Framework (EF) 可以通过以下步骤实现: 1. 创建一个C# Winform应用程序项目。 2. 在项目中添加对Entity Framework的引用。可以通过NuGet包管理器安装Entity Framework。 3. 创建一个数据模型类,用于映射数据库表。可以使用EF的Code First或者DB First方法来创建模型类。 4. 配置数据库连接字符串。可以在应用程序的配置文件中添加连接字符串,或者在代码中直接指定连接字符串。 5. 使用EF的DbContext类来操作数据库。可以通过DbContext类的实例来执行查询、插入、更新和删除等操作。 下面是一个简单的示例代码,演示了如何在C# Winform应用程序中使用EF进行数据库操作: ```csharp using System; using System.Linq; using System.Windows.Forms; namespace WinformEFExample { public partial class MainForm : Form { private MyDbContext dbContext; public MainForm() { InitializeComponent(); dbContext = new MyDbContext(); } private void MainForm_Load(object sender, EventArgs e) { // 查询数据 var customers = dbContext.Customers.ToList(); dataGridView1.DataSource = customers; } private void btnAdd_Click(object sender, EventArgs e) { // 添加数据 var customer = new Customer { Name = txtName.Text, Email = txtEmail.Text }; dbContext.Customers.Add(customer); dbContext.SaveChanges(); // 刷新数据 var customers = dbContext.Customers.ToList(); dataGridView1.DataSource = customers; } } } ``` 请注意,上述示例中的`MyDbContext`和`Customer`是根据具体的数据模型和数据库表来定义的,你需要根据自己的实际情况进行修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值