S7NetPlus / s7netplus

S7.NET+ -- A .NET library to connect to Siemens Step7 devices
MIT License
1.35k stars 595 forks source link

S7.Net.PlcException : Index was outside the bounds of the array. #470

Closed OlivierMiracle closed 1 year ago

OlivierMiracle commented 1 year ago

Hi! I am trying to get communication to work, but I am getting an exception. There are steps i took:

Next, I have created a few unit test methods:

For checking avaibility:

[Fact]
public void PlcRead_IsAvailable()
{
    //Arrange
    Plc plc = new Plc(CpuType.S71200, "192.168.0.1", 0, 0);

    //Act
    bool result = plc.IsAvailable;

    //Assert
    Assert.True(result);
}

For checking connection:

[Fact]
public void PlcRead_IsConnected()
{
    //Arrange
    Plc plc = new Plc(CpuType.S71200, "192.168.0.1", 0, 0);

    //Act
    plc.Open();
    bool result = plc.IsConnected;
    plc.Close();

    //Assert
    Assert.True(result);
}

And, finally, for write and read instructions.

[Fact]
public void PlcRead_WriteAndRead()
{
    //Arrange
    Plc plc = new Plc(CpuType.S71200, "192.168.0.1", 0, 0);
    byte expected = 10;
    byte result;

    //Act
    plc.Open();
    plc.Write("DB1.DBB0", expected);

    result = (byte)plc.Read("DB1.DBB0");

    //Assert
    Assert.Equal(expected, result);
}

Only the last one is failing with exception:

S7.Net.PlcException : Index was outside the bounds of the array.
---- System.IndexOutOfRangeException : Index was outside the bounds of the array.

The problem doesn't not depend on type of variables, their number, casting etc.

kristjanp28 commented 1 year ago

Hello, I have 1500 PLC but it should work the same. I will also try on 1200. Was having same problems and I checked PUT/GET communication in "Protection & Security" settings.

Don't forget to go offline and download hardware changes! Can be a small thing but easy to forget.

OlivierMiracle commented 1 year ago

Ok, I needed to do what you said. Making changes in hardware settings and then clicking "download to device" seems to not change parameters in PLC itself.

To do that properly I had to choose PLC_1(my plc) > Download to device > Hardware configuration

I am overly descriptive for other lost souls like me.