C# 读取西门子S7系列PLC教程及源码

这篇具有很好参考价值的文章主要介绍了C# 读取西门子S7系列PLC教程及源码。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

创建 PLC 实例,连接和断开连接

若要创建驱动程序的实例,需要使用此构造函数:

public Plc(CpuType cpu, string ip, Int16 rack, Int16 slot)
  • CPU:这指定您要连接到的 CPU。支持的 CPU 包括:
public enum CpuType {
    S7200 = 0,
    S7300 = 10,
    S7400 = 20,
    S71200 = 30,
    S71500 = 40,
}
  • ip:指定 CPU 或外部以太网卡的 IP 地址
  • 机架:它包含PLC的机架,您可以在Step7的硬件配置中找到
  • 插槽:这是CPU的插槽,您可以在Step7的硬件配置中找到

例:

此代码为 IP 地址为 7.300.127.0 的 S0-1 plc 创建一个 Plc 对象,为 CPU 位于插槽 0 的机架 2 中的 plc 创建一个 Plc 对象:

Plc plc = new Plc(CpuType.S7300, "127.0.0.1", 0, 2);

连接到 PLC

public void Open()

例如,这行代码打开连接:

plc.Open();

断开与 PLC 的连接

public void Close()

例如,这将关闭连接:

plc.Close();

错误处理

任何方法都可能导致各种错误。您应该实现正确的错误处理。 提供了 和 足够的错误消息。PlcExceptionPlcExceptionErrorCode

以下是错误的类型:

public enum ErrorCode
{
    NoError = 0,
    WrongCPU_Type = 1,
    ConnectionError = 2,
    IPAddressNotAvailable, 
    WrongVarFormat = 10,
    WrongNumberReceivedBytes = 11, 
    SendData = 20,
    ReadData = 30, 
    WriteData = 50
}

检查 PLC 可用性

要检查 plc 是否可用(打开套接字),您可以使用该属性

public bool IsAvailable

当您检查此属性时,驱动程序将尝试连接到 plc,如果它可以连接,则返回 true,否则返回 false。

检查 PLC 连接

检查 plc 连接是微不足道的,因为您必须检查 PC 插座是否已连接,而且还要检查 PLC 是否仍连接在插座的另一侧。 在这种情况下,您必须检查的属性是:

public bool IsConnected

在调用方法 Open() 并且结果成功后,可以检查此属性,以检查连接是否仍处于活动状态。

读取字节/写入字节

该库提供了几种读取变量的方法。基本和最常用的是ReadBytes。

public byte[] ReadBytes(DataType dataType, int db, int startByteAdr, int count)

public void WriteBytes(DataType dataType, int db, int startByteAdr, byte[] value)

这将从给定内存位置读取您指定的所有字节。此方法会自动处理多个请求,以防字节数超过单个请求中可以传输的最大字节数。

  • 数据类型:您必须使用枚举数据类型指定内存位置
public enum DataType
{
    Input = 129,
    Output = 130,
    Memory = 131,
    DataBlock = 132,
    Timer = 29,
    Counter = 28
}
  • db:数据类型的地址,例如如果要读取 DB1,此字段为 “1”;如果要读取 T45,则此字段为 45。
  • startByteAdr:要读取的第一个字节的地址,例如,如果要读取 DB1。DBW200,这是200。
  • 计数:包含要读取的字节数。
  • 值[ ]:要从PLC读取的字节数组。

例: 此方法读取 DB200 的前 1 个字节:

var bytes = plc.ReadBytes(DataType.DataBlock, 1, 0, 200);

读写解码

此方法允许根据提供的varType读取和接收已解码的结果。如果您读取多个相同类型的字段(例如 20 个连续的 DBW),这将非常有用。如果指定 VarType.Byte,则它具有与 ReadBytes 相同的功能。

public object Read(DataType dataType, int db, int startByteAdr, VarType varType, int varCount)

public void Write(DataType dataType, int db, int startByteAdr, object value)
  • 数据类型:您必须使用枚举数据类型指定内存位置
public enum DataType
{
    Input = 129,
    Output = 130,
    Memory = 131,
    DataBlock = 132,
    Timer = 29,
    Counter = 28
}
  • db:数据类型的地址,例如如果要读取 DB1,此字段为 “1”;如果要读取 T45,则此字段为 45。
  • startByteAdr:要读取的第一个字节的地址,例如,如果要读取 DB1。DBW200,这是200。
  • varType:指定要转换字节的数据。
public enum VarType
{
    Bit,
    Byte,
    Word,
    DWord,
    Int,
    DInt,
    Real,
    String,
    StringEx,
    Timer,
    Counter
}
  • count:包含要读取的变量数。
  • :要写入 PLC 的值数组。它可以是单个值或数组,只需记住该类型是唯一的(例如双精度数组、整数数组、短整型数组等)。

