首页 > 编程语言 >C#与倍福PLC实现ADS通讯

C#与倍福PLC实现ADS通讯

时间:2023-08-21 14:11:39浏览次数:41  
标签:dresult ADS C# iHandleTemp public adsClient PLC PlcVariableName string

1.引入倍福的TWINCAT的文件

引用--管理NUGET程序包--搜索Twincat--选择这个包安装

 2.安装完成后,写一个倍福ADS通讯操作类

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using TwinCAT.Ads;



namespace SIC上位.BeckoffHelp
{

    public class BHelp
    {

        /// <summary>
        /// 创建Ads对象
        /// </summary>
        public TwinCAT.Ads.TcAdsClient _adsClient = new TcAdsClient();

        ///// <summary>
        ///// 定义连接PLC的AMSNetID
        ///// </summary>
        public string string_AmsNetID = "";

        ///// <summary>
        ///// 定义连接PLC的端口号
        ///// TwinCAT2,端口号801;TwinCAT3,端口号为851
        ///// </summary>
        public int iPort = 851;

        ////public  int iPort = 851;

        /// <summary>
        /// PLC连接成功的标志
        /// </summary>
        public bool bConnectToBeckhoffOK = false;

        public ArrayList notificationHandles = new ArrayList();





        public void ADS()
        {
            _adsClient = new TcAdsClient();
        }
        /// <summary>
        /// 连接到PLC
        /// </summary>
        public void ConnectPLC()
        {
            try
            {
                _adsClient.Connect(string_AmsNetID, iPort);
                //_adsClient.Connect(iPort);
                bConnectToBeckhoffOK = true;
            }
            catch
            {
                bConnectToBeckhoffOK = false;
            }
        }


        /// <summary>
        /// 断开PLC
        /// </summary>
        public void DisConnectPLC()
        {
            
                _adsClient.Dispose();
           
        }




        /// <summary>
        /// 读取int类型的变量,非实时刷新
        /// </summary>
        /// <param name="PlcVariableName"></param>
        /// <returns></returns>
        public int ReadInt(string PlcVariableName)
        {
            int dresult = 0;
            try
            {
                int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
                dresult = (int)(_adsClient.ReadAny(iHandleTemp, typeof(int)));
                _adsClient.DeleteVariableHandle(iHandleTemp);
            }
            catch { return 0; }
            return dresult;
        }

        /// <summary>
        /// 读取string类型的变量,非实时刷新
        /// </summary>
        /// <param name="PlcVariableName"></param>
        /// <returns></returns>
        public char[] Readchar(string PlcVariableName)
        {
            char[] dresult = { };
            try
            {
                int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
                dresult = (char[])(_adsClient.ReadAny(iHandleTemp, typeof(char[])));
                _adsClient.DeleteVariableHandle(iHandleTemp);
            }
            catch { }
            return dresult;
        }

        /// <summary>
        /// 读取string类型的变量,非实时刷新
        /// </summary>
        /// <param name="PlcVariableName"></param>
        /// <returns></returns>
        public string Readstring(string PlcVariableName)
        {
            string dresult = "";
            try
            {
                int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
                dresult = _adsClient.ReadAny(iHandleTemp, typeof(string), new int[] { 80 }).ToString();
                _adsClient.DeleteVariableHandle(iHandleTemp);
            }
            catch { }
            return dresult;
        }

        /// <summary>
        /// 读取float类型的变量,非实时刷新
        /// </summary>
        /// <param name="PlcVariableName"></param>
        /// <returns></returns>
        public float Readfloat(string PlcVariableName)
        {
            float dresult;
            try
            {
                int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
                dresult = (float)(_adsClient.ReadAny(iHandleTemp, typeof(float)));
                _adsClient.DeleteVariableHandle(iHandleTemp);
            }
            catch { return 0; }
            return dresult;
        }

        /// <summary>
        /// 读取short类型的变量,非实时刷新
        /// </summary>
        /// <param name="PlcVariableName"></param>
        /// <returns></returns>
        public short Readshort(string PlcVariableName)
        {
            short dresult;
            try
            {
                int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
                dresult = (short)(_adsClient.ReadAny(iHandleTemp, typeof(short)));
                _adsClient.DeleteVariableHandle(iHandleTemp);
            }
            catch { return 0; }
            return dresult;
        }

