Keyence 协议驱动
包: Snet.Keyence | 类: KeyenceOperate | 类型: 5
提供通过以太网上的 Keyence 专用协议连接基恩士 PLC(KV 系列)的能力。
安装
dotnet add package Snet.Keyence
快速开始
using Snet.Keyence;
var op = new KeyenceOperate(new KeyenceData.Basics
{
IpAddress = "192.168.1.100",
Port = 8501
});
var address = new Address(new List<AddressDetails>
{
new("读数", "DM100", DataType.Int16),
new("读数2", "DM102", DataType.Int16)
});
var result = await op.ReadAsync(address);
if (result.Status)
{
var data = result.GetSource<ConcurrentDictionary<string, AddressValue>>();
foreach (var kv in data)
Console.WriteLine($"{kv.Key} = {kv.Value.ResultValue}");
}
else
{
Console.WriteLine($"读取失败: {result.Message}");
}
// 订阅之前 绑定数据事件
op.OnDataEventAsync += async (sender, e) =>
{
if (e.Status)
{
var data = e.GetSource<ConcurrentDictionary<string, AddressValue>>();
foreach (var kv in data)
Console.WriteLine($"{kv.Key} = {kv.Value.ResultValue}");
}
else
{
Console.WriteLine($"{e.Message}");
}
};
var subAddr = new Address(new List<AddressDetails>
{
new("读数", "DM100", DataType.Int16)
});
await op.SubscribeAsync(subAddr);
// 后续:取消订阅
// await op.UnSubscribeAsync(subAddr);
await op.DisposeAsync();
配置
| 参数 |
类型 |
默认值 |
描述 |
IpAddress |
string |
— |
PLC IP 地址 |
Port |
int |
8501 |
通信端口 |
ConnectTimeOut |
int |
1000 |
连接超时(毫秒) |
ReceiveTimeOut |
int |
1000 |
接收超时(毫秒) |
支持的协议类型
| 类型 |
描述 |
KeyenceMcNet |
MC 协议二进制通过以太网 TCP(KV 系列) |
KeyenceMcAsciiNet |
MC 协议 ASCII 通过以太网 TCP |
KeyenceNanoSerial |
Nano 串行协议通过 RS-232/485 |
KeyenceNanoSerialOverTcp |
通过 TCP 桥接的 Nano 串行协议 |
KeyenceKvOld |
KV 旧版协议 |
支持的操作
| 操作 |
方法 |
| 连接 |
OnAsync() |
| 断开 |
OffAsync() |
| 读取 |
ReadAsync(address) |
| 写入 |
WriteAsync(values) |
| 订阅 |
SubscribeAsync(address) |
参见