例: 此方法读取 DB20 的前 1 个 DWord:

var dwords = plc.Read(DataType.DataBlock, 1, 0, VarType.DWord, 20);

读取单个变量/写入单个变量

此方法通过解析字符串并返回正确的结果,从 plc 读取单个变量。虽然这是最简单的入门方法,但效率非常低,因为驱动程序为每个变量发送 TCP 请求。

public object Read(string variable)

public void Write(string variable, object value)
  • variable: specify the variable to read by using strings like “DB1.DBW20”, “T45”, “C21”, “DB1.DBD400”, etc.

Example: This reads the variable DB1.DBW0. The result must be cast to ushort to get the correct 16-bit format in C#.

ushort result = (ushort)plc.Read("DB1.DBW0");

读取结构/编写结构

This method reads all the bytes from a specified DB needed to fill a struct in C#, and returns the struct that contains the values. It is recommended to use when you want to read many variables in a single data block in some continuous memory range.

The "read struct" and "write struct" methods do not support strings.

public object ReadStruct(Type structType, int db, int startByteAdr = 0)

public void WriteStruct(object structValue, int db, int startByteAdr = 0)
  • structType: Type of the struct to be read, for example: typeOf(MyStruct))
  • db: index of the DB to read
  • startByteAdr: specified the first address of the byte to read (the default is zero).

Then add a struct into your .Net application that is similiar to the DB in the plc:

public struct testStruct
{
    public bool varBool0;
    public bool varBool1;
    public bool varBool2;
    public bool varBool3;
    public bool varBool4;
    public bool varBool5;
    public bool varBool6;
    public byte varByte0;
    public byte varByte1;
    public ushort varWord0;
    public float varReal0;
    public bool varBool7;
    public float varReal1;
    public byte varByte2;
    public UInt32 varDWord;
}

then add the code to read or write the complete struct

// reads a struct from DataBlock 1 at StartAddress 0
testStruct myTestStruct = (testStruct) plc.ReadStruct(typeof(testStruct), 1, 0);

Read a class / Write a class

This method reads all the bytes from a specified DB needed to fill a class in C#. The class is passed as reference and values are assigned by using reflection. It is recommended to use when you want to read many variables in a single data block in some continuous memory range.

The "read class" and "write class" methods do not support strings.

public void ReadClass(object sourceClass, int db, int startByteAdr = 0)

public void WriteClass(object classValue, int db, int startByteAdr = 0)
  • sourceClass: instance of the class that you want to assign the values
  • db: index of the DB to read
  • startByteAdr: specified the first address of the byte to read (the default is zero). 

Then add a struct into your .Net application that is similiar to the DB in the plc:

public class TestClass
{
    public bool varBool0 { get; set;}
    public bool varBool1 { get; set;}
    public bool varBool2 { get; set;}
    public bool varBool3 { get; set;}
    public bool varBool4 { get; set;}
    public bool varBool5 { get; set;}
    public bool varBool6 { get; set;}

    public byte varByte0 { get; set;}
    public byte varByte1 { get; set;}

    public ushort varWord0 { get; set;}

    public float varReal0 { get; set;}
    public bool varBool7 { get; set;}
    public float varReal1 { get; set;}

    public byte varByte2 { get; set;}
    public UInt32 varDWord { get; set;}
}

then add the code to read or write the complete class

// reads a class from DataBlock 1, startAddress 0
TestClass myTestClass = new TestClass();
plc.ReadClass(myTestClass, 1, 0);

Read multiple variables

This method reads multiple variables in a single request. The variables can be located in the same or in different data blocks.

public void Plc.ReadMultibleVars(List<DataItem> dataItems);
  • List<>: you have to specify a list of DataItem which contains all data items you want to read

Example: this method reads several variables from a data block

define the data items first

private static DataItem varBit = new DataItem()
{
    DataType = DataType.DataBlock,
    VarType = VarType.Bit,
    DB = 83,
    BitAdr = 0,
    Count = 1,
    StartByteAdr = 0,
    Value = new object()
};

private static DataItem varByteArray = new DataItem()
{
    DataType = DataType.DataBlock,
    VarType = VarType.Byte,
    DB = 83,
    BitAdr = 0,
    Count = 100,
    StartByteAdr = 0,
    Value = new object()
};

private static DataItem varInt = new DataItem()
{
    DataType = DataType.DataBlock,
    VarType = VarType.Int,
    DB = 83,
    BitAdr = 0,
    Count = 1,
    StartByteAdr = 102,
    Value = new object()
};

