我正在尝试使用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
发布于 2016-11-01 03:31:47
几乎整个方法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。
除非您愿意更改库的代码,否则只能使用调试器来查找问题的原因。
发布于 2016-12-18 05:33:36
此外,以防万一,请检查PLC配置是否有权限。如果设置不正常,PLC将拒绝任何请求。
发布于 2021-12-02 12:06:48
https://stackoverflow.com/questions/40342039
复制相似问题