跳到主要内容

VoE (Vendor over EtherCAT)

VoE 协议用于厂商自定义数据传输,支持 ETG.1000.6 Mailbox 通信规范(Mailbox Type 0x0F)。每个厂商可以定义自己的 VoE 头格式和数据结构。

通过 slave.VoE 访问。从站不支持 VoE 时为 null

属性

属性类型访问说明
DefaultTimeoutMsint读写默认超时时间(毫秒),默认 500
VoEHeaderSizeint只读VoE 头部大小(6 字节:VendorID 4 + VendorType 2)

基本操作

Send(uint vendorId, ushort vendorType, byte[] data, int timeoutMs = DefaultTimeoutMs)

public bool Send(uint vendorId, ushort vendorType, byte[] data, int timeoutMs = DefaultTimeoutMs)

发送 VoE 数据到从站。

参数:

  • vendorId (uint) — 厂商 ID (4字节)
  • vendorType (ushort) — 厂商类型 (2字节)
  • data (byte[]) — 要发送的数据
  • timeoutMs (int) — 超时时间(毫秒)

返回值:

  • bool — 成功返回 true

示例:

slave.VoE.Send(0x00000002, 0x0001, new byte[] { 0x01, 0x02, 0x03 });

Receive(int timeoutMs = DefaultTimeoutMs)

public VoEResponse? Receive(int timeoutMs = DefaultTimeoutMs)

从从站接收 VoE 数据。

返回值:

  • VoEResponse? — VoE 响应对象,失败返回 null

SendAndReceive(uint vendorId, ushort vendorType, byte[] data, int timeoutMs = DefaultTimeoutMs)

public VoEResponse? SendAndReceive(uint vendorId, ushort vendorType, byte[] data, int timeoutMs = DefaultTimeoutMs)

发送 VoE 数据并等待响应。

参数:

  • vendorId (uint) — 厂商 ID (4字节)
  • vendorType (ushort) — 厂商类型 (2字节)
  • data (byte[]) — 要发送的数据
  • timeoutMs (int) — 超时时间(毫秒)

返回值:

  • VoEResponse? — VoE 响应对象,失败返回 null

示例:

var response = slave.VoE.SendAndReceive(0x00000002, 0x0001, new byte[] { 0x10 });
if (response != null)
{
Console.WriteLine($"厂商ID: 0x{response.VendorId:X8}");
Console.WriteLine($"数据: {response.ToHexString()}");
}

VoEResponse

相关结构:

  • VendorId (uint) — 厂商 ID
  • VendorType (ushort) — 厂商类型
  • Data (byte[]) — 响应数据
  • DataLength (int) — 数据长度
  • ToHexString() (string) — 数据格式化为十六进制字符串(如 "01 02 03"
  • ToString() (string) — 完整描述(如 "VoE Response: VendorID=0x00000002, VendorType=0x0001, DataLength=3"

原始帧操作

SendRaw(byte[] frameData, int timeoutMs = DefaultTimeoutMs)

public bool SendRaw(byte[] frameData, int timeoutMs = DefaultTimeoutMs)

发送 VoE 原始帧(用户自行组织帧格式,包括 VoE 头)。

返回值:

  • bool — 成功返回 true

ReceiveRaw(int timeoutMs = DefaultTimeoutMs)

public byte[]? ReceiveRaw(int timeoutMs = DefaultTimeoutMs)

接收 VoE 原始帧。

返回值:

  • byte[]? — 原始帧数据,失败返回 null

SendRawAndReceive(byte[] frameData, int timeoutMs = DefaultTimeoutMs)

public byte[]? SendRawAndReceive(byte[] frameData, int timeoutMs = DefaultTimeoutMs)

发送原始帧并等待响应。

返回值:

  • byte[]? — 响应的原始帧数据,失败返回 null

辅助方法

BuildVoEFrame(uint vendorId, ushort vendorType, byte[]? data)

public byte[] BuildVoEFrame(uint vendorId, ushort vendorType, byte[]? data)

构建标准 VoE 帧(VoE 头 + 数据)。

返回值:

  • byte[] — 构建的 VoE 帧

ParseVoEFrame(byte[]

public VoEResponse? ParseVoEFrame(byte[] frame)

解析 VoE 帧头部。

返回值:

  • VoEResponse? — 解析后的 VoE 响应对象,解析失败返回 null

完整示例

厂商命令交互

if (slave.VoE)
{
var response = slave.VoE.SendAndReceive(
vendorId: 0x00000002,
vendorType: 0x0001,
data: new byte[] { 0x01, 0x00 }
);

if (response != null)
Console.WriteLine(response.ToString());
}

批量数据传输

if (slave.VoE)
{
byte[] payload = new byte[1024];
slave.VoE.Send(0x00000002, 0x1000, payload);

var reply = slave.VoE.Receive();
if (reply != null)
Console.WriteLine($"收到 {reply.DataLength} 字节响应");
}