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
    }}函數

相關文章
相關標籤/搜索