        /// <summary>
        /// 读取bool类型的变量,非实时刷新
        /// </summary>
        /// <param name="PlcVariableName"></param>
        /// <returns></returns>
        public bool Readbool(string PlcVariableName)
        {
            bool dresult;
            try
            {
                int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
                dresult = (bool)(_adsClient.ReadAny(iHandleTemp, typeof(bool)));
                _adsClient.DeleteVariableHandle(iHandleTemp);
            }
            catch { return false; }
            return dresult;
        }

        /// <summary>
        /// 根据PLC变量名,写PLC变量
        /// </summary>
        /// <param name="PlcVariableName"></param>
        /// <param name="dresult"></param>
        public void WriteVariable(string PlcVariableName, Double dresult)
        {
            try
            {
                int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
                _adsClient.WriteAny(iHandleTemp, dresult);
                _adsClient.DeleteVariableHandle(iHandleTemp);
            }
            catch { }
        }

        /// <summary>
        /// 根据PLC变量名,写PLC变量
        /// </summary>
        /// <param name="PlcVariableName"></param>
        /// <param name="dresult"></param>
        public void WriteVariable(string PlcVariableName, short dresult)
        {
            try
            {
                int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
                _adsClient.WriteAny(iHandleTemp, dresult);
                _adsClient.DeleteVariableHandle(iHandleTemp);
            }
            catch { }
        }

        /// <summary>
        /// 根据PLC变量名,写PLC变量
        /// </summary>
        /// <param name="PlcVariableName"></param>
        /// <param name="dresult"></param>
        public void WriteVariable(string PlcVariableName, bool dresult)
        {
            try
            {
                int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
                _adsClient.WriteAny(iHandleTemp, dresult);
                _adsClient.DeleteVariableHandle(iHandleTemp);
            }
            catch { }
        }

        /// <summary>
        /// 根据PLC变量名,写PLC变量
        /// </summary>
        /// <param name="PlcVariableName"></param>
        /// <param name="dresult"></param>
        public void WriteVariable(string PlcVariableName, float dresult)
        {
            try
            {
                int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
                _adsClient.WriteAny(iHandleTemp, dresult);
                _adsClient.DeleteVariableHandle(iHandleTemp);
            }
            catch { }
        }

        /// <summary>
        /// 根据PLC变量名,写PLC变量
        /// </summary>
        /// <param name="PlcVariableName"></param>
        /// <param name="dresult"></param>
        public void WriteVariable(string PlcVariableName, int dresult)
        {
            try
            {
                int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
                _adsClient.WriteAny(iHandleTemp, dresult);
                _adsClient.DeleteVariableHandle(iHandleTemp);
            }
            catch { }
        }

        /// <summary>
        /// 根据PLC变量名,写PLC变量
        /// </summary>
        /// <param name="PlcVariableName"></param>
        /// <param name="dresult"></param>
        public void WriteVariable(string PlcVariableName, byte dresult)
        {
            try
            {
                int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
                _adsClient.WriteAny(iHandleTemp, dresult);
                _adsClient.DeleteVariableHandle(iHandleTemp);
            }
            catch { }
        }

        /// <summary>
        /// 根据PLC变量名,写PLC变量
        /// </summary>
        /// <param name="PlcVariableName"></param>
        /// <param name="dresult"></param>
        public void WriteVariable(string PlcVariableName, string dresult)
        {
            int stringlen = dresult.Length;
            AdsStream datastream = new AdsStream(stringlen + 1);
            BinaryWriter binwrite = new BinaryWriter(datastream, System.Text.Encoding.ASCII);
            datastream.Position = 0;
            try
            {
                binwrite.Write(dresult.ToCharArray());
                binwrite.Write('\0');
                int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
                _adsClient.Write(iHandleTemp, datastream);
                _adsClient.DeleteVariableHandle(iHandleTemp);
            }
            catch (Exception err)
            {
            }
        }


    }
}







3.操作类写完后可以在主程序中引用,按钮中写如下代码,点击主界面即可连接,实现通讯。

 private void uiSymbolButtonConnectSign_Click(object sender, EventArgs e)
        {
            bHelp.string_AmsNetID = "5.126.62.10.1.1";
            bHelp.iPort = 851;
            bHelp.ConnectPLC();//开始连接PLC

            uiSymbolLabel1.Text = fLogin.UserName;

            timer2.Enabled = true;





        }

 4.因为倍福无心跳通讯信号,需在PLC中写一个心跳变量来实现通讯是否接通的显示。

