VoE(Vendor over EtherCAT)
VoE プロトコルは、デバイスベンダが EtherCAT メールボックスを通じて独自のベンダ固有データを交換できるようにする。標準外のベンダ機能向け(Mailbox Type 0x0F)。
公開の VoE インタフェースは、通知購読の組 voe_register_notification / voe_unregister_notification とリスナスレッド制御である(ハンドルなし、関数ポインタテーブルなし)。
能力判定
三値の ec_slave_get_proto_support_state(master, slave, MBXPROT_VOE) で照会する(EC_PROTO_SUPPORT_YES/NO/UNKNOWN を返す)。VoE は SII General に専用の詳細フィールドを持たないため、メールボックス能力ビットが 0 のとき返せる回答は UNKNOWN だけである。「そのデバイスは VoE をサポートしない」と報告してはいけない。
送受信と通知
VoE はデバイスベンダが独自のベンダ固有データを交換できるようにする。ベンダからのプッシュは後述の非同期通知で受け取り、同期の送受信エントリポイントはパッケージに同梱される公開ヘッダで定義される。
非同期通知(slave/voe.h)
VoE には CoE のような標準イベント機構がないが、ベンダはしばしば「スレーブのプッシュ」→「アプリケーションのコールバック」という意味が必要になる。slave/voe.h はこれをバックグラウンドのリスナスレッドで提供する。アプリケーションは購読を登録するだけで、データが届くと通知される。
リスナスレッドと購読はマスタ単位に区切られる。マスタごとに独自のスレッドと独自の購読表を持つ。マスタ A 上のスレーブ N からの VoE プッシュは、A に登録されたコールバックにだけ届き、マスタ B 上の同じスレーブ番号へ漏れることはない。
voe_notification_cb_t
typedef void (*voe_notification_cb_t)(uint16_t master_index,
uint16_t slave,
uint32_t vendor_id,
uint16_t vendor_type,
const uint8_t* data,
uint32_t data_size,
void* user_data);
コールバックのパラメータ:
master_index(uint16_t) — 通知の発生元マスタ(マルチマスタの経路選択キー)slave(uint16_t) — スレーブインデックスvendor_id(uint32_t) — ベンダ IDvendor_type(uint16_t) — ベンダ定義の型data(const uint8_t*) — 受信した VoE データ(コールバック中だけ有効)data_size(uint32_t) — データサイズuser_data(void*) — 登録時に渡したコンテキストポインタ
voe_start_notification_listener()
int voe_start_notification_listener(uint16_t master_index);
指定マスタの VoE 通知リスナスレッドを開始する。成功は 1、ランタイム未準備または失敗は 0。
voe_register_notification()
int voe_register_notification(uint16_t master_index, uint16_t slave,
uint32_t vendor_id, uint16_t vendor_type,
voe_notification_cb_t cb, void* user_data);
VoE 通知の購読を登録する(vendor_id / vendor_type で絞り込む)。購読は master_index が示すマスタに属し、マスタごとに独立である。購読インデックス >= 0 を返し、負値は失敗。
voe_unregister_notification()
int voe_unregister_notification(uint16_t master_index, int subscription_index);
インデックス指定で購読を解除する。このマスタ(master_index)の購読だけを取り外せる。
voe_stop_notification_listener()
int voe_stop_notification_listener(uint16_t master_index);
指定マスタのリスナスレッドを停止する(他のマスタには影響しない)。
voe_is_notification_listening()
int voe_is_notification_listening(uint16_t master_index);
指定マスタのリスナスレッドが動いているかを照会する(1 = 実行中)。
例:
#include "slave/voe.h"
static void on_voe_data(uint16_t mi, uint16_t si, uint32_t vid, uint16_t vtype,
const uint8_t* data, uint32_t size, void* user)
{
printf("VoE 推送: master=%u, slave=%u, VendorID=0x%08X, Type=0x%04X, %u 字节\n", /* VoE プッシュ: master=%u、slave=%u、VendorID=0x%08X、Type=0x%04X、%u バイト */
mi, si, vid, vtype, size);
/* data はコールバック中のみ有効。保持する必要がある場合はすぐにコピーする */
}
int main(void)
{
uint16_t master = Initialize();
/* ... 初期化は省略 ... */
/* このマスタのリスナスレッドを開始する */
voe_start_notification_listener(master);
/* このマスタでスレーブ 1 / 2 の購読を登録する */
int sub1 = voe_register_notification(master, 1, 0x12345678, 0x0001, on_voe_data, NULL);
int sub2 = voe_register_notification(master, 2, 0x12345678, 0x0001, on_voe_data, NULL);
/* ... 通常処理ループ ... */
/* 停止してリソースを解放する */
voe_unregister_notification(master, sub1);
voe_unregister_notification(master, sub2);
voe_stop_notification_listener(master);
Dispose(master);
return 0;
}
コールバックはアドバンス層内部のリスナスレッド上で動き、メインプログラムとは非同期である。共有データはミューテックスで保護する。コールバックからブロッキングする送受信インタフェースを呼んではいけない。リスナ自身とメールボックスを取り合ってしまう。
完全な例
#include "ethercat.h"
#include "slave/voe.h"
#include <stdio.h>
static void on_voe_data(uint16_t mi, uint16_t si, uint32_t vid, uint16_t vtype,
const uint8_t* data, uint32_t size, void* user)
{
printf("VoE 推送: master=%u, slave=%u, VendorID=0x%08X, Type=0x%04X, %u 字节\n", /* VoE プッシュ: master=%u、slave=%u、VendorID=0x%08X、Type=0x%04X、%u バイト */
mi, si, vid, vtype, size);
}
int main(void)
{
uint16_t master = Initialize();
SetNetwork(master, "\\Device\\NPF_{...}", "");
SetStateSequence(master, EC_STATE_PRE_OP, 5000);
/* リスナを開始して購読を登録する */
voe_start_notification_listener(master);
int sub = voe_register_notification(master, 1, 0x12345678, 0x0001,
on_voe_data, NULL);
/* ... 通常処理ループ ... */
voe_unregister_notification(master, sub);
voe_stop_notification_listener(master);
Dispose(master);
return 0;
}
VoE の dx_* インタフェース
slave/voe.h の 5 つの dx_voe_* 関数は、VoE の同期送受信と能力照会のエントリポイントである。send / receive は vendor_id + vendor_type を取り、send_raw / receive_raw はフレーム全体を直接やり取りする。
| 分類 | 関数 | 戻り値の型 | 読み取り/書き込み | 説明 |
|---|---|---|---|---|
| 送信 | dx_voe_send | BOOL | 書き込み専用 | vendor_id + vendor_type で VoE 要求を送信する。ペイロードは data / data_size |
| dx_voe_send_raw | BOOL | 書き込み専用 | フレーム全体 frame_data / frame_size を直接送信する。vendor_id / vendor_type のパラメータを持たない | |
| 受信 | dx_voe_receive | BOOL | 読み取り専用 | 出力パラメータ vendor_id / vendor_type つきで VoE 応答を受け取る。*data はランタイムが確保し、長さは *data_size に入る。FreeMemory() で解放しなければならない |
| dx_voe_receive_raw | BOOL | 読み取り専用 | フレーム全体を *frame_data / *frame_size で受け取る。これもランタイムが確保し、FreeMemory() で解放する | |
| 能力 | dx_voe_is_supported | BOOL | 読み取り専用 | このスレーブが VoE をサポートするか |
送受信の違いと能力判定
- パラメータ指定とフレーム全体:
dx_voe_sendはベンダ三つ組(vendor_id/vendor_typeとペイロード)を取り、dx_voe_send_rawは完全な VoE フレームを取る。受信側でもdx_voe_receiveはvendor_id/vendor_typeをデータから分離し、dx_voe_receive_rawは生のフレームバイトを返す。自分でフレームを組み立てる/解析するときは_rawを、ベンダデータだけが関心ならパラメータ指定のエントリポイントを用いる。 - 受信バッファはランタイムの所有:
dx_voe_receiveの*dataもdx_voe_receive_rawの*frame_dataも、どちらもランタイムが確保し、長さは*data_size/*frame_sizeに入る。FreeMemory()で解放する。 dx_voe_is_supportedは真偽判定: ヘッダが述べるとおり、真偽値のエントリポイントは「照会失敗/読めない」を0として報告し、「本当に非サポート」と同じ形になる。「不明」と「非サポート」を区別したいときは、三値照会dx_voe_query_support(VOE_SUPPORT_UNKNOWN/VOE_SUPPORT_NO/VOE_SUPPORT_YES)を用いる。
例
#include "ethercat.h"
#include <stdio.h>
int main(void)
{
uint16_t master = Initialize();
SetNetwork(master, "\\Device\\NPF_{...}", "");
SetStateSequence(master, EC_STATE_PRE_OP, 5000);
/* 真偽値の口は「サポートされているか」にしか答えず、読めなくても 0 を返す*/
if (!dx_voe_is_supported(master, 1)) {
printf("不支持 VoE, 或能力读取失败\n"); /* VoE 未対応、または能力の読み取りに失敗しました */
Dispose(master);
return 0;
}
/* パラメータ指定の送信 + 受信*/
uint8_t req[4] = {0x01, 0x02, 0x03, 0x04};
if (dx_voe_send(master, 1, 0x12345678, 0x0001, req, sizeof(req), 5000)) {
uint32_t vendor_id = 0;
uint16_t vendor_type = 0;
uint8_t* rsp = NULL;
int rsp_size = 0;
if (dx_voe_receive(master, 1, &vendor_id, &vendor_type, &rsp, &rsp_size, 5000)) {
printf("vendor=0x%08X type=0x%04X, %d 字节\n", vendor_id, vendor_type, rsp_size); /* vendor=0x%08X type=0x%04X、%d バイト */
FreeMemory(rsp); /* 受信バッファは実行時ライブラリが確保する*/
}
}
/* フレーム全体の送受信*/
uint8_t raw[8] = {0};
dx_voe_send_raw(master, 1, raw, sizeof(raw), 5000);
uint8_t* rsp_raw = NULL;
int rsp_raw_size = 0;
if (dx_voe_receive_raw(master, 1, &rsp_raw, &rsp_raw_size, 5000)) {
FreeMemory(rsp_raw);
}
Dispose(master);
return 0;
}