3

I'm trying to read values from S7-1200 PLC using s7.net plus library. When I try to read data from datablocks it returns "WrongVarFormat" message. My code is:

    using (var plc = new Plc(CpuType.S71200, "192.168.1.17", 0, 0))
    {
    //IP is responding
    if (plc.IsAvailable)
    {
        ErrorCode connectionResult = plc.Open();
        //Connection successful
        if (connectionResult.Equals(ErrorCode.NoError))
        {
            //Get data
            object b2 = plc.Read("DB1.DBD38");//This part always return "WrongVarFormat"
        }
    }

Also, I set the plc settings and i declare the datablock and values as this: S7-1200 DB1

ZF007
  • 3,708
  • 8
  • 29
  • 48
Dnate
  • 31
  • 1
  • 5

3 Answers3

1

Also, just in case, check the PLC configuration for permissions.
If the setup is not ok, the PLC will refuse any requests.

https://www.youtube.com/watch?v=tYTjNG8YL-c

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
0

Almost the entire method public object Read(string variable) is wrapped by try/catch and it always returns ErrorCode.WrongVarFormat, when any exception is hit.

    public object Read(string variable)
    {
        ...
        try
        {
            ...
        }
        catch 
        {
            lastErrorCode = ErrorCode.WrongVarFormat;
            lastErrorString = "Die Variable '" + variable + "' konnte nicht entschlüsselt werden!";
            return lastErrorCode;
        }
    }

No matter, what exeception is thrown inside the try-block, the code always returns ErrorCode.WrongVarFormat and the information about the crash is lost.

As an aid in debugging, the catch can be changed to:

catch (Exception ex)
{
    Console.WriteLine("Got exception {0}\n", ex.ToString());
    ...

The code should define its own exception class for WrongVarFormat error conditions. The catch-statement should catch only this exception and the throw-statements in the address parser should be changed to throw the WrongVarFormat-Ecxeption.

Unless you are willing to change the code of the library, you can only use a debugger to find the cause of your problem.

Mario Klebsch
  • 361
  • 2
  • 12
0
  1. Make sure your plc have Get/Put allowed (under HW-config)
  2. You cant use optimezed block access.
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 02 '21 at 17:15