VoE (Vendor over EtherCAT)
VoE 协议用于厂商自定义数据传输,支持 ETG.1000.6 Mailbox 通信规范(Mailbox Type 0x0F)。每个厂商可以定义自己的 VoE 头格式和数据结构。
通过 slave.VoE() 访问。
属性
| 属性 | 类型 | 读写 | 说明 |
|---|---|---|---|
| DefaultTimeoutMs() / DefaultTimeoutMs(int) | int | 读写 | 默认超时时间(毫秒),默认 500 |
| VOE_HEADER_SIZE | int | 只读 | VoE 头部大小常量(6 字节:VendorID 4 + VendorType 2,static final) |
| 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 实现统一的 IMailboxProtocol 接口。2.5.x 起,getStatistics() / resetStatistics() 经真实导出的 native EcxMbxGetStats / EcxMbxResetStats 拉取(此前为占位空快照)。
| 类别 | 方法 | 类型 | 读写 | 说明 |
|---|---|---|---|---|
| 协议标识 | getProtocolType() | byte | 只读 | 协议类型常量(VoE=0x0F) |
| getProtocolName() | String | 只读 | 协议名称("VoE") | |
| isSupported() | boolean | 只读 | 从站是否支持本协议(等同 IsSupported()) | |
| 事务状态 | getLastStatus() | MailboxStatus | 只读 | 最近一次邮箱事务的统一状态 |
| getLastErrorCode() | long | 只读 | 最近一次协议层错误码(0=无错误) | |
| 统计 | getStatistics() | MailboxStatistics | 只读 | 邮箱统计快照 |
| resetStatistics() | void | — | 重置邮箱统计计数器 |
MailboxStatistics 结构字段与 MailboxStatus 枚举详见 CoE — 邮箱统计。
完整示例
厂商命令交互
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() + " 字节响应");
}