C# TCP/IP通讯协议的整理(三)附带——与倍福PLC通讯

首先,需要一个和倍福PLC通讯的dll,一般厂家会提供
C# TCP/IP通讯协议的整理(三)附带——与倍福PLC通讯_第1张图片
添加到引用后,直接创建通讯类

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

namespace CSDN
{
    public class BeckHoffParent
    {
        private TcAdsClient _TcAdsClient;

        /// 
        /// 与PLC进行连接
        /// 
        /// 
        /// 
        /// 
        public bool ConnectSever(string netID, int srvport)
        {
            try
            {
                if (_TcAdsClient == null || !_TcAdsClient.IsConnected)
                {
                    _TcAdsClient = new TcAdsClient();
                    _TcAdsClient.Connect(netID, srvport);
                }
                System.Threading.Thread.Sleep(50);
                return _TcAdsClient.IsConnected;
            }
            catch (Exception err)
            {
                return false;
            }
        }


        #region 读取
        /// 
        /// 读取int16类型的数据
        /// 
        /// 
        /// 
        public int Read_Int16(string address)
        {
            try
            {
                if (_TcAdsClient == null || !_TcAdsClient.IsConnected)
                    return -1;
                int hint1 = _TcAdsClient.CreateVariableHandle(address);
                Int16 iRt = (Int16)_TcAdsClient.ReadAny(hint1, typeof(Int16));
                return iRt;
            }
            catch (Exception err)
            {
                return -1;
            }
        }

        /// 
        /// 读取int类型的数据
        /// 
        /// 
        /// 
        public int Read_Int32(string address)
        {
            try
            {
                if (_TcAdsClient == null || !_TcAdsClient.IsConnected)
                    return -1;
                int hint1 = _TcAdsClient.CreateVariableHandle(address);
                int iRt = (int)_TcAdsClient.ReadAny(hint1, typeof(int));
                return iRt;
            }
            catch (Exception err)
            {
                return -1;
            }
        }

        /// 
        /// 读取float类型的数据
        /// 
        /// 
        /// 
        public float Read_Float(string address)
        {
            try
            {
                if (_TcAdsClient == null || !_TcAdsClient.IsConnected)
                    return -1;
                int hint1 = _TcAdsClient.CreateVariableHandle(address);
                float iRt = (float)_TcAdsClient.ReadAny(hint1, typeof(float));
                return iRt;
            }
            catch (Exception err)
            {
                return -1;
            }
        }

        /// 
        /// 读取double类型的数据
        /// 
        /// 
        /// 
        public double Read_Double(string address)
        {
            try
            {
                if (_TcAdsClient == null || !_TcAdsClient.IsConnected)
                    return -1;
                int hint1 = _TcAdsClient.CreateVariableHandle(address);
                double iRt = (double)_TcAdsClient.ReadAny(hint1, typeof(double));
                return iRt;
            }
            catch (Exception err)
            {
                return -1;
            }
        }

        /// 
        /// 读取bool类型的数据
        /// 
        /// 
        /// 
        public bool Read_Bool(string address,out bool result)
        {
            result = false;
            try
            {                
                if (_TcAdsClient == null || !_TcAdsClient.IsConnected)                  
                    return false;
                int hint1 = _TcAdsClient.CreateVariableHandle(address);
                result = (bool)_TcAdsClient.ReadAny(hint1, typeof(bool));
                return true;
            }
            catch (Exception err)
            {
                return false;
            }
        }
        #endregion

        #region 写入

        /// 
        /// 写入Int16类型的数据
        /// 
        /// 
        /// 
        /// 
        public bool Write_Int16(string address, Int16 value)
        {
            try
            {
                if (_TcAdsClient == null || !_TcAdsClient.IsConnected)
                    return false;
                int hint1 = _TcAdsClient.CreateVariableHandle(address);
                _TcAdsClient.WriteAny(hint1, value);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// 
        /// 写入Int32类型的数据
        /// 
        /// 
        /// 
        /// 
        public bool Write_Int32(string address, int value)
        {
            try
            {
                if (_TcAdsClient == null || !_TcAdsClient.IsConnected)
                    return false;
                int hint1 = _TcAdsClient.CreateVariableHandle(address);
                _TcAdsClient.WriteAny(hint1, value);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// 
        /// 写入float类型的数据
        /// 
        /// 
        /// 
        /// 
        public bool Write_Float(string address, float value)
        {
            try
            {
                if (_TcAdsClient == null || !_TcAdsClient.IsConnected)
                    return false;
                int hint1 = _TcAdsClient.CreateVariableHandle(address);
                _TcAdsClient.WriteAny(hint1, value);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// 
        /// 写入double类型的数据
        /// 
        /// 
        /// 
        /// 
        public bool Write_Double(string address, double value)
        {
            try
            {
                if (_TcAdsClient == null || !_TcAdsClient.IsConnected)
                    return false;
                int hint1 = _TcAdsClient.CreateVariableHandle(address);
                _TcAdsClient.WriteAny(hint1, value);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// 
        /// 写入Bool类型的数据
        /// 
        /// 
        /// 
        /// 
        public bool Write_Bool(string address, bool value)
        {
            try
            {
                if (_TcAdsClient == null || !_TcAdsClient.IsConnected)
                    return false;
                int hint1 = _TcAdsClient.CreateVariableHandle(address);
                _TcAdsClient.WriteAny(hint1, value);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

    }
}

使用方法

  public void Use()
        {
            _BeckHoffParent = new BeckHoffParent();
            if (_BeckHoffParent.ConnectSever("192.168.1.1.1.1",801))//注意的是这里的ip地址的格式
            {
                int result = _BeckHoffParent.Read_Int32(".MW0");//地址和具体读取/写入的数据类型要和PLC那边协商的
                bool write = _BeckHoffParent.Write_Int32(".MW0", result);
            }
        }

你可能感兴趣的:(C#,通讯协议篇,C#,C#)