S7NetPlus / s7netplus

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

TPKT error trying reading/writing merker value #185

Closed aetasoul closed 6 years ago

aetasoul commented 6 years ago

Hi all,

I'm trying the c# library on visual studio 2017, I have installed it via nuget packages. I'm getting this error when I run my application:

TPKT is incomplete / invalid

The cpu is S7-300.

This is my code:

using S7.Net;

Plc plc = new Plc(CpuType.S7300,"192.168.1.1",0,2)
plc.Open();

if(plc.isConnected && plc.isAvailable){
    plc.Write("M20.0",true);
    bool testRead = (bool)plc.Read("M20.1");
}

plc.Close();

I'm getting the error on: plc.Write("M20.0",true)

If I comment the line I get the error on the reading command.

On the PLC I have only the OB1 with one segment.

|-----|M20.0| -------(M20.1)---

Thanks for help!

jerko1979 commented 6 years ago

Hi, Maybe you should check your write permission on CPU settings? good luck!

jerko1979 commented 6 years ago

Also confirm that your CPU is mounted on Rack 0 && slot 2 in HW config?

mycroes commented 6 years ago

@aetasoul I'm not sure if anyone ever tested writing memory bits, if I look through the code it seems probable that it's broken (the vartype that is parsed from the address doesn't seem to be used anywhere). Can you verify that you have successful communication (read/write) in DB areas?

aetasoul commented 6 years ago

Hi,

This is my HW configuration: rack_slot

And this is the DB1 that I create for test: db1

I have changed my code like this:

using S7.Net;

Plc plc = new Plc(CpuType.S7300,"192.168.1.1",0,2) plc.Open();

if(plc.isConnected && plc.isAvailable){ plc.Write("DB1.DBX2.0",1); bool testRead = (bool)plc.Read("DB1.DBX2.1"); //plc.Write("M20.0",true); //bool testRead = (bool)plc.Read("M20.1"); }

plc.Close();

the error is the same:

TPKT is incomplete / invalid

@jerko1979 the racks and slot are right as yo ucan check in the picture. What do you mean with "Maybe you should check your write permission on CPU settings?"?

@mycroes I have checked this example for start: https://archive.codeplex.com/?p=s7net

Thanks for help guys!

jerko1979 commented 6 years ago

image

aetasoul commented 6 years ago

I have only the keyswitch settings checked.

No protection

protezione

This is the project, just dearchive it. If you want you can take a look!

Testcommunication.zip

Thanks!

jerko1979 commented 6 years ago

Maybe you should use sharp7,library , here is the link how to . I have never used s7net to be honest! https://www.mesta-automation.com/how-to-write-a-siemens-s7-plc-driver-with-c-and-sharp7/

mycroes commented 6 years ago

@aetasoul I think you might have an error in the Write call, I think you need to pass a bool instead of 1, although I'm 100% confident it's a problem.

plc.Write("DB1.DBX2.0", true);
bool testRead = (bool)plc.Read("DB1.DBX2.0");
}

Could you perhaps try to read just an int or dint? Also, make sure the exception is originating from the Read or Write calls, there have been issues with opening connection as well with some PLC's.

mycroes commented 6 years ago

Maybe you should use sharp7 , library here is the link how to . I have never uset s7net to be honest! https://www.mesta-automation.com/how-to-write-a-siemens-s7-plc-driver-with-c-and-sharp7/

@jerko1979 I don't think you're really helping @aetasoul by pointing him to another library (where I'm afraid the support will be far worse than here, since the developer is a C programmer that ported his code to C# following C conventions). You provided some good advice with regards to the settings, we should be able to get this problem solved by diagnosing what's going on.

aetasoul commented 6 years ago

I found the problem.

using S7.Net;

Plc plc = new Plc(CpuType.S7300,"192.168.1.1",0,2) plc.Open();

if(plc.isConnected && plc.isAvailable){ plc.Write("M20.0",true); bool testRead = (bool)plc.Read("M20.1"); }

plc.Close();

on this code if I remove the "if" statement for check if the plc.IsAvaiable && plc.IsConnected can write the memory bit 20.0 and read from the memory bit 20.1

the working code is this:

Plc plc = new Plc(CpuType.S7300,"192.168.1.1",0,2); plc.Open();

plc.Write("M20.0",true); bool testRead = Convert.ToBoolean(plc.Read("M20.1"));

plc.Close();

jerko1979 commented 6 years ago

Well Done ! 👍

aetasoul commented 6 years ago

It also works for DB. So you can read/write memory bit and DB.

Thanks all for the help!

If you need any help to test the library, or improve it tell me. I'm interested in it and I have this cpu: 315-2 PN/DP, 315-2 DP, S7-1500, S7-1200, ET200SP, s7-314, 316-2 DP, 317-2 DP

@mycroes if you want to do more test to check why this if statement cause the problem, just tell me.

mycroes commented 6 years ago

@aetasoul @jerko1979 Thanks to both of you. The IsAvailable method has been broken in recent versions, I'll make sure a separate issue covers this.

Mallowan commented 6 years ago

Just in case. v0.1.8 -0.1.9 IsConnected and IsAvailable work properly. As it should be.

Alkaidxx commented 2 years ago

how do you fanilly solve it? what's main problem, is s7.net or plc set wrong?