点击或拖拽改变大小

MelsecMcNetReadRandomAsync 方法 (String, UInt16)

使用块读取PLC的数据信息,可以跨地址,跨类型组合,每个地址是任意的长度。收到结果后,需要自行解析数据,目前只支持字地址,比如D区,W区,R区,不支持X,Y,M,B,L等等
Read the data information of the PLC randomly. It can be combined across addresses and types. Each address is of any length. After receiving the results, you need to parse the data yourself. Currently, only word addresses are supported, such as D area, W area, R area. X, Y, M, B, L, etc

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

参数

address
类型:SystemString
所有的地址的集合
length
类型:SystemUInt16
每个地址的长度信息

返回值

类型:TaskOperateResultByte
结果
备注
实际测试不一定所有的plc都可以读取成功,具体情况需要具体分析
1 块数按照下列要求指定 120 ≧ 字软元件块数 + 位软元件块数
2 各软元件点数按照下列要求指定 960 ≧ 字软元件各块的合计点数 + 位软元件各块的合计点数
示例
随机批量字读取示例
// 随机读取的示例,所谓的随机读取,就是跨地址读取数据

// 下面我们举个例子,我们读取D100-D104的值,D200-D209的值,M32-M47的值,W100-W104的值,如何一次就读取出来呢?
MelsecMcNet melsec_net = new MelsecMcNet( "192.168.0.100", 6000 );

OperateResult<byte[]> read = melsec_net.ReadRandom( new string[] { "D100", "D200", "M32", "W100" }, new ushort[] { 5, 10, 1, 5 } );
if (read.IsSuccess)
{
    short d100 = melsec_net.ByteTransform.TransInt16( read.Content, 0 );
    short d101 = melsec_net.ByteTransform.TransInt16( read.Content, 2 );
    short d102 = melsec_net.ByteTransform.TransInt16( read.Content, 4 );
    short d103 = melsec_net.ByteTransform.TransInt16( read.Content, 6 );
    short d104 = melsec_net.ByteTransform.TransInt16( read.Content, 8 );
    short d200 = melsec_net.ByteTransform.TransInt16( read.Content, 10 );
    short d201 = melsec_net.ByteTransform.TransInt16( read.Content, 12 );
    // 等等
    short w100 = melsec_net.ByteTransform.TransInt16( read.Content, 32 );
    short w101 = melsec_net.ByteTransform.TransInt16( read.Content, 34 );
    short w102 = melsec_net.ByteTransform.TransInt16( read.Content, 36 );
    short w103 = melsec_net.ByteTransform.TransInt16( read.Content, 38 );
    short w104 = melsec_net.ByteTransform.TransInt16( read.Content, 40 );

    // M是位地址,提取稍微麻烦一点点
    bool[] array = HslCommunication.BasicFramework.SoftBasic.ByteToBoolArray( melsec_net.ByteTransform.TransByte( read.Content, 30, 2 ) );
    bool m32 = array[0];
    bool m33 = array[1];
    bool m34 = array[2];
    bool m35 = array[3];
    // 等等,按照规律操作就可以

    // do something
}
else
{
    // failed
}
参见