S7net【C#】

C#项目,跟西门子PLC通讯

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using S7.Net;

namespace MeProgram
{
    public class S7NetPlc
    {
        Plc plc;
        /// 
        /// 初始化S7NetPLC
        /// 
        /// 
        /// 
        public S7NetPlc(string IP, string CPUType ="S71200")
        {
            CreatePlc(IP, CPUType);
        }

        /// 
        /// 实例化PLC  IP和CPU型号  CPU型号默认S71200
        /// 
        /// 
        /// 
        void CreatePlc(string ip, string cpu)
        {
            CpuType cputype = new CpuType();
            if (Enum.TryParse(cpu.ToUpper(), out cputype))
            {
                plc = new Plc(cputype, ip, 0, 1);
            }
            else
            {
                Console.WriteLine("CPU型号错误");
            }
        }
        /// 
        /// 连接PLC 再次调用可重新连接
        /// 
        public async void Connet()
        {
            try
            {
                await plc.OpenAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        /// 
        /// PLC连接状态
        /// 
        /// 
        public bool ConnetStatus()
        {
            return plc.IsConnected;
        }

        #region 读取PLC数据
        /// 
        /// 读取PLC 输入点 i
        /// 
        /// 开始的地址
        /// 读取字节数量
        /// 
        public BitArray Get_I(int 地址I点, int 数量)
        {
            BitArray bitArray = new BitArray(数量);
            try
            {
                if (plc.IsConnected)
                {
                    bitArray = (BitArray)plc.Read(DataType.Input, 0, 地址I点, VarType.Bit, 数量);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return bitArray;
        }
        /// 
        /// 读取PLC 输出点Q
        /// 
        /// 开始的地址
        /// 读取字节数量
        /// 
        public BitArray Get_Q(int 地址Q点, int 数量)
        {
            BitArray bitArray = new BitArray(数量);
            try
            {
                if (plc.IsConnected)
                {
                    bitArray = (BitArray)plc.Read(DataType.Output, 0, 地址Q点, VarType.Bit, 数量);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return bitArray;
        }
        /// 
        /// 读取PLC默认寄存器地址
        /// 
        /// 
        /// 
        /// 
        public BitArray Get_M(int 地址M点, int 数量)
        {
            BitArray bitArray = new BitArray(数量);
            try
            {
                if (plc.IsConnected)
                {
                    bitArray = (BitArray)plc.Read(DataType.Memory, 0, 地址M点, VarType.Bit, 数量);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return bitArray;
        }
        /// 
        /// 批量读取连续的 bool 值
        /// 
        /// 
        /// 
        /// 
        /// 
        public BitArray Get_BitArray(int DB, int 偏移量, int 数量)
        {
            BitArray bitArray = new BitArray(数量);
            try
            {
                if (plc.IsConnected)
                {
                    bitArray = (BitArray)plc.Read(DataType.DataBlock, DB, 偏移量, VarType.Bit, 数量);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return bitArray;
        }
        /// 
        /// 读取PLC DB块字节数据
        /// 
        /// DB块
        /// 开始的偏移量
        /// 读取的长度
        /// 
        public byte[] Get_bytes(int DB, int 偏移量, int 数量)
        {
            byte[] db_byte = new byte[数量];
            try
            {
                if (plc.IsConnected)
                {
                    db_byte = plc.ReadBytes(DataType.DataBlock, DB, 偏移量, 数量);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("读取PLC_byte_Error:" + ex.Message);
            }
            return db_byte;
        }
        /// 
        /// 读取PLC DB快16位有符号数据 
        /// 
        /// 
        /// 
        public short Get_int(int DB, int 偏移量)
        {
            short data = 0;
            try
            {
                if (plc.IsConnected)
                {
                    data = (short)plc.Read(DataType.DataBlock, DB, 偏移量, VarType.Int, 1);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("读取PLC_int_Error:" + ex.Message);
            }
            return data;
        }
        /// 
        /// 读取PLC DB块32位有符号数据
        /// 
        /// 
        /// 
        /// 
        public int Get_Dint(int DB, int 偏移量)
        {
            int data = 0;
            try
            {
                if (plc.IsConnected)
                {
                    data = (int)plc.Read(DataType.DataBlock, DB, 偏移量, VarType.DInt, 1);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("读取PLC_Dint出错:" + ex.Message);
            }

            return data;
        }
        /// 
        /// 读取PLC DB快16位无符号数据
        /// 
        /// 
        /// 
        public ushort Get_Word(int DB, int 偏移量)
        {
            ushort data = 0;
            try
            {
                if (plc.IsConnected)
                {
                    data = (ushort)plc.Read(DataType.DataBlock, DB, 偏移量, VarType.Word, 1);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("读取PLC_Word_Error:" + ex.Message);
            }
            return data;
        }
        /// 
        /// 读取PLC DB快32位无符号数据
        /// 
        /// 
        /// 
        /// 
        public uint Get_DWord(int DB, int 偏移量)
        {
            uint data = 0;
            try
            {
                if (plc.IsConnected)
                {
                    data = (uint)plc.Read(DataType.DataBlock, DB, 偏移量, VarType.DWord, 1);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("读取PLC_DWord出错:" + ex.Message);
            }
            return data;
        }
        /// 
        /// 读取PLC Timer 类型数据
        /// 
        /// 
        /// 
        public int Get_Timer(string 绝对地址)
        {
            object ss = 0;
            try
            {
                if (plc.IsConnected)
                {
                    ss = plc.Read(绝对地址.ToUpper());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("读取PLC_Timer_Error:" + ex.Message);
            }
            int fs = Convert.ToInt32(ss);
            return fs;
        }
        /// 
        /// 读取PLC Real 类型数据
        /// 
        /// PLC绝对地址
        /// 
        public float Get_Real(int DB, int 偏移量)
        {
            float result = 0;
            try
            {
                if (plc.IsConnected)
                {
                    result = (float)plc.Read(DataType.DataBlock, DB, 偏移量, VarType.Real, 1);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("读取PLC_Real_Error:" + ex.Message);
            }
            return result;
        }
        /// 
        /// 读取PLC string 类型数据
        /// 
        /// DB块
        /// 开始的偏移量
        /// 
        public string Get_String(int DB, int 偏移量)
        {
            string 返回值 = string.Empty;
            try
            {
                if (plc.IsConnected)
                {
                    //西门子字符串长度 第一个字节是字符串的总长度 第二个是字符串的当前长度 要从当前字节的+1字节读取当前长度
                    var bytelen = (byte)plc.Read(DataType.DataBlock, DB, 偏移量 + 1, VarType.Byte, 1);
                    返回值 = (string)plc.Read(DataType.DataBlock, DB, 偏移量 + 2, VarType.String, bytelen);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("读取PLC_String_Error:" + ex.Message);
            }
            return 返回值;
        }
        #endregion

        #region 写入PLC数据
        /// 
        /// 写入PLC输出点Q 地址
        /// 
        /// Q5.0 的5
        /// Q5.4 的4
        /// 值
        public void Set_Q(int 输出Q, int 第几位, bool 值)
        {
            try
            {
                if (plc.IsConnected)
                {
                    plc.WriteBit(DataType.Output, 0, 输出Q, 第几位, 值);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("写入Q点出错:" + ex.Message);
            }
        }
        /// 
        /// 写入PLC默认寄存器地址
        /// 
        /// 
        /// 
        /// 
        public void Set_M(int 地址M, int 第几位, bool 值)
        {
            try
            {
                if (plc.IsConnected)
                {
                    plc.WriteBit(DataType.Memory, 0, 地址M, 第几位, 值);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("写入M地址:" + ex.Message);
            }
        }
        /// 
        /// 写入PLCDB块 bool 类型地址
        /// 
        /// 
        /// 分配的第几个字节
        /// 字节里的第几位
        /// 
        public void Set_Bit(int DB, int 偏移量, int 第几位, bool 值)
        {
            try
            {
                if (plc.IsConnected)
                {
                    plc.WriteBit(DataType.DataBlock, DB, 偏移量, 第几位, 值);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("写入PLC_bool出错:" + ex.Message);
            }
        }
        /// 
        /// 写入PLC DB块16位有符号数据
        /// 
        /// 
        /// 
        public void Set_int(string 绝对地址, short 值)
        {
            try
            {
                if (plc.IsConnected)
                {
                    plc.Write(绝对地址.ToUpper(), 值);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("写入PLC_int出错:" + ex.Message);
            }
        }
        /// 
        /// 写入PLC DB块32位有符号数据
        /// 
        /// 
        /// 
        public void Set_Dint(string 绝对地址, int 值)
        {
            try
            {
                if (plc.IsConnected)
                {
                    plc.Write(绝对地址.ToUpper(), 值);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("写入PLC_Dint出错:" + ex.Message);
            }
        }
        /// 
        /// 写入PLC DB块16位无符号数据
        /// 
        /// 
        /// 
        public void Set_Word(string 绝对地址, ushort 值)
        {
            try
            {
                if (plc.IsConnected)
                {
                    plc.Write(绝对地址.ToUpper(), 值);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("写入PLC_Word出错:" + ex.Message);
            }
        }
        /// 
        /// 写入PLC DB块32位无符号数据
        /// 
        /// 
        /// 
        public void Set_DWord(string 绝对地址, uint 值)
        {
            try
            {
                if (plc.IsConnected)
                {
                    plc.Write(绝对地址.ToUpper(), 值);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("写入PLC_DWord出错:" + ex.Message);
            }
        }
        /// 
        /// 写入PLC Real 类型地址
        /// 
        /// 
        /// 
        public void Set_Real(string 绝对地址, float 值)
        {
            try
            {
                if (plc.IsConnected)
                {
                    plc.Write(绝对地址.ToUpper(), 值);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("写入PLC_Real出错:" + ex.Message);
            }
        }
        /// 
        /// 写入PLC Timer 类型地址
        /// 
        /// 
        /// 
        public void Set_Timer(string 绝对地址, int 值)
        {
            try
            {
                if (plc.IsConnected)
                {
                    plc.Write(绝对地址.ToUpper(), 值);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("写入PLC_Timer出错:" + ex.Message);
            }
        }
        /// 
        /// 写入PLC string 类型地址
        /// 
        /// DB块
        /// 分配的偏移量
        /// 
        public void Set_String(int DB, int 偏移量, string Values)
        {
            try
            {
                if (plc.IsConnected)
                {
                    var lenMax = (byte)plc.Read(DataType.DataBlock, DB, 偏移量, VarType.Byte, 1);//拿到地址申请的字节长度
                    plc.Write(DataType.DataBlock, DB, 偏移量, GetStringByteArray(lenMax, Values));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("写入PLC_String出错:" + ex.Message);
            }
        }
        /// 
        /// 把 string数据 转换成西门子String数据格式
        /// 
        /// 
        /// 
        private byte[] GetStringByteArray(byte LenMax, string Values)
        {
            byte[] byteArray = Encoding.Default.GetBytes(Values);
            byte[] sheel = new byte[2];
            sheel[0] = Convert.ToByte(LenMax);//申请的最大内存
            sheel[1] = Convert.ToByte(Values.Length);//当前字符长度
            return sheel.Concat(byteArray).ToArray();
        }
        #endregion
    }
}

你可能感兴趣的:(visual,studio,c#,开发语言)