C#通过ADS通讯实现与倍福PLC通讯

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中写一个心跳变量来实现通讯是否接通的显示。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值