点击或拖拽改变大小

SiemensS7NetRead 方法 (String, UInt16)

一次性从PLC获取所有的数据,按照先后顺序返回一个统一的Buffer,需要按照顺序处理,两个数组长度必须一致,数组长度无限制
One-time from the PLC to obtain all the data, in order to return a unified buffer, need to be processed sequentially, two array length must be consistent

命名空间:  HslCommunication.Profinet.Siemens
程序集:  HslCommunication (在 HslCommunication.dll 中) 版本:11.8.2.0 (11.8.2.0)
语法
public OperateResult<byte[]> Read(
	string[] address,
	ushort[] length
)

参数

address
类型:SystemString
起始地址,格式为I100,M100,Q100,DB20.100
Starting address, formatted as I100,M100,Q100,DB20.100
length
类型:SystemUInt16
数据长度数组
Array of data Lengths

返回值

类型:OperateResultByte
是否读取成功的结果对象 -> Whether to read the successful result object
异常
异常条件
NullReferenceException
备注
警告 警告:
原先的批量的长度为19,现在已经内部自动处理整合,目前的长度为任意和长度。
示例
以下是一个高级的读取示例
Read示例
// 实例化
SiemensS7Net siemens = new SiemensS7Net( SiemensPLCS.S1200, " 192.168.1.110" );
siemens.SetPersistentConnection( ); // 设置长连接

// 假设我要的数据在 M,I,Q三个数据块,实际上是任意的数据库信息
// 下面的是什么意思,我一条指定读取了M100-M119,I0-I9,Q0-Q14共计45个字节的数据内容
// 怎么样提取呢?
OperateResult<byte[]> read = siemens.Read( new string[] { "M100", "I0", "Q0" }, new ushort[] { 20, 10, 15 } );
// 使用下面的读取代码也是一样的效果,方便做个对比
//OperateResult<byte[]> read = siemens.Read(
//    new HslCommunication.Core.Address.S7AddressData[]
//    {
//        HslCommunication.Core.Address.S7AddressData.ParseFrom("M100", 20).Content,
//        HslCommunication.Core.Address.S7AddressData.ParseFrom("I0", 10).Content,
//        HslCommunication.Core.Address.S7AddressData.ParseFrom("Q0", 15).Content,
//    } );

if (read.IsSuccess)
{
    // 示例提取几个M的数据,实际的代码取决于PLC的定义
    float temp = siemens.ByteTransform.TransInt16( read.Content, 0 ) / 10f;
    float press = siemens.ByteTransform.TransInt16( read.Content, 2 ) / 100f;
    int count = siemens.ByteTransform.TransInt32( read.Content, 2 );

    // 下面演示提取 I1.2 的通断情况
    bool i_1_2 = (read.Content[21] & 0x04) == 0x04;

    // 下面再演示提取 Q0.0 的通断情况
    bool q_0_0 = (read.Content[30] & 0x01) == 0x01;

    // 下面演示批量读取bool数据,从I0.0~I9.7
    bool[] inputArray = HslCommunication.BasicFramework.SoftBasic.ByteToBoolArray( siemens.ByteTransform.TransByte( read.Content, 20, 10 ) );
    // inputArray[0] = I0.0
    // inputArray[1] = I0.1
    // inputArray[2] = I0.2
    // inputArray[3] = I0.3
    // inputArray[4] = I0.4
    // inputArray[5] = I0.5
    // inputArray[6] = I0.6
    // inputArray[7] = I0.7
    // inputArray[8] = I1.0
    // inputArray[9] = I1.1
    // 以此类推

    // do something
}
else
{
    // failed
}

siemens.ConnectClose( );  // 关闭连接
参见