logo资料库

S7.NET中文说明书.pdf

第1页 / 共17页
第2页 / 共17页
第3页 / 共17页
第4页 / 共17页
第5页 / 共17页
第6页 / 共17页
第7页 / 共17页
第8页 / 共17页
资料共17页,剩余部分请下载后查看
S7.Net documentation S7.Net 文档 How to download s7.Net 如何下载 s7.Net The official repository is on GitHub (https://github.com/killnine/s7netplus) , you can also download the library directly from NuGet (https://www.nuget.org/packages/S7netplus/). 官 方 存 储 库 位 于 GitHub ( https://github.com/killnine/s7netplus ) 上 , 您 也 可 以 直 接 从 NuGet (https://www.nuget.org/packages/S7netplus/)下载该库。 What is S7.Net 什么是 S7.Net S7.Net is a plc driver that works only with Siemens PLC and only with Ethernet connection. This means that your plc must have a Profinet CPU or a profinet external card (CPxxx card). S7.Net is written entirely in C#, so you can debug it easily without having to go through native dlls. S7.Net 是一个 plc 驱动程序,仅适用于 Siemens PLC 和以太网连接。 这意味着 您的 PLC 必须具有 Profinet CPU 或 Profinet 外部卡(CPxxx 卡)。 S7.Net 完全用 C#编写,因此您无需通过本机 dll 即可轻松调试它。 Supported PLC 支持的 PLC S7.Net is compatible with S7-200, S7-300, S7-400, S7-1200, S7-1500. S7.Net 与 S7-200,S7-300,S7-400,S7-1200 和 S7-1500 兼容。
Getting started with S7.Net S7.Net 入门 To get started with S7.Net you have to download and include the S7.Net.dll in your project. You can do this by downloading the NuGet package, or by downloading the sources and compile them. 要开始使用 S7.Net,您必须下载 S7.Net.dll 并将其包含在您的项目中。 你可以这样做 通过下载 NuGet 软件包,或通过下载源代码并进行编译。 Create a PLC instance, connect and disconnect 创建一个 PLC 实例,连接和断 开连接 To create an instance of the driver you need to use this constructor: 要创建驱动程序的实例,您需要使用以下构造函数: public Plc(CpuType cpu, string ip, Int16 rack, Int16 slot) Cpu: this specify what CPU you are connecting to. The supported CPU are: public enum CpuType { S7200 = 0, S7300 = 10, S7400 = 20, S71200 = 30, S71500 = 40, } Ip: this contains the IP address of the CPU of external Ethernet card 包含外部以太网卡 CPU 的 IP 地址 Rack: this contains the rack of the plc, that you can find in hardware configuration in Step7 它包含 PLC 的机架,您可以在 Step7 的硬件配置中找到该机架 Slot: this is the slot of the CPU, that you can find in hardware configuration in Step7 这是 CPU 的插槽,您可以在 Step7 的硬件配置中找到它 Example: This code creates a Plc object for a S7-300 plc at the IP address 127.0.0.1, that it’s localhost, for a plc that it’s in rack 0 and a cpu that it’s in slot 2 of the hardware configuration: 例: 这段代码为 IP 地址为 127.0.0.1 的 S7-300 plc 创建了一个 Plc 对象,该对象是本地主机,对于 它在机架 0 中,而 CPU 在硬件配置的插槽 2 中: Plc plc = new Plc(CpuType.S7300, "127.0.0.1", 0, 2); Connecting to the PLC 连接到 PLC public ErrorCode Open() For example this line of code open the connection: 例如,以下代码行打开连接: plc.Open();
Disconnecting from the PLC 与 PLC 断开连接 public void Close() For example this closes the connection: 例如,这将关闭连接: plc.Close(); Error handling 错误处理 The Open() method returns an ErrorCode to check if the operation was successful. You should always check that it returns ErrorCode.NoError. These are the types of errors: Open()方法返回一个 ErrorCode 来检查操作是否成功。 你应该总是 检查它是否返回 ErrorCode.NoError。 这些是错误的类型: public enum ErrorCode { NoError = 0, WrongCPU_Type = 1, ConnectionError = 2, IPAddressNotAvailable, WrongVarFormat = 10, WrongNumberReceivedBytes = 11, SendData = 20, ReadData = 30, WriteData = 50 } Global error handling 全局错误处理 Not all methods returns an error. You can check for 并非所有方法都返回错误。 您可以检查 public ErrorCode LastErrorCode and public string LastErrorString on every methods that you execute, in order to catch errors while running the driver. 在执行的每个方法上,以在运行驱动程序时捕获错误。
Check PLC availability 检查 PLC 的可用性 To check if the plc is available you can use the property 要检查 plc 是否可用,您可以使用属性 public bool IsAvailable When you check this property, the driver will send a ping to the plc and returns true if the plc responds to the ping, false otherwise. 当您检查此属性时,驱动程序将向 ping 发送 ping 并在 plc 响应时返回 true。 ping,否则为 false。 Check PLC connection 检查 PLC 连接 Checking the plc connection is trivial, because you have to check if the PC socket is connected but also if the PLC is still connected. 检查 plc 连接很简单,因为您必须检查 PC 插座是否已连接,而且还要检查 PLC 仍然连接。 The property that you have to check in this case is this: 在这种情况下,您必须检查的属性是: public bool IsConnected This property can be checked after you called the method Open(), to check if the connection is still alive. 在调用方法 Open()之后,可以检查此属性,以检查连接是否仍然有效。
Read bytes / Write bytes 读取字节/写入字节 The library offers several methods to read variables. The basic one and the most used is ReadBytes. 该库提供了几种读取变量的方法。 最基本的也是最常用的是 ReadBytes。 public byte[] ReadBytes(DataType dataType, int db, int startByteAdr, int count) public ErrorCode WriteBytes(DataType dataType, int db, int startByteAdr, byte[] value) This reads up to 200 bytes (actual limit of the protocol) from a memory location that you determine. 这将从您确定的内存位置读取最多 200 个字节(协议的实际限制)。 dataType: you have to specify the memory location with the enum DataType 您必须使用枚举 DataType 指定内存位置 public enum DataType { Input = 129, Output = 130, Memory = 131, DataBlock = 132, Timer = 29, Counter = 28 } db: this is the address of the dataType, for example if you want to read DB1, this field is “1”; if you want to read T45, this field is 45. 这是 dataType 的地址,例如,如果您想读取 DB1,则此字段为“ 1”; 如果你 要读取 T45,此字段为 45。 startByteAdr: this is the address of the first byte that you want to read, for example if you want to read DB1.DBW200, this is 200. 这是您要读取的第一个字节的地址,例如,如果您 想读取 DB1.DBW200,这是 200。 count: this contains how many bytes you want to read. It’s limited to 200 bytes and if you need more, you must use recursion. 它包含您要读取的字节数。 长度限制为 200 个字节,如果需要更多字节,则必须使用递归。 Value[]: array of bytes to be written to the plc. 要写入 plc 的字节数组。 Example: This method reads the first 200 bytes of DB1: 例: 此方法读取 DB1 的前 200 个字节: var bytes = plc.ReadBytes(DataType.DataBlock, 1,0,200); Example with recursion: 递归示例:
private List ReadMultipleBytes(int numBytes, int db, int startByteAdr = 0) { List resultBytes = new List(); int index = startByteAdr; while (numBytes > 0) { var maxToRead = (int)Math.Min(numBytes, 200); byte[] bytes = ReadBytes (DataType.DataBlock, db, index, (int)maxToRead); if (bytes == null) return new List(); resultBytes.AddRange(bytes); numBytes -= maxToRead; index += maxToRead; } return resultBytes; }
Read and decode / Write decoded 读取和解码/写入解码 This method permits to read and receive an already decoded result based on the varType provided. This is useful if you read several fields of the same type (for example 20 consecutive DBW). This is also limited to maximum 200 bytes. If you specify VarType.Byte, it has the same functionality as ReadBytes. 此方法允许基于提供的 varType 读取和接收已经解码的结果。 这是 如果您读取多个相同类型的字段(例如 20 个连续的 DBW),则很有用。 这也仅限于 最多 200 个字节。 如果指定 VarType.Byte,则它具有与 ReadBytes 相同的功能。 public object Read(DataType dataType, int db, int startByteAdr, VarType varType, int va rCount) public ErrorCode Write(DataType dataType, int db, int startByteAdr, object value) dataType: you have to specify the memory location with the enum DataType 您必须使用枚举 DataType 指定内存位置 public enum DataType { Input = 129, Output = 130, Memory = 131, DataBlock = 132, Timer = 29, Counter = 28 } db: this is the address of the dataType, for example if you want to read DB1, this field is “1”; if you want to read T45, this field is 45. 这是 dataType 的地址,例如,如果您想读取 DB1,则此字段为“ 1”; 如果你 要读取 T45,此字段为 45。 startByteAdr: this is the address of the first byte that you want to read, for example if you want to read DB1.DBW200, this is 200. 这是您要读取的第一个字节的地址,例如,如果您 想读取 DB1.DBW200,这是 200。 varType: specify the data that you want to get your bytes converted. 指定要转换字节的数据。 public enum VarType { Bit, Byte, Word, DWord, Int, DInt, Real, String, Timer, Counter }
count: this contains how many variables you want to read. It’s limited to 200 bytes and if you need more, you must use recursion. count:包含要读取的变量数。 最多 200 个字节,如果您 需要更多,必须使用递归。 Value: array of values to be written to the plc. It can be a single value or an array, the important is that the type is unique, for example array of double, array of int, array of shorts, etc.. 要写入 plc 的值数组。 它可以是单个值或数组,重要的是 类型是唯一的,例如双精度数组,整数数组,短裤数组等。 Example: This method reads the first 20 DWords of DB1: 例: 此方法读取 DB1 的前 20 个 DWord: var dwords = plc.Read(DataType.DataBlock, 1,0, VarType.DWord, 20);
分享到:
收藏