【问题标题】:Reading from S7-1200 PLC with s7.net plus library使用 s7.net plus 库从 S7-1200 PLC 读取
【发布时间】:2017-03-13 12:40:07
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: c# plc s7-1200 siemens


    【解决方案1】:

    几乎整个方法 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内部抛出什么异常,代码总是返回ErrorCode.WrongVarFormat,导致崩溃的信息丢失。

    作为调试的帮助,可以将 catch 更改为:

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

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

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

    【讨论】:

    • 我做了,发现,主要错误是“Wrong Number Received Bytes”...
    【解决方案2】:

    此外,以防万一,请检查 PLC 配置是否允许。如果设置不正确,PLC 将拒绝任何请求。

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

    【讨论】:

      【解决方案3】:
      1. 确保您的 plc 允许 Get/Put(在 HW-config 下)
      2. 您不能使用优化的块访问。

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 2016-02-28
      • 2014-10-06
      • 1970-01-01
      • 2016-06-13
      • 2015-10-07
      • 2017-04-04
      • 2022-08-13
      • 1970-01-01
      • 2016-11-18
      相关资源
      最近更新 更多