private static DataItem varReal = new DataItem()
{
    DataType = DataType.DataBlock,
    VarType = VarType.Real,
    DB = 83,
    BitAdr = 0,
    Count = 1,
    StartByteAdr = 112,
    Value = new object()
};

private static DataItem varString = new DataItem()
{
    DataType = DataType.DataBlock,
    VarType = VarType.StringEx,
    DB = 83,
    BitAdr = 0,
    Count = 20,         // max lengt of string
    StartByteAdr = 116,
    Value = new object()
};
    
private static DataItem varDateTime = new DataItem()
{
    DataType = DataType.DataBlock,
    VarType = VarType.DateTime,
    DB = 83,
    BitAdr = 0,
    Count = 1,
    StartByteAdr = 138,
    Value = new object()
}; 

Then define a list where the DataItems will be stored in

private static List<DataItem> dataItemsRead = new List<DataItem>();

add the data items to the list

dataItemsRead.Add(varBit);
dataItemsRead.Add(varByteArray);
dataItemsRead.Add(varInt);
dataItemsRead.Add(varReal);
dataItemsRead.Add(varString);
dataItemsRead.Add(varDateTime);

open the connection to the plc and read the items at once

myPLC.Open();

// read the list of variables
myPLC.ReadMultipleVars(dataItemsRead);

// close the connection
myPLC.Close();

// access the values of the list
Console.WriteLine("Int:" + dataItemsRead[2].Value);

Write multiple variables

This method writes multiple variables in a single request.

public void Plc.Write(Array[DataItem] dataItems);
  • Array[] you have to specifiy an array of DataItem which contains all the items you want to write

Example: this method writes multiple variables into one data block

define the data items

 private static DataItem varWordWrite = new DataItem()
{
    DataType = DataType.DataBlock,
    VarType = VarType.Word,
    DB = 83,
    BitAdr = 0,
    Count = 1,
    StartByteAdr = 146,
    Value = new object()
};

private static DataItem varIntWrite = new DataItem()
{
    DataType = DataType.DataBlock,
    VarType = VarType.Int,
    DB = 83,
    BitAdr = 0,
    Count = 1,
    StartByteAdr = 148,
    Value = new object()
};

private static DataItem varDWordWrite = new DataItem()
{
    DataType = DataType.DataBlock,
    VarType = VarType.DWord,
    DB = 83,
    BitAdr = 0,
    Count = 1,
    StartByteAdr = 150,
    Value = new object()
};

private static DataItem varDIntWrite = new DataItem()
{
    DataType = DataType.DataBlock,
    VarType = VarType.DInt,
    DB = 83,
    BitAdr = 0,
    Count = 1,
    StartByteAdr = 154,
    Value = new object()
};

private static DataItem varRealWrite = new DataItem()
{
    DataType = DataType.DataBlock,
    VarType = VarType.Real,
    DB = 83,
    BitAdr = 0,
    Count = 1,
    StartByteAdr = 158,
    Value = new object()
};

private static DataItem varStringWrite = new DataItem()
{
    DataType = DataType.DataBlock,
    VarType = VarType.StringEx,
    DB = 83,
    BitAdr = 0,
    Count = 20,
    StartByteAdr = 162,
    Value = new object()
};

Asign the values to the data items. Be aware to use the correct data conversion for the variables to fit into the S7-data types

// asign values to the variable to be written                
varWordWrite.Value = (ushort)67;
varIntWrite.Value = (ushort)33;
varDWordWrite.Value = (uint)444;
varDIntWrite.Value = 6666;
varRealWrite.Value = 77.89;
varStringWrite.Value = "Writting";

Then define a list to store the data items and add the created items to the list

private static List<DataItem> dataItemsWrite = new List<DataItem>();

// add data items to list of data items to write                
dataItemsWrite.Add(varWordWrite);
dataItemsWrite.Add(varIntWrite);
dataItemsWrite.Add(varDWordWrite);
dataItemsWrite.Add(varDIntWrite);
dataItemsWrite.Add(varRealWrite);
dataItemsWrite.Add(varStringWrite);

Finally open a connection to the plc and write the items at once. Use to convert the list into an array..ToArray()

// open the connection
myPLC.Open();

// write the items
myPLC.Write(dataItemsWrite.ToArray());

// close the connection
myPLC.Close();

开源下载链接:C#读取西门子S7系列PLC教程及源码Profinet-网络基础文档类资源-CSDN文库文章来源地址https://www.toymoban.com/news/detail-478510.html

