赞
踩
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;
- /// <summary>
- /// 初始化S7NetPLC
- /// </summary>
- /// <param name="IP"></param>
- /// <param name="CPUType"></param>
- public S7NetPlc(string IP, string CPUType ="S71200")
- {
- CreatePlc(IP, CPUType);
- }
-
- /// <summary>
- /// 实例化PLC IP和CPU型号 CPU型号默认S71200
- /// </summary>
- /// <param name="ip"></param>
- /// <param name="cpu"></param>
- 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型号错误");
- }
- }
- /// <summary>
- /// 连接PLC 再次调用可重新连接
- /// </summary>
- public async void Connet()
- {
- try
- {
- await plc.OpenAsync();
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- /// <summary>
- /// PLC连接状态
- /// </summary>
- /// <returns></returns>
- public bool ConnetStatus()
- {
- return plc.IsConnected;
- }
-
- #region 读取PLC数据
- /// <summary>
- /// 读取PLC 输入点 i
- /// </summary>
- /// <param name="start">开始的地址</param>
- /// <param name="字节数">读取字节数量</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 读取PLC 输出点Q
- /// </summary>
- /// <param name="start">开始的地址</param>
- /// <param name="字节数">读取字节数量</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 读取PLC默认寄存器地址
- /// </summary>
- /// <param name="地址M点"></param>
- /// <param name="字节数"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 批量读取连续的 bool 值
- /// </summary>
- /// <param name="DB"></param>
- /// <param name="起始地址"></param>
- /// <param name="数量"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 读取PLC DB块字节数据
- /// </summary>
- /// <param name="DB">DB块</param>
- /// <param name="start">开始的偏移量</param>
- /// <param name="字节数">读取的长度</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 读取PLC DB快16位有符号数据
- /// </summary>
- /// <param name="绝对地址"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 读取PLC DB块32位有符号数据
- /// </summary>
- /// <param name="DB"></param>
- /// <param name="偏移量"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 读取PLC DB快16位无符号数据
- /// </summary>
- /// <param name="绝对地址"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 读取PLC DB快32位无符号数据
- /// </summary>
- /// <param name="DB"></param>
- /// <param name="偏移量"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 读取PLC Timer 类型数据
- /// </summary>
- /// <param name="绝对地址"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 读取PLC Real 类型数据
- /// </summary>
- /// <param name="绝对地址">PLC绝对地址</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 读取PLC string 类型数据
- /// </summary>
- /// <param name="DB">DB块</param>
- /// <param name="偏移地址">开始的偏移量</param>
- /// <returns></returns>
- 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数据
- /// <summary>
- /// 写入PLC输出点Q 地址
- /// </summary>
- /// <param name="输出Q">Q5.0 的5</param>
- /// <param name="第几位">Q5.4 的4</param>
- /// <param name="值">值</param>
- 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);
- }
- }
- /// <summary>
- /// 写入PLC默认寄存器地址
- /// </summary>
- /// <param name="地址M"></param>
- /// <param name="第几位"></param>
- /// <param name="值"></param>
- 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);
- }
- }
- /// <summary>
- /// 写入PLCDB块 bool 类型地址
- /// </summary>
- /// <param name="DB"></param>
- /// <param name="偏移量">分配的第几个字节</param>
- /// <param name="第几位">字节里的第几位</param>
- /// <param name="值"></param>
- 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);
- }
- }
- /// <summary>
- /// 写入PLC DB块16位有符号数据
- /// </summary>
- /// <param name="绝对地址"></param>
- /// <param name="值"></param>
- public void Set_int(string 绝对地址, short 值)
- {
- try
- {
- if (plc.IsConnected)
- {
- plc.Write(绝对地址.ToUpper(), 值);
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("写入PLC_int出错:" + ex.Message);
- }
- }
- /// <summary>
- /// 写入PLC DB块32位有符号数据
- /// </summary>
- /// <param name="绝对地址"></param>
- /// <param name="值"></param>
- public void Set_Dint(string 绝对地址, int 值)
- {
- try
- {
- if (plc.IsConnected)
- {
- plc.Write(绝对地址.ToUpper(), 值);
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("写入PLC_Dint出错:" + ex.Message);
- }
- }
- /// <summary>
- /// 写入PLC DB块16位无符号数据
- /// </summary>
- /// <param name="绝对地址"></param>
- /// <param name="值"></param>
- public void Set_Word(string 绝对地址, ushort 值)
- {
- try
- {
- if (plc.IsConnected)
- {
- plc.Write(绝对地址.ToUpper(), 值);
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("写入PLC_Word出错:" + ex.Message);
- }
- }
- /// <summary>
- /// 写入PLC DB块32位无符号数据
- /// </summary>
- /// <param name="绝对地址"></param>
- /// <param name="值"></param>
- public void Set_DWord(string 绝对地址, uint 值)
- {
- try
- {
- if (plc.IsConnected)
- {
- plc.Write(绝对地址.ToUpper(), 值);
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("写入PLC_DWord出错:" + ex.Message);
- }
- }
- /// <summary>
- /// 写入PLC Real 类型地址
- /// </summary>
- /// <param name="绝对地址"></param>
- /// <param name="值"></param>
- public void Set_Real(string 绝对地址, float 值)
- {
- try
- {
- if (plc.IsConnected)
- {
- plc.Write(绝对地址.ToUpper(), 值);
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("写入PLC_Real出错:" + ex.Message);
- }
- }
- /// <summary>
- /// 写入PLC Timer 类型地址
- /// </summary>
- /// <param name="绝对地址"></param>
- /// <param name="值"></param>
- public void Set_Timer(string 绝对地址, int 值)
- {
- try
- {
- if (plc.IsConnected)
- {
- plc.Write(绝对地址.ToUpper(), 值);
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("写入PLC_Timer出错:" + ex.Message);
- }
- }
- /// <summary>
- /// 写入PLC string 类型地址
- /// </summary>
- /// <param name="DB">DB块</param>
- /// <param name="偏移地址">分配的偏移量</param>
- /// <param name="值"></param>
- 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);
- }
- }
- /// <summary>
- /// 把 string数据 转换成西门子String数据格式
- /// </summary>
- /// <param name="datastring"></param>
- /// <returns></returns>
- 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
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。