使用s7.net plus库从S7-1200 PLC读取

时间:2016-10-31 12:18:24

标签: c# plc s7-1200 siemens

我尝试使用s7.net plus库从S7-1200 PLC读取值。当我尝试从数据块读取数据时,它返回"WrongVarFormat"消息。我的代码是:

    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"
        }
    }

另外,我设置了plc设置,我声明了数据块和值,如下所示: S7-1200 DB1

2 个答案:

答案 0 :(得分:0)

几乎整个方法public object Read(string variable)都被try / catch包装,当遇到任何异常时,它总是返回ErrorCode.WrongVarFormat。

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

无论在try-block中抛出什么exeception,代码总是返回ErrorCode.WrongVarFormat,并且有关崩溃的信息丢失。

作为调试的辅助工具,catch可以更改为:

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

代码应该为WrongVarFormat错误条件定义自己的异常类。 catch语句应该只捕获此异常,并且应该更改地址解析器中的throw-statements以抛出WrongVarFormat-Ecxeption。

除非您愿意更改库的代码,否则只能使用调试器来查找问题的原因。

答案 1 :(得分:0)

另外,为了以防万一,请检查PLC配置是否存在。如果设置不正确,PLC将拒绝任何请求。

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

相关问题