基本信息
源码名称:基于S7.net类库与西门子PLC通讯
源码大小:2.91M
文件格式:.rar
开发语言:C#
更新时间:2025-02-27
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 1 元 
   源码介绍
【实例简介
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using S7.Net;
using S7.Net.Types;
using xktComm;
using xktComm.Common;


namespace SiemensByS7.NETComm.DLL
{
   public class PlcComm
    {
        //创建PLC对象
        private Plc MyPlc=null;

        #region 打开、关闭PLC连接
        /// <summary>
        /// 打开PLC通讯
        /// </summary>
        /// <param name="cpuType">CPU型号</param>
        /// <param name="plcIP">PLCIP地址</param>
        /// <param name="rack">机架号</param>
        /// <param name="slot">CPU槽位号</param>
        /// <exception cref="Exception"></exception>
        public void OpenPlc(CpuType cpuType, string plcIP, short rack, short slot )
        {
           
            try
            {
                MyPlc = new Plc( cpuType,plcIP,rack,slot);
                MyPlc.Open();
            }
            catch (Exception ex)
            {
                throw new Exception("PLC连接失败!原因" ex.Message);
            }
        }

        //关闭PLC通讯
        public void ClosePlc() 
        { 
            MyPlc.Close();  
        }
        #endregion

        #region 写入PLC数据
        /// <summary>
        /// 按照【地址】写入数据(位、字节、字、双字)
        /// </summary>
        /// <param name="varAddress">地址</param>
        /// <param name="varValue">值</param>
        public void WriteDataToPLC(string varAddress, object varValue)
        {
            lock (this)//锁机制
                try
                {
                   MyPlc.Write(varAddress, varValue);
                }
                catch (Exception ex)
                {

                    throw new Exception("写入数据错误,原因:"   ex.Message);
                }

        }

        // 按照【地址】写入Float类型数据
        public void WriteFolatDataToPLC(string varAddress, object varValue)
        {
            lock (this)
                try
                {
                    float value = (float)Convert.ToDouble(varValue);//(float)隐式转换
                   MyPlc.Write(varAddress, value);
                }
                catch (Exception ex)
                {

                    throw new Exception("写入数据错误,原因:"   ex.Message);
                }
        }
        #endregion

        #region 读取PLC数据
        /// <summary>
        /// 按照【地址】读取一个浮点数据
        /// </summary>
        /// <param name="varAddress"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public string ReadFloatDataFromPLC(int db, int varAddress, int count)
        {
            //从PLC中指定地址中读取数据,返回字节数组
            byte[] data =MyPlc.ReadBytes(S7.Net.DataType.DataBlock, db, varAddress, count);
            //把字节数组转换成浮点数,再转换成字符串,最后返回
            //return FloatLib.GetFloatFromByteArray(data, 0).ToString();
            return S7.Net.Types.Real.FromByteArray(data).ToString();
        }

        //读取一个Word数据
        public string ReadWordDataFromPLC(int db, int varAddress, int count)
        {
            byte[] data = MyPlc.ReadBytes(S7.Net.DataType.DataBlock, db, varAddress, count);
            ushort value = Word.FromByteArray(data);
            return value.ToString();
        }
        //读取一个Byte数据
        public string ReadByteDataFromPLC(int db, int varAddress, int count)
        {
            byte[] data = MyPlc.ReadBytes(S7.Net.DataType.DataBlock, db, varAddress, count);
            byte value = data[0];
            return value.ToString();
        }
        //读取一个Bool量
        public string ReadBoolDataFromPLC(string varAddress)
        {
            bool value = Convert.ToBoolean(MyPlc.Read(varAddress));
            return value.ToString();
        }
    }
    #endregion
}