两线接口#
例如,您可以使用相机来控制兼容 TWI 的镜头的对焦。
该功能的使用#
运作原理#
TWI 总线是一个双线总线,由一条串行数据线 (SDA) 和一条串行时钟线 (SCL) 组成。这些都是双向线路。
相机(主机)和外部设备(从机)连接到数据线和时钟线。相机通过数据线发送或请求并检索数据。时钟线用于发送时钟信号,由相机控制。
要使用上面概述的 TWI 接口,相机的 GenICam 参数必须符合从机设备的规范。
帮助您实现合适通信协议的相机参数列于下方。
配置 TWI 通信#
若要配置相机与外部设备之间的 TWI 通信:
- 设置
BslTwiBitrate参数设置为您想要用于 TWI 的比特率,例如 50 kbps。 - 配置相机的输入输出信号。
- 设置
LineSelector参数设置为应用於写入和读取串行数据的 I/O 线路,例如,Line2。该线路必须配置为 输入输出线路. - 设置
BslLineConnection参数至TwiSda. - 设置
LineSelector参数设置为应用于发送时钟信号的 I/O 线路,例如,Line3。该线路必须配置为输入输出线路。 - 设置
BslLineConnection参数至TwiScl.
- 设置
写入和读取数据#
在配置 TWI 通信之后,您可以向外部设备写入数据并从中读取数据。
写入和读取参数#
写入和读取数据均涉及以下参数:
BslTwiTransferBuffer:传输缓冲区保存要写入的数据,还包含已读取的数据。它最多可以保存 16 个字节。BslTwiTransferLength:指示应从传输缓冲区传输或传输到传输姆缓冲区的字节数。写入时,传输长度指定相机每次事务应传输的字节数。读取时,它指定相机在上一次事务期间接收到的字节数。最大值为 16。BslTwiTargetAddress:指示外部从设备的 7 位目标地址。
写入数据时涉及以下参数:
BslTwiWrite:执行此命令,可将传输长度指定的字节数从传输缓冲区写入从设备的目标地址。
读取数据时涉及以下参数:
BslTwiRead:执行此命令,可从从设备的目标地址读取或检索数据。读取的字节数由传输长度指定。
传输状态#
传输状态指示数据传输的当前状态。当传输成功完成后,传输状态指示为 Success。要检索当前的传输状态,可通过执行 BslTwiUpdateTransferStatus 命令来更新传输状态,然后获取 BslTwiTransferStatus.
的值。如果 BslTwiTransferStatus 的值为 Pending的值为 Success,则必须重复更新和读取传输状态,直到传输状态指示为 Success.
。只有在传输状态指示为时,才能写入或读取新数据
Success:最新数据传输成功。Pending:数据传输挂起。NAKAddress:相机在最近一次写入或读取命令后未收到确认目标地址的应答位。在此情况下,数据传输失败。NAKData:相机在最近一次写入命令后未收到数据传输成功的应答位。在此情况下,数据传输失败。
将 I/O 线路拉低#
某些 TWI 从设备需要在开始运行之前将 I/O 线路拉低。如果需要将 I/O 线路拉低,请将 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 轻松设置参数。