我是 Modbus 和 UWA 的新手,我正在尝试制作一个应用程序,从我的西门子徽标设备 (6ED1052-1HB08-0BA0 LOGO!24RCE) 读取输入 1、输出 1 和输出 2 的状态。 Logo 正在运行一个简单的测试程序。这是我到目前为止想出的代码。
(other packages)
using EasyModbus;
using System.Net.Sockets;
namespace AC_C_Demo
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
private ModbusClient modbusClient;
public MainPage()
{
this.InitializeComponent();
modbusClient = new ModbusClient("192.168.0.3", 502); // Replace with your device's IP address and port
ReadModbusData_Click();
}
private async void ReadModbusData_Click()
{
modbusClient.Connect();
while (true)
{
// Read input 1
bool i1Value = modbusClient.ReadDiscreteInputs(0, 1)[0];"I1"
// Read output 1
bool q1Value = modbusClient.ReadCoils(0, 1)[0];
// Read output 1
bool q2Value = modbusClient.ReadCoils(1, 1)[0];
// Display the values in TextBlocks
InputStatusTextBlock.Text = "I1: " + i1Value.ToString();
// Q1ValueTextBlock.Text = "Q1: " + q1Value.ToString();
// Q2ValueTextBlock.Text = "Q2: " + q2Value.ToString();
}
}
}
}
然而,
bool i1Value = modbusClient.ReadDiscreteInputs(0, 1)[0];
行给了我以下错误:
System.IO.IOException: 'Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.'
SocketException: An existing connection was forcibly closed by the remote host
我不知道是什么原因造成的,我的设备通过以太网连接,并且已经将我的连接配置在范围内,并且我已经尝试关闭防火墙,但没有任何变化。
或者有更好的方法来实现我想要实现的目标吗?
我尝试了不同的 Modbus 软件包并以同样的问题结束。