C# TCP 连接到 Siemens S7 PLC - 网络新手

C# TCP Connection to Siemens S7 PLC - Network Newbie

我想在 PC 和 s7-PLC 之间建立双向通信设置。 到目前为止,我已经成功地使用此处找到的开源驱动程序以一种方式进行了设置: https://github.com/killnine/s7netplus

这个驱动程序允许我告诉 PC 从 PLC 获取数据,也可以写入数据。但问题是我想为特定的 PLC 数据设置一个 onChanged 事件,目前实现这一目标的唯一方法是频率轮询 PLC 的数据,直到我注意到它已经改变。

我希望它更受中断驱动,因此 PLC 在数据更改时异步向 PC 发送信号,从而实现更快的响应并减少网络流量。

所以我找到了西门子Open Communication Wizard,基本上在PLC端配置了一个可以在PLC代码中引用的连接。我已经设置了连接,因此 PLC 处于被动状态并正在寻找端口 2000:

现在在我的 C# 端,我将如何建立连接,只使用套接字?

// open the channel
_mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

_mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
_mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);

IPEndPoint server = new IPEndPoint(IPAddress.Parse(IP), 2000);
_mSocket.Connect(server);

我真的是网络新手。如果 PC 是主动伙伴并且我建立了连接,我希望能够异步地接受来自 PLC 的数据, 如果我在某处设置 socket.BeginRecieve() ,我仍然可以通过 PC->PLC 的套接字进行主动通信,还是会卡在等待 BeginRecieve 中?

我知道这是一个旧线程,但现在有一个比 s7netplus 更好的工具。

查看:Snap7 - Step7 Ethernet Communication Suite

Davide Nardella 的 LGPL3 优秀工具。

Overview

Snap7 is an open source, 32/64 bit, multi-platform Ethernet communication suite for interfacing natively with Siemens S7 PLCs. The new CPUs 1200/1500, the old S7200, the small LOGO 0BA7/0BA8 and SINAMICS Drives are also partially supported.

Although it has been designed to overcome the limitations of OPC servers when transferring large amounts of high speed data in industrial facilities, it scales well down to small Linux based arm or mips boards such as Raspberry PI, BeagleBone Black, pcDuino, CubieBoard, UDOO and ARDUINO YUN.

Three specialized components, Client, Server and Partner, allow you to definitively integrate your PC based systems into a PLC automation chain.