标签:dresult,ADS,C#,iHandleTemp,public,adsClient,PLC,PlcVariableName,string
From: https://www.cnblogs.com/lsh-love/p/17645846.html

相关文章

  • Linux/centos上如何配置管理samba服务器?
    (Linux/centos上如何配置管理samba服务器?)1samba服务相关知识samba服务器可使用户在异构网络操作系统之间进行文件系统共享;samba服务器提供了在Windows环境下共享Linux中用户目录的一个工具;在linux中安装samba后,只需在windows中做一些登陆即可访问。1.1SMB协议Windows系......
  • Linux/centos上如何配置管理samba服务器?
    (Linux/centos上如何配置管理samba服务器?)1samba服务相关知识samba服务器可使用户在异构网络操作系统之间进行文件系统共享;samba服务器提供了在Windows环境下共享Linux中用户目录的一个工具;在linux中安装samba后,只需在windows中做一些登陆即可访问。1.1SMB协议Windows系......
  • javascript学习笔记day4
    今天重点学习了数组,老实说学过了c#和python的数组,但是今天重新接触js的数字还是有很多要重新学习的,下面是今天的笔记查询条件五个以上时,switch的效果比iflese高两倍以上.letarr=[]声明数组letarr=newArray(1,2,3,4)声明数组修改数组letarr=['a','b','c']for(letinde......
  • 海天tech注塑机数据采集测试软件
    本测试软件支持采集海天/力劲系列注塑机,弘讯TECH1、弘讯AK628、弘讯TECH2、弘讯AK668等型号控制器。采集数据类型示例如下:设备状态、报警信息、开模总数输出压力、速度、背压射出、模座、托模位置温度一~九段开模、关模、高压、低压射退时间、射退位置再循环计时、模具冷......
  • 技术组件-nginx-docker安装
    安装搜索版本dockersearchnginx下载镜像dockerpullnginx:1.22创建目录mkdir-p/data/docker/data/nginx/confmkdir-p/data/docker/data/nginx/logmkdir-p/data/docker/data/nginx/htmlmkdir-p/data/docker/data/nginx/cer创建临时nginxdockerru......
  • PPPoE vs DHCP:哪种方式最适合您网络接入
     在构建网络连接时,选择适合的接入方式是至关重要的。本文将深入比较PPPoE和DHCP两种常见的接入方式,帮助您了解它们的优劣势和适用场景,为您提供实际操作价值的解决方案。让我们一起来探讨,为您的网络选择最佳的接入方式。 在开始比较之前,让我们先了解一下PPPoE和DHCP的基本概念......
  • C++文档入口汇总
    一、C++参考手册(中文)cppreferencehttps://zh.cppreference.com/二、菜鸟教程(中文)https://www.runoob.com/cplusplus/cpp-tutorial.html三、C++的常用库及其文档:标准模板库(STL)提供了许多数据结构和算法,如向量、链表、队列、堆栈、映射和排序算法等。其中文文档链接:https://......
  • ETL之apache hop数据增量同步功能
    ETL增量数据抽取CDC概念:ChangeDataCapture,变化的数据捕获,也称:【增量数据抽取】(名词解释)CDC是一种实现数据的增量抽取解决方案,是实现【ETL整体解决方案】中的一项子方案/子问题。(对CDC的定位)如何捕获变化的数据是增量抽取的关键,对捕获方法一般有2点要求:准确性:能够将业务系......
  • lumerical使用 MATLAB
    使用MATLAB创建矢量图https://optics.ansys.com/hc/en-us/articles/360034404034  使用MATLAB在现场图像上叠加矢量图https://optics.ansys.com/hc/en-us/sections/1500000909761-Examples   https://optics.ansys.com/hc/en-us/articles/360034923933......
  • 6. 权责发生制 Accrual Accounting Basis
    Profitability盈利能力盈利Profit=收入Revenue-开支Expense1.什么是权责发生制在会计学中,收入并不等于收款,开支并不等于付款。将收入和支出记录在它们发生的周期内,而不一定是收款和付款的周期内,这就是权责发生制。权责发生就是收入或支出的确认。2.权责发生制的三......