VoE (Vendor over EtherCAT)
VoE 协议用于厂商自定义数据传输,支持 ETG.1000.6 Mailbox 通信规范(Mailbox Type 0x0F)。每个厂商可以定义自己的 VoE 头格式和数据结构。
通过 slave.VoE() 访问。
属性
| 属性 | 类型 | 读写 | 说明 |
|---|---|---|---|
| DefaultTimeoutMs() / DefaultTimeoutMs(int) | int | 读写 | 默认超时时间(毫秒),默认 500 |
| VoEHeaderSize | int | 只读 | VoE 头部大小(6 字节:VendorID 4 + VendorType 2) |
| IsSupported() | boolean | 只读 | 从站是否支持 VoE 邮箱协议 |
基本操作
Send(int vendorId, short vendorType, byte[] data, int timeoutMs)
public boolean Send(int vendorId, short vendorType, byte[] data, int timeoutMs)
public boolean Send(int vendorId, short vendorType, byte[] data)
发送 VoE 数据到从站。
参数:
vendorId(int) — 厂商 ID (4字节)vendorType(short) — 厂商类型 (2字节)data(byte[]) — 要发送的数据timeoutMs(int) — 超时时间(毫秒,可选)
返回值:
boolean— 成功返回true
示例:
slave.VoE().Send(0x00000002, (short) 0x0001, new byte[] { 0x01, 0x02, 0x03 });
Receive(int timeoutMs)
public VoEResponse Receive(int timeoutMs)
public VoEResponse Receive()
从从站接收 VoE 数据。
参数:
timeoutMs(int) — 超时时间(毫秒,可选)
返回值:
VoEResponse— VoE 响应对象,失败返回null
SendAndReceive(int vendorId, short vendorType, byte[] data, int timeoutMs)
public VoEResponse SendAndReceive(int vendorId, short vendorType, byte[] data, int timeoutMs)
public VoEResponse SendAndReceive(int vendorId, short vendorType, byte[] data)
发送 VoE 数据并等待响应。
参数:
vendorId(int) — 厂商 ID (4字节)vendorType(short) — 厂商类型 (2字节)data(byte[]) — 要发送的数据timeoutMs(int) — 超时时间(毫秒,可选)
返回值:
VoEResponse— VoE 响应对象,失败返回null
示例:
VoE.VoEResponse response = slave.VoE().SendAndReceive(0x00000002, (short) 0x0001, new byte[] { 0x10 });
if (response != null) {
System.out.printf("厂商ID: 0x%08X%n", response.VendorId);
System.out.println("数据: " + response.ToHexString());
}
相关结构:
public static class VoEResponse {
public int VendorId; // 厂商 ID
public short VendorType; // 厂商类型
public byte[] Data; // 响应数据
public int DataLength(); // 数据长度
public String ToHexString(); // 格式化为十六进制(如 "01 02 03")
public String toString(); // 完整描述
}
原始帧操作
SendRaw(byte[] frameData, int timeoutMs)
public boolean SendRaw(byte[] frameData, int timeoutMs)
public boolean SendRaw(byte[] frameData)
发送 VoE 原始帧(用户自行组织帧格式,包括 VoE 头)。
参数:
frameData(byte[]) — 原始帧数据timeoutMs(int) — 超时时间(毫秒,可选)
返回值:
boolean— 成功返回true
ReceiveRaw(int timeoutMs)
public byte[] ReceiveRaw(int timeoutMs)
public byte[] ReceiveRaw()
接收 VoE 原始帧。
参数:
timeoutMs(int) — 超时时间(毫秒,可选)
返回值:
byte[]— 原始帧数据,失败返回null
SendRawAndReceive(byte[] frameData, int timeoutMs)
public byte[] SendRawAndReceive(byte[] frameData, int timeoutMs)
public byte[] SendRawAndReceive(byte[] frameData)
发送原始帧并等待响应。
参数:
frameData(byte[]) — 原始帧数据timeoutMs(int) — 超时时间(毫秒,可选)
返回值:
byte[]— 响应的原始帧数据,失败返回null
辅助方法
BuildVoEFrame(int vendorId, short vendorType, byte[] data)
public byte[] BuildVoEFrame(int vendorId, short vendorType, byte[] data)
构建标准 VoE 帧(VoE 头 + 数据)。
参数:
vendorId(int) — 厂商 IDvendorType(short) — 厂商类型data(byte[]) — 数据(可为null)
返回值:
byte[]— 构建的 VoE 帧
ParseVoEFrame(byte[] frame)
public VoEResponse ParseVoEFrame(byte[] frame)
解析 VoE 帧头部。
参数:
frame(byte[]) — VoE 帧数据
返回值:
VoEResponse— 解析后的 VoE 响应对象,解析失败返回null
Vendor 异步通知
VoE 还支持从站主动推送的异步通知:通过 voe.addNotificationListener(...) 注册回调,再调用 voe.StartNotificationListener() 启动监听线程;停止时调用 voe.StopNotificationListener()。回调参数 VoENotificationEventArgs 包含 slaveIndex / vendorId / vendorType / data / timestampUnixMs。
完整示例
厂商命令交互
VoE voe = slave.VoE();
if (voe.IsSupported()) {
VoE.VoEResponse response = voe.SendAndReceive(
0x00000002, (short) 0x0001,
new byte[] { 0x01, 0x00 }
);
if (response != null)
System.out.println(response.toString());
}
批量数据传输
VoE voe = slave.VoE();
if (voe.IsSupported()) {
byte[] payload = new byte[1024];
voe.Send(0x00000002, (short) 0x1000, payload);
VoE.VoEResponse reply = voe.Receive();
if (reply != null)
System.out.println("收到 " + reply.DataLength() + " 字节响应");
}