hslcommunication CIP 通讯 欧姆龙 PLC

nuget 安装 hslcommunication 

 

Install-Package HslCommunication -Version 12.3.2

 

 

客户端 连接cip 服务器

HslCommunication.Profinet.Omron.OmronCipNet plc = new HslCommunication.Profinet.Omron.OmronCipNet( );
plc.Slot = 0;
plc.CommunicationPipe = new HslCommunication.Core.Pipe.PipeTcpNet("127.0.0.1", 44818)
{
    ConnectTimeOut = 5000,    // 连接超时时间,单位毫秒
    ReceiveTimeOut = 10000,    // 接收设备数据反馈的超时时间
};

写入float

OperateResult write = plc.Write( "A1", float.Parse( "112.333" ) );
if (write.IsSuccess)
{
    Console.WriteLine( "Write [A1] success" );
}
else
{    Console.WriteLine( "Write [A1] failed: " + write.Message );
}

 

 

读取float

OperateResult<float> read = plc.ReadFloat( "A1" );
if (read.IsSuccess)
{
    Console.WriteLine( "Read [A1] Success, Value: " + read.Content );
}
else
{
    Console.WriteLine( "Read [A1] failed: " + read.Message );
}

 

写入int

OperateResult write = plc.Write( "B", int.Parse( "123" ) );
if (write.IsSuccess)
{
    Console.WriteLine( "Write [B] success" );
}
else
{    Console.WriteLine( "Write [B] failed: " + write.Message );
}

读取int

OperateResult<int> read = plc.ReadInt32( "B" );
if (read.IsSuccess)
{
    Console.WriteLine( "Read [B] Success, Value: " + read.Content );
}
else
{
    Console.WriteLine( "Read [B] failed: " + read.Message );
}

读取数组中指定元素

OperateResult<float> read = plc.ReadFloat( "A1[3]" );
if (read.IsSuccess)
{
    Console.WriteLine( "Read [A1[3]] Success, Value: " + read.Content );
}
else
{
    Console.WriteLine( "Read [A1[3]] failed: " + read.Message );
}

写入字符串

OperateResult<string> read = plc.ReadString( "G[1]", 1, Encoding.ASCII );
if (read.IsSuccess)
{
    Console.WriteLine( "Read [G[1]] Success, Value: " + read.Content );
}
else
{
    Console.WriteLine( "Read [G[1]] failed: " + read.Message );
}

 

 

读取字符串

OperateResult<string> read = plc.ReadString( "G[1]", 1, Encoding.ASCII );
if (read.IsSuccess)
{
    Console.WriteLine( "Read [G[1]] Success, Value: " + read.Content );
}
else
{
    Console.WriteLine( "Read [G[1]] failed: " + read.Message );
}

 只能读取单个的简单类型的数据,结构体就无法读取。

 免费版本可以单次可以连续测试使用24小时、超过时间限制以后重启软件或者自行购买官方授权key

posted @ 2025-06-11 09:20  JohnnyLei  阅读(787)  评论(0)    收藏  举报