简体   繁体   English

在 C# 中使用 S7NetPlus 库的 PLC 通信错误

[英]PLC communication error using S7NetPlus library in C#

I am developing a windows forms application which will communicate with the Siemens S7-1200 PLC for real accessing real time data from PLC and sending data to PLC.我正在开发一个 windows forms 应用程序,它将与 Siemens S7-1200 PLC 通信,以实时访问来自 PLC 的实时数据并将数据发送到 PLC。 The connection and real time communication is all working fine.连接和实时通信一切正常。 But I'm facing issues when但是当我遇到问题时

  1. Closing the connection to PLC.关闭与 PLC 的连接。
  2. Ethernet cable is disconnected以太网电缆已断开
  3. The PLC is powered off PLC 断电

1. I have used Plc.close() method in the form.closed event. 1. 我在 form.close 事件中使用了 Plc.close() 方法。 But the connection is not getting closed, instead throwing PLC disconnection error.但是连接没有关闭,而是引发 PLC 断开连接错误。

2 & 3. I expected same behavior but it is throwing different errors. 2 & 3. 我期望相同的行为,但它会引发不同的错误。 When ethernet cable was disconnected either at application side or PLC side, the ethernet socket exception is thrown.当以太网电缆在应用程序端或 PLC 端断开连接时,将引发以太网套接字异常。 When the PLC power was turned off the program was still trying to write data to the PLC for some time and then showed the ethernet socket error happened.当 PLC 电源关闭时,程序仍在尝试向 PLC 写入数据一段时间,然后显示发生以太网套接字错误。

Can anyone help me with how to handle these errors?谁能帮我处理这些错误?

As you can see on s7netplus repository on Github the PLC class is implementig IDisposeable.正如您在 Github 上的s7netplus存储库中看到的那样,PLC class 实现了 IDisposeable。 In your question there is no code but if you are calling the PLC class with a using statment:在您的问题中没有代码,但如果您使用 using 语句调用 PLC class:

using(Plc plc = new PLC(...) {
  // Do some actions
}

the Close() method is called after the end of the using block (Dispose).在 using 块 (Dispose) 结束后调用 Close() 方法。 So if you are trying to call the Close() method again an exception occurs.因此,如果您尝试再次调用 Close() 方法,则会发生异常。

If it is possible, please provide a code example.如果可能,请提供代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM