点击或拖拽改变大小

MelsecFxSerialRead 方法 (String, UInt16)

根据指定的地址及长度信息从三菱PLC中读取原始的字节数据,根据PLC中实际定义的规则,可以解析出任何类的数据信息
Read the original byte data from the Mitsubishi PLC according to the specified address and length information. According to the rules actually defined in the PLC, any type of data information can be parsed

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

参数

address
类型:SystemString
读取地址,,支持的类型参考文档说明
length
类型:SystemUInt16
读取的数据长度

返回值

类型:OperateResultByte
带成功标志的结果数据对象

实现

IReadWriteNetRead(String, UInt16)
IReadWriteNetRead(String, UInt16)
示例
假设起始地址为D100,D100存储了温度,100.6℃值为1006,D101存储了压力,1.23Mpa值为123,D102,D103存储了产量计数,读取如下:
Read示例
MelsecFxSerial melsecFx = new MelsecFxSerial( );
melsecFx.SerialPortInni( sp =>
{
    sp.PortName = "COM1";
    sp.BaudRate = 9600;
    sp.DataBits = 7;
    sp.StopBits = System.IO.Ports.StopBits.One;
    sp.Parity = System.IO.Ports.Parity.Even;
} );
melsecFx.Open( );

OperateResult<byte[]> read = melsecFx.Read( "D100", 4 );
if (read.IsSuccess)
{
    float temp = melsecFx.ByteTransform.TransInt16( read.Content, 0 ) / 10f;
    float press = melsecFx.ByteTransform.TransInt16( read.Content, 2 ) / 100f;
    int count = melsecFx.ByteTransform.TransInt32( read.Content, 2 );

    // do something
}
else
{
    // failed
}
以下是读取不同类型数据的示例
Read示例
MelsecFxSerial melsecFx = new MelsecFxSerial( );
melsecFx.SerialPortInni( sp =>
{
    sp.PortName = "COM1";
    sp.BaudRate = 9600;
    sp.DataBits = 7;
    sp.StopBits = System.IO.Ports.StopBits.One;
    sp.Parity = System.IO.Ports.Parity.Even;
} );
melsecFx.Open( );

// 此处以D寄存器作为示例
short short_D1000 = melsecFx.ReadInt16( "D100" ).Content;         // 读取D1000的short值 
ushort ushort_D1000 = melsecFx.ReadUInt16( "D100" ).Content;      // 读取D1000的ushort值
int int_D1000 = melsecFx.ReadInt32( "D100" ).Content;             // 读取D1000-D1001组成的int数据
uint uint_D1000 = melsecFx.ReadUInt32( "D100" ).Content;          // 读取D1000-D1001组成的uint数据
float float_D1000 = melsecFx.ReadFloat( "D100" ).Content;         // 读取D1000-D1001组成的float数据
long long_D1000 = melsecFx.ReadInt64( "D100" ).Content;           // 读取D1000-D1003组成的long数据
ulong ulong_D1000 = melsecFx.ReadUInt64( "D100" ).Content;        // 读取D1000-D1003组成的long数据
double double_D1000 = melsecFx.ReadDouble( "D100" ).Content;      // 读取D1000-D1003组成的double数据
string str_D1000 = melsecFx.ReadString( "D100", 10 ).Content;     // 读取D1000-D1009组成的条码数据

// 读取数组
short[] short_D1000_array = melsecFx.ReadInt16( "D100", 10 ).Content;         // 读取D1000的short值 
ushort[] ushort_D1000_array = melsecFx.ReadUInt16( "D100", 10 ).Content;      // 读取D1000的ushort值
int[] int_D1000_array = melsecFx.ReadInt32( "D100", 10 ).Content;             // 读取D1000-D1001组成的int数据
uint[] uint_D1000_array = melsecFx.ReadUInt32( "D100", 10 ).Content;          // 读取D1000-D1001组成的uint数据
float[] float_D1000_array = melsecFx.ReadFloat( "D100", 10 ).Content;         // 读取D1000-D1001组成的float数据
long[] long_D1000_array = melsecFx.ReadInt64( "D100", 10 ).Content;           // 读取D1000-D1003组成的long数据
ulong[] ulong_D1000_array = melsecFx.ReadUInt64( "D100", 10 ).Content;        // 读取D1000-D1003组成的long数据
double[] double_D1000_array = melsecFx.ReadDouble( "D100", 10 ).Content;      // 读取D1000-D1003组成的double数据
参见