读取S7-1200 PLC与s7.net +库
本文关键字:net s7 S7-1200 PLC 读取 | 更新日期: 2023-09-27 18:15:43
我正试图使用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 - 1200db1
另外,为了以防万一,检查PLC配置的权限。
如果设置不正确,PLC将拒绝任何请求。
几乎整个方法public object Read(string variable)
都被try/catch包装,它总是返回ErrorCode。错误varformat,当遇到异常时。
public object Read(string variable)
{
...
try
{
...
}
catch
{
lastErrorCode = ErrorCode.WrongVarFormat;
lastErrorString = "Die Variable '" + variable + "' konnte nicht entschlüsselt werden!";
return lastErrorCode;
}
}
无论在try块中抛出什么异常,代码总是返回ErrorCode。错误的varformat和崩溃的信息丢失。
作为调试的辅助,catch可以更改为:
catch (Exception ex)
{
Console.WriteLine("Got exception {0}'n", ex.ToString());
...
代码应该为WrongVarFormat错误条件定义自己的异常类。catch语句应该只捕获这个异常,地址解析器中的throw语句应该被修改为抛出wrongvarformat - exception。
除非您愿意更改库的代码,否则您只能使用调试器来查找问题的原因。
- 确保您的plc有Get/Put允许(在HW-config下)
- 不能使用优化块访问