C#和倍福PLC之间的通讯控制

1.新建项目并引用TwinCAT动态链接库

 

2.引用类,并编写倍福控制的相关对象

  1 using System;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using System.Linq;
  5 using System.Text;
  6 using TwinCAT.Ads;
  7 
  8 
  9 namespace Project_V1._001_Data
 10 {
 11     public class Beckhoff_PLC
 12     {
 13        
 14 
 15         #region 基本对象定义
 16         /// <summary>
 17         /// 创建Ads对象
 18         /// </summary>
 19         public static TwinCAT.Ads.TcAdsClient _adsClient = new TcAdsClient();
 20 
 21         /// <summary>
 22         /// 定义连接PLC的AMSNetID
 23         /// </summary>
 24         public static string string_AmsNetID = "192.168.0.122.1.1";
 25      
 26 
 27         /// <summary>
 28         /// 定义连接PLC的端口号
 29         /// TwinCAT2,端口号801;TwinCAT3,端口号为851
 30         /// </summary>
 31         //public static int iPort = 801; 
 32         public static int iPort = 851;
 33 
 34 
 35         /// <summary>
 36         /// PLC连接成功的标志
 37         /// </summary>
 38         public static bool bConnectToBeckhoffOK = false;
 39 
 40 
 41         public static ArrayList notificationHandles = new ArrayList();
 42         #endregion
 43 
 44         #region 函数定义
 45         /// <summary>
 46         /// 连接到PLC
 47         /// </summary>
 48         public static void ConnectPLC()
 49         {
 50             try
 51             {
 52                 //_adsClient.Connect(string_AmsNetID, iPort);
 53                 _adsClient.Connect(iPort);
 54                 bConnectToBeckhoffOK = true;
 55                
 56             }
 57             catch
 58             {
 59                 bConnectToBeckhoffOK = false;
 60             }
 61         }
 62 
 63         /// <summary>
 64         /// 读取double类型的变量,非实时刷新
 65         /// </summary>
 66         /// <param name="PlcVariableName"></param>
 67         /// <returns></returns>
 68         public static double ReadDouble(string PlcVariableName)
 69         {
 70             double dResult = 0;
 71             try
 72             {
 73                 int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
 74                 dResult = (double)(_adsClient.ReadAny(iHandleTemp, typeof(double)));
 75                 _adsClient.DeleteVariableHandle(iHandleTemp);
 76             }
 77             catch { return 0; }
 78             return dResult;
 79         }
 80 
 81         /// <summary>
 82         /// 根据PLC变量名,写PLC变量
 83         /// </summary>
 84         /// <param name="PlcVariableName"></param>
 85         /// <param name="dResult"></param>
 86         public static void WriteVariable(string PlcVariableName, double dResult)
 87         {
 88             try
 89             {
 90                 int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
 91                 _adsClient.WriteAny(iHandleTemp, dResult);
 92                 _adsClient.DeleteVariableHandle(iHandleTemp);
 93             }
 94             catch { }
 95         }
 96 
 97         /// <summary>
 98         /// 根据PLC变量名,写PLC变量
 99         /// </summary>
100         /// <param name="PlcVariableName"></param>
101         /// <param name="dResult"></param>
102         public static void WriteVariable(string PlcVariableName, bool bResult)
103         {
104             try
105             {
106                 int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
107                 _adsClient.WriteAny(iHandleTemp, bResult);
108                 _adsClient.DeleteVariableHandle(iHandleTemp);
109             }
110             catch { }
111         }
112 
113 
114         #endregion
115 
116     }
117 }

 

 3.主程序中直接调用倍福控制类

 

 

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

namespace Project_V1._001_Data{    public class Beckhoff_PLC    {       
        #region 基本对象定义        /// <summary>        /// 创建Ads对象        /// </summary>        public static TwinCAT.Ads.TcAdsClient _adsClient = new TcAdsClient();
        /// <summary>        /// 定义连接PLC的AMSNetID        /// </summary>        public static string string_AmsNetID = "192.168.0.122.1.1";     
        /// <summary>        /// 定义连接PLC的端口号        /// TwinCAT2,端口号801;TwinCAT3,端口号为851        /// </summary>        //public static int iPort = 801;         public static int iPort = 851;

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

        public static ArrayList notificationHandles = new ArrayList();        #endregion
        #region 函数定义        /// <summary>        /// 连接到PLC        /// </summary>        public static void ConnectPLC()        {            try            {                //_adsClient.Connect(string_AmsNetID, iPort);                _adsClient.Connect(iPort);                bConnectToBeckhoffOK = true;                           }            catch            {                bConnectToBeckhoffOK = false;            }        }
        /// <summary>        /// 读取double类型的变量,非实时刷新        /// </summary>        /// <param name="PlcVariableName"></param>        /// <returns></returns>        public static double ReadDouble(string PlcVariableName)        {            double dResult = 0;            try            {                int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);                dResult = (double)(_adsClient.ReadAny(iHandleTemp, typeof(double)));                _adsClient.DeleteVariableHandle(iHandleTemp);            }            catch { return 0; }            return dResult;        }
        /// <summary>        /// 根据PLC变量名,写PLC变量        /// </summary>        /// <param name="PlcVariableName"></param>        /// <param name="dResult"></param>        public static 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 static void WriteVariable(string PlcVariableName, bool bResult)        {            try            {                int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);                _adsClient.WriteAny(iHandleTemp, bResult);                _adsClient.DeleteVariableHandle(iHandleTemp);            }            catch { }        }

        #endregion
    }}

posted @ 2019-07-10 10:53  帝雪寒天  阅读(7045)  评论(1编辑  收藏  举报