什么是Sharp7:
Sharp7 是 Snap7 Client 的 C# 端口。它不是包装器,即您没有加载 snap7.dll(或 .so)的接口代码,但它是 S7Protocol 的纯 C# 实现。
Sharp7 部署为单个源文件,其中包含一些可以直接在 .NET 项目中使用的类来与 S7 PLC 进行通信。
它设计用于与基于 .NET 的小型硬件配合使用,甚至适用于不需要扩展控制功能的大型项目。
主要特点:
- ·完全标准的“安全管理”C# 代码,无任何依赖性。
- ·事实上,每个具有能够运行 .NET Core 的以太网适配器的硬件都可以连接到 S7 PLC。
- ·打包协议标头以提高性能。
- ·帮助程序类可访问所有 S7 类型,而无需担心小端到大端的转换。
- ·兼容通用Windows平台,包括Win10 IoT for Raspberry。
- ·一个文件。
- ·兼容Snap7.net.cs,即插即用可更换。
- ·无需部署额外的库。
S7Client
-
连接方法
这些方法允许控制客户端对象的行为。
Function | Purpose |
ConnectTo | 客户端连接到PLC |
SetConnectionType | 设置连接类型(PG/OP/S7Basic) |
SetConnectionParams | 设置连接参数 |
Connect | 客户端通过隐士参数连接到PLC |
Disconnect | 断开客户端的连接 |
-
SetConnectionType
设置连接的类型,即客户端连接到PLC的方式
public void SetConnectionType(short ConnectionType)
连接类型表如下:
Connection Type |
Value |
Helper Const |
PG |
0x01 |
S7.PG |
OP |
0x02 |
S7.OP |
S7 Basic |
0x03..0x10 |
S7.S7_BASIC |
-
ConnectTo
将客户端连接到(IP、机架、插槽)对应处的硬件。
public int ConnectTo(String Address, int Rack, int Slot)
机架 插槽类型表如下:
S7 300 CPU |
0 |
2 |
Always |
S7 400 CPU |
Not fixed |
Follow the hardware configuration. |
|
WinAC CPU |
Not fixed |
Follow the hardware configuration. |
|
S7 1200 CPU |
0 |
0 |
Or 0, 1 |
S7 1500 CPU |
0 |
0 |
Or 0, 1 |
WinAC IE |
0 |
0 |
Or follow Hardware configuration. |
-
SetConnectionParams
设置ip 机架 插槽参数,此方法必须在Connect()之前调用
public void SetConnectionParams(String Address, int LocalTSAP,int RemoteTSAP)
-
Connect
使用先前调用ConnectTo () 或SetConnectionParams () 中指定的参数将客户端连接到 PLC。
public int Connect()
返回值:0代表连接成功。
其他值代表连接失败,请参考错误代码。
-
Disconnect
断开客户端与PLC的连接
public void Disconnect()
I/O基本功能方法
Function |
Purpose |
ReadArea |
从plc读取数据区域 |
WriteArea |
写进plc数据区域 |
ReadMultiVars |
从plc读取不同数据类型的数据 |
WriteMultiVars |
写进plc不同数据类型的数据 |
-
ReadArea
这是从PLC读取数据的主要功能。有了它,您可以读取 DB、输入、输出、Mekers、计时器和计数器。
public int ReadArea ( int Area, int DBNumber , int Start, int Amount, int WordLen , byte [] Buffer)
public int ReadArea ( int Area, int DBNumber , int Start, int Amount, int WordLen , byte [] Buffer, ref int BytesRead )
|
Type |
Dir. |
Mean |
Area |
int |
In |
区域标识 |
DBNumber |
int |
In |
如果Area=S7A热爱DB,则为DB号,否则忽略 |
Start |
int |
In |
开始偏移量 |
Amount |
int |
In |
读取元素数量 |
Wordlen |
int |
In |
Word size (2) |
Buffer |
Byte Buffer |
In |
缓冲 |
BytesRead |
int |
Out |
读取的字节数 |
Area table
|
Value |
Mean |
S7Consts.S7AreaPE |
0x81 |
Process Inputs. |
S7Consts.S7AreaPA |
0x82 |
Process Outputs. |
S7Consts.S7AreaMK |
0x83 |
Merkers. |
S7Consts.S7AreaDB |
0x84 |
DB |
S7Consts.S7AreaCT |
0x1C |
Counters. |
S7Consts.S7AreaTM |
0x1D |
Timers |
WordLen table
|
Value |
Mean |
S7WLBit |
0x01 |
Bit (inside a word) |
S7WLByte |
0x02 |
Byte (8 bit) |
S7WLWord |
0x04 |
Word (16 bit) |
S7WLDWord |
0x06 |
Double Word (32 bit) |
S7WLReal |
0x08 |
Real (32 bit float) |
S7WLCounter |
0x1C |
Counter (16 bit) |
S7WLTimer |
0x1D |
Timer (16 bit) |
Return value
- 0 : The function was accomplished with no errors.
- Other values : see the Errors Code List.
Remarks
Buffer size (byte) = Word size * Amount
|
Word size |
S7Consts.S7WLBit |
1 |
S7Consts.S7WLByte |
1 |
S7Consts.S7WLWord |
2 |
S7Consts.S7WLDWord |
4 |
S7Consts.S7WLReal |
4 |
S7Consts.S7WLCounter |
2 |
S7Consts.S7WLTimer |
2 |
-
WriteArea
这是将数据写入PLC的主要功能。它是ReadArea ()的补充函数,参数及含义相同。唯一的区别是数据从字节缓冲区传输到 PLC。
public int WriteArea(int Area, int DBNumber, int Start, int Amount, int WordLen, byte[] Buffer)
public int WriteArea(int Area, int DBNumber, int Start, int Amount, int WordLen, byte[] Buffer, ref int BytesWritten)
-
ReadMultiVars
该功能允许在一次调用中从 PLC 读取不同类型的变量。有了它,您可以读取 DB、输入、输出、Mekers、计时器和计数器。
public int ReadMultiVars(S7DataItem[] Items, int ItemsCount)
|
Type |
Dir. |
|
Item |
S7DataItem[] |
In |
See below //见下文 |
ItemsCount |
integer |
In |
Number of Items to read. //读取数量 |
S7DataItem struct
|
Type |
Dir. |
|
Area |
int |
In |
Area identifier. |
Wordlen |
int |
In |
Word size |
Result |
int |
Out |
Item operation result (2) //条目操作结果 |
DBNumber |
int |
In |
DB Number if Area = S7AreaDB, otherwise is ignored. |
Start |
int |
In |
Offset to start //开始偏移位置 |
Amount |
int |
In |
Amount of words to read (1) //字数量 |
pData |
IntPtr |
In |
Pointer to user Buffer //指向用户缓冲区的指针 |
-
WriteMultiVars
该功能允许在一次调用中从 PLC 写入不同类型的变量。有了它,您可以读取 DB、输入、输出、Mekers、计时器和计数器。
public int WriteMultiVars(S7DataItem[] Items, int ItemsCount)
|
Type |
Dir. |
|
Item |
S7DataItem[] |
In |
See below |
ItemsCount |
integer |
In |
Number of Items to write. |
|
Type |
Dir. |
|
Area |
int |
In |
Area identifier. |
Wordlen |
int |
In |
Word size |
Result |
int |
Out |
Item operation result (2) |
DBNumber |
int |
In |
DB Number if Area = S7AreaDB, otherwise is ignored. |
Start |
int |
In |
Offset to start |
Amount |
int |
In |
Amount of words to write (1) |
pData |
IntPtr |
In |
Pointer to user Buffer |
-
Lean Data I/O Functions
这些实用函数可简化ReadArea和WriteArea的使用。即,它们在内部调用ReadArea 和WriteArea传递正确的Area 和 WordLen。
Function |
Purpose |
DBRead |
Reads a part of a DB from a PLC. //从plc的db中读取数据 |
DBWrite |
Writes a part of a DB into a PLC. //写进数据到plc的db中 |
ABRead |
Reads a part of IPU area from a PLC. //... |
ABWrite |
Writes a part of IPU area into a PLC.//... |
EBRead |
Reads a part of IPI area from a PLC.//... |
EBWrite |
Writes a part of IPI area into a PLC.//... |
MBRead |
Reads a part of Merkers area from a PLC.//... |
MBWrite |
Writes a part of Merkers area into a PLC.//... |
TMRead |
Reads timers from a PLC. //... |
TMWrite |
Write timers into a PLC. //... |
CTRead |
Reads counters from a PLC. //... |
CTWrite |
Write counters into a PLC. //... |
-
面向块的方法
Function |
Purpose |
GetAgBlockInfo |
Returns info about a given block in PLC memory. //返回plc中块的信息 |
DBGet |
Uploads a DB from the PLC //从plc上传db块数据 |
DBFill |
Fills a DB into the PLC with a given value. //填充plc中的db块 |
-
Date/TIme Functions
Function |
Purpose |
GetPlcDateTime |
Returns the PLC date/time.返回PLC日期和时间 |
SetPlcDateTime |
Sets the PLC date/time with a given value.设置PLC日期和时间 |
SetPlcSystemDateTime |
Sets the PLC date/time with the host (PC) date/time.使用主机日期/时间设置PLC日期和时间 |
public int GetPlcDateTime(ref DateTime DT)
DateTime |
Date |
In |
DateTime class instance |
public int SetPlcDateTime(DateTime DT)
public int SetPlcSystemDateTime()
-
获取系统信息方法
Function |
Purpose |
ReadSZL |
Reads a partial list of given ID and Index.读取给定ID和索引 |
GetOrderCode |
Returns the CPU order code.返回CPU订购代码 |
GetCpuInfo |
Returns some information about the AG.返回AG信息 |
GetCpInfo |
Returns some information about the CP (communication processor).返回CP信息 |
-
PLC控制功能方法
Function |
Purpose |
PlcColdStart |
Puts the CPU in RUN mode performing an COLD START.//运行模式,执行冷启动 |
PlcHotStart |
Puts the CPU in RUN mode performing an HOT START.//RUN运行,执行热启动 |
PlcStop |
Puts the CPU in STOP mode.//STOP模式 |
PlcGetStatus |
Returns the CPU status (running/stopped).//返回CPU状态 |
S7 Helper Reference
该帮助类将“S7”值读/写到字节缓冲区中,支持所有1200/1500数据类型
- 读取方法的格式:
public static <.NET native Type> Get<S7 Type>At(byte[] Buffer, int Pos)
从字节缓冲区中字节号为“Pos”开始提取S7 Type类型的值
- 写值方法的格式:
public static void Set<S7 Type>At(byte[] Buffer, int Pos, <.NET native Type> Value)
将.net类型,插入到缓冲区字节号为“Pos”的位置开始的字节中
标签:基本,Sharp7,Area,int,S7Consts,S7,介绍,PLC,public From: https://www.cnblogs.com/davisdabing/p/17822203.html