C# S7.netPlus 使用类的方式读取PLC。
简单的类型使用类映射读取已经实现比如bool,int,real已经实现,但是现在遇到了DB块里面有Struct,和Array这种类型的数据,适应类映射该怎么读取。如何写?
使用的方法plc.ReadClass(t, db, startByteAdr);
数组类型报错 Received error from PLC: Address out of range.”
结构体类型报错 The size of the class is less than 1 byte and therefore cannot be read 和S7.Net.PlcException:“Received error from PLC: Address out of range.”
结构体DB块
结构体类声明
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PLCClass
{
public class PlcEntity
{
public struct _6P101
{
public bool _local { get; set; }
public bool _Fail { get; set; }
public bool _Start_Cmd { get; set; }
public bool _Stop_Cmd { get; set; }
public bool _Km_Start { get; set; }
public bool _Km_Stop { get; set; }
public bool _I { get; set; }
public ushort _Sec { get; set; }
public ushort _min { get; set; }
public ushort _Hour { get; set; }
public bool _Clean { get; set; }
public bool _Sec_Plus { get; set; }
}
public struct _6P102
{
public bool _local { get; set; }
public bool _Fail { get; set; }
public bool _Start_Cmd { get; set; }
public bool _Stop_Cmd { get; set; }
public bool _Km_Start { get; set; }
public bool _Km_Stop { get; set; }
public bool _I { get; set; }
public ushort _Sec { get; set; }
public ushort _min { get; set; }
public ushort _Hour { get; set; }
public bool _Clean { get; set; }
public bool _Sec_Plus { get; set; }
}
//public _6P101 _6P1017 { get; set; } = new _6P101();
// public _6P102 _6P1028 { get; set; } = new _6P102();
public _6P101 _6P1017 { get; set; }
public _6P102 _6P1028 { get; set; }
// 定义一个静态变量来保存类的实例
private static PlcEntity uniqueInstance;
// 定义一个标识确保线程同步
private static readonly object locker = new object();
// 定义私有构造函数,使外界不能创建该类实例
private PlcEntity()
{
}
/// <summary>2
/// 定义公有方法提供一个全局访问点,同时你也可以定义公有属性来提供全局访问点
/// </summary>
/// <returns></returns>
public static PlcEntity GetInstance()
{
// 当第一个线程运行到这里时,此时会对locker对象 "加锁",
// 当第二个线程运行该方法时,首先检测到locker对象为"加锁"状态,该线程就会挂起等待第一个线程解锁
// lock语句运行完之后(即线程运行完之后)会对该对象"解锁"
if (uniqueInstance == null)
{
lock (locker)
{
// 如果类的实例不存在则创建,否则直接返回
if (uniqueInstance == null)
{
uniqueInstance = new PlcEntity();
}
}
}
return uniqueInstance;
}
}
}
数组DB块
数组类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PLCClass
{
public class PLCDB10Entity
{
public bool[] data1 { get; set; } = new bool[11];
// 定义一个静态变量来保存类的实例
private static PLCDB10Entity uniqueInstance;
// 定义一个标识确保线程同步
private static readonly object locker = new object();
// 定义私有构造函数,使外界不能创建该类实例
private PLCDB10Entity()
{
}
/// <summary>2
/// 定义公有方法提供一个全局访问点,同时你也可以定义公有属性来提供全局访问点
/// </summary>
/// <returns></returns>
public static PLCDB10Entity GetInstance()
{
// 当第一个线程运行到这里时,此时会对locker对象 "加锁",
// 当第二个线程运行该方法时,首先检测到locker对象为"加锁"状态,该线程就会挂起等待第一个线程解锁
// lock语句运行完之后(即线程运行完之后)会对该对象"解锁"
if (uniqueInstance == null)
{
lock (locker)
{
// 如果类的实例不存在则创建,否则直接返回
if (uniqueInstance == null)
{
uniqueInstance = new PLCDB10Entity();
}
}
}
return uniqueInstance;
}
}
}
报错