Two-Wire Interface#
たとえば、カメラを使用してTWI互換レンズのフォーカスを制御できます。
機能を使用する#
仕組み#
TWIバスは、1本のシリアルデータライン (SDA) と1本のシリアルクロックライン (SCL) で構成される2線式バスです。これらは双方向ラインです。
カメラ(マスター)と外部デバイス(スレーブ)は、データラインとクロックラインの両方に接続されます。データは、データラインを介してカメラによって送信、または要求および取得されます。クロックラインはクロック信号の送信に使用され、カメラによって制御されます。
上述のTWIインターフェースを利用するには、カメラのGenICamパラメータがスレーブデバイスの仕様に準拠している必要があります。
適切な通信プロトコルの実装に役立つカメラパラメータについては、以下を参照してください。
TWI通信の設定#
カメラと外部デバイス間のTWI通信を設定するには:
- パラメーターを
BslTwiBitrateパラメータを、TWIで使用するビットレート(例:50 kbps)に設定します。 - カメラの入出力信号を設定します。
- パラメーターを
LineSelectorパラメータを、シリアルデータの書き込みおよび読み取りに使用するI/Oラインに設定します(例:Line2)。ラインは次のように設定する必要があります: 入出力ライン. - パラメーターを
BslLineConnectionパラメータをTwiSda. - パラメーターを
LineSelectorパラメータを、クロック信号の送信に使用するI/Oラインに設定します(例:Line3)。ラインは入出力ラインとして設定する必要があります。 - パラメーターを
BslLineConnectionパラメータをTwiScl.
- パラメーターを
データの書き込みと読み取り#
TWI通信を設定した後、外部デバイスへのデータの書き込みと外部デバイスからのデータの読み取りを行うことができます。
パラメータの書き込みと読み取り#
データの書き込みと読み取りの両方において、以下のパラメータが関係します:
BslTwiTransferBuffer: 転送バッファには、書き込まれるデータが保持され、読み取られたデータも格納されます。最大16バイトを保持できます。BslTwiTransferLength: 転送バッファとの間で送受信する必要があるバイト数を示します。書き込み時、転送長はカメラが1回のトランザクションで送信する必要があるバイト数を指定します。読み取り時、前回のトランザクション中にカメラが受信したバイト数を指定します。最大値は16です。BslTwiTargetAddress: 外部スレーブデバイスの7ビットのターゲットアドレスを示します。
データの書き込みには、次のパラメータが関連しています:
BslTwiWrite:このコマンドを実行すると、転送バッファから転送長で指定されたバイト数のデータがスレーブデバイスのターゲットアドレスに書き込まれます。
データの読み出しには、次のパラメータが関連しています:
BslTwiRead:このコマンドを実行すると、スレーブデバイスのターゲットアドレスからデータが読み出(取得)されます。読み出されるバイト数は転送長によって指定されます。
転送ステータス#
転送ステータスは、データ伝送の現在のステータスを示します。伝送が正常に完了すると、転送ステータスは Successを示します。現在の転送ステータスを取得するには、 BslTwiUpdateTransferStatus コマンドを実行して転送ステータスを更新し、 BslTwiTransferStatus.
の値の場合 BslTwiTransferStatus です。 Pendingの値を取得します。転送ステータスが Successを示すようになるまで、転送ステータスの更新と読み取りを繰り返し行う必要があります。新しいデータの書き込みまたは読み取りは、転送ステータスが Success.
を示している場合にのみ実行できます。
Success:直前のデータ伝送が成功しました。Pending:データ伝送が保留中です。NAKAddress:直前の書き込みまたは読み出しコマンドの実行後、カメラがターゲットアドレスを確認するアクノリッジビットを受信しませんでした。この場合、データ伝送は失敗します。NAKData:直前の書き込みコマンドの実行後、カメラがデータ伝送の成功を確認するアクノリッジビットを受信しませんでした。この場合、データ伝送は失敗します。
I/OラインのLowレベルへのプル#
TWIスレーブデバイスによっては、動作を開始する前にI/OラインをLowにする必要があるものがあります。I/OラインをLowにする必要がある場合は、 BslTwiPullSdaLow パラメータまたは BslTwiPullSclLow パラメータ(あるいはその両方)を trueに設定します。いずれか一方が trueの間は、データ伝送が無効になります。
サンプルコード#
// Configure TWI communication
// Set the bit rate to 50 kbps
camera.BslTwiBitrate.SetValue(BslTwiBitrate_Bitrate50kbps);
// Set line 2 to input-output and use it for TWI SDA
camera.LineSelector.SetValue(LineSelector_Line2);
camera.LineMode.SetValue(LineMode_InOut);
camera.BslLineConnection.SetValue(BslLineConnection_TwiSda);
// Set line 3 to input-output and use it for TWI SCL
camera.LineSelector.SetValue(LineSelector_Line3);
camera.LineMode.SetValue(LineMode_InOut);
camera.BslLineConnection.SetValue(BslLineConnection_TwiScl);
// Now, you must implement a suitable communication protocol.
// Camera parameters that help you implement the protocol are listed
// in the "Writing and Reading and Data" section above.
INodeMap& nodemap = camera.GetNodeMap();
// Configure TWI communication
// Set the bit rate to 50 kbps
CEnumParameter(nodemap, "BslTwiBitrate").SetValue("Bitrate50kbps");
// Set line 2 to input-output and use it for TWI SDA
CEnumParameter(nodemap, "LineSelector").SetValue("Line2");
CEnumParameter(nodemap, "LineMode").SetValue("InOut");
CEnumParameter(nodemap, "BslLineConnection").SetValue("TwiSda");
// Set line 3 to input-output and use it for TWI SCL
CEnumParameter(nodemap, "LineSelector").SetValue("Line3");
CEnumParameter(nodemap, "LineMode").SetValue("InOut");
CEnumParameter(nodemap, "BslLineConnection").SetValue("TwiScl");
// Now, you must implement a suitable communication protocol.
// Camera parameters that help you implement the protocol are listed
// in the "Writing and Reading and Data" section above.
// Configure TWI communication
// Set the bit rate to 50 kbps
camera.Parameters[PLCamera.BslTwiBitrate].SetValue(PLCamera.BslTwiBitrate.Bitrate50kbps);
// Set line 2 to input-output and use it for TWI SDA
camera.Parameters[PLCamera.LineSelector].SetValue(PLCamera.LineSelector.Line2);
camera.Parameters[PLCamera.LineMode].SetValue(PLCamera.LineMode.InOut);
camera.Parameters[PLCamera.BslLineConnection].SetValue(PLCamera.BslLineConnection.TwiSda);
// Set line 3 to input-output and use it for TWI SCL
camera.Parameters[PLCamera.LineSelector].SetValue(PLCamera.LineSelector.Line3);
camera.Parameters[PLCamera.LineMode].SetValue(PLCamera.LineMode.InOut);
camera.Parameters[PLCamera.BslLineConnection].SetValue(PLCamera.BslLineConnection.TwiScl);
// Now, you must implement a suitable communication protocol.
// Camera parameters that help you implement the protocol are listed
// in the "Writing and Reading and Data" section above.
/* Macro to check for errors */
#define CHECK(errc) if (GENAPI_E_OK != errc) printErrorAndExit(errc)
GENAPIC_RESULT errRes = GENAPI_E_OK; /* Return value of pylon methods */
/* Configure TWI communication */
/* Set the bit rate to 50 kbps */
errRes = PylonDeviceFeatureFromString(hdev, "BslTwiBitrate", "Bitrate50kbps");
CHECK(errRes);
/* Set line 2 to input-output and use it for TWI SDA */
errRes = PylonDeviceFeatureFromString(hdev, "LineSelector", "Line2");
CHECK(errRes);
errRes = PylonDeviceFeatureFromString(hdev, "LineMode", "InOut");
CHECK(errRes);
errRes = PylonDeviceFeatureFromString(hdev, "BslLineConnection", "TwiSda");
CHECK(errRes);
/* Set line 3 to input-output and use it for TWI SCL */
errRes = PylonDeviceFeatureFromString(hdev, "LineSelector", "Line3");
CHECK(errRes);
errRes = PylonDeviceFeatureFromString(hdev, "LineMode", "InOut");
CHECK(errRes);
errRes = PylonDeviceFeatureFromString(hdev, "BslLineConnection", "TwiScl");
CHECK(errRes);
/* Now, you must implement a suitable communication protocol. */
/* Camera parameters that help you implement the protocol are listed */
/* in the "Writing and Reading and Data" section above. */
# Configure TWI communication
# Set the bit rate to 50 kbps
camera.BslTwiBitrate.Value = "Bitrate50kbps"
# Set line 2 to input-output and use it for TWI SDA
camera.LineSelector.Value = "Line2"
camera.LineMode.Value = "InOut"
camera.BslLineConnection.Value = "TwiSda"
# Set line 3 to input-output and use it for TWI SCL
camera.LineSelector.Value = "Line3"
camera.LineMode.Value = "InOut"
camera.BslLineConnection.Value = "TwiScl"
# Now, you must implement a suitable communication protocol.
# Camera parameters that help you implement the protocol are listed
# in the "Writing and Reading and Data" section above.
pylon Viewerを使用して、パラメーターを簡単に設定することもできます。