到了这里,关于C# 读取西门子S7系列PLC教程及源码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 西门子S7-1200F或1500F系列安全PLC的组态步骤和基础编程(一)

    第一部分:组态配置 具体步骤可参考以下内容 : 如下图所示,新建一个项目后,添加一个安全型PLC,这里以1516F-3 PN/DP为例进行说明, 如下图所示,添加CPU完成后,可以看到左侧的项目树中比普通的PLC多了几个选项和模块, 如下图所示,我们选中该CPU后进入属性画面,在“

    2024年02月06日
    浏览(54)
  • 西门子PLC S7-1200如何实现远程上下载?

    西门子S7-1200是一款高性能的PLC,具有模块化、结构紧凑、功能全面、编程简单的特点,总工业自动化领域中应用广泛,如贴片系统、传送带系统、污水处理厂、配电站、能源管理系统。 在使用过程,无论是为了减少现场调试的成本时间,还是为了给客户提高更快更强的技术

    2024年02月12日
    浏览(36)
  • 西门子S7-1200与S7-300PLC的九大不同点

    S7-1200作为新推出的紧凑型控制器,其产品定位在原有的SIMATIC S7-200和S7-300之间,它与S7-300的区别主要体现在硬件、通信、工程、存储器、功能块、计数器、定时器、工艺功能等方面。 一、硬件的区别 在硬件扩展方面,S7-300的主机架多支持八个扩展模块,而S7-1200支持扩展多八

    2024年01月25日
    浏览(32)
  • 西门子PLC S7-1200程序实例,西门子1200与安川机器人TCP IP通讯

    西门子PLC S7-1200程序实例,博图版本V15 1,西门子1200与安川机器人TCP IP通讯,包含机器人GSD文件; 2,西门子1200控制6轴伺服电机,四台台脉冲控制台达B2伺服,两台PN通讯控制西门子V90伺服电机; 3,两台西门子1200开放式通讯交互数据联动; 4,与4台位移传感器modbus485轮询读取

    2024年04月10日
    浏览(34)
  • 两个西门子S7-1200PLC之间的TCP以太网通讯

    两个西门子S7-1200PLC之间的TCP以太网通讯 西门子S7_1200两个CPU之间的以太网通讯程序,一个做主站一个做从站,可实现两个CPU之间的数据发送和读取,外加两个西门子KTP1200 12寸的触摸屏, 两个西门子S7-1200 PLC之间的TCP以太网通讯 随着工业自动化的发展,越来越多的设备之间需

    2024年02月02日
    浏览(33)
  • 【西门子PLC S7-200smart与汇川变频器通过通讯控制】

    一,变频器通讯设置部分 先查看汇川变频器的使用说明书,将FD组“通讯参数设置”设置好对应波特率,数据格式,本机地址,通讯协议 FD-00 波特率设置   0:300bps 1:600bps 2:1200bps 3:2400bpd 4:4800bps 5:9600bps9:115200bps FD-01 数据格式    1:偶校验  2:奇校验  3:无校验 FD-02 本机地

    2024年02月04日
    浏览(54)
  • 西门子PLC S7-1200程序实例 西门子1200与安川机器人TCP/IP通讯,包含机器人GSD文件

    西门子PLC S7-1200程序实例,博图版本V15,仅供电气编程者学习借鉴, 1,西门子1200与安川机器人TCP/IP通讯,包含机器人GSD文件; 2,西门子1200控制6轴伺服电机,四台台脉冲控制台达B2伺服,两台PN通讯控制西门子V90伺服电机; 3,两台西门子1200开放式通讯交互数据联动; 4,与

    2024年02月11日
    浏览(38)
  • 西门子PLC S7-200SMART Modbus TCP通讯的步骤和要点

    Modbus TCP是一个非常传统,应用广泛的通讯协议,很多智能设备都支持该协议。西门子S7-200SMART及1200、1500系列都免费支持(300和400还是要高昂收费),并且做成了标准库,使用起来非常方便,下面简单介绍一下客户端的配置步骤,服务器的配置更加简单,可以自行摸索: 1、引

    2023年04月09日
    浏览(31)
  • 基于西门子PLC s7-1200 实现物料的上料,搅拌,排出的自动化过程。

      摘要:工业中有多种物料(本文为三种)需要上料,搅拌,排料的过程,通过分析流程,利用PLC实现过程的自动化。用博图软件进行编程,最后将自己的成果展示给大家。 一、工艺流程   当按下启动按钮时,物料一从出料口进入搅拌罐,接着当物料一到达最低液面,传感

    2024年02月02日
    浏览(54)
  • 使用IOT-Tree Server连接西门子PLC S7-300/1200/1500

    IOT-Tree Server是个开源物联网软件,可以作为组态软件成为自动化系统的上位软件。她提供了接入、数据组织管理、控制逻辑和人机交互多个方面的功能。从版本0.99开始,IOT-Tree Server新增了西门子以太网驱动,能够通过以太网的方式直接访问S7-300/1200/1500. S7-200 smart好像也支持

    2024年02月03日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包