两线接口#
例如,您可以使用相机控制兼容 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 位目标地址。
写入数据时涉及以下 Property:
BslTwiWrite:执行此命令,可将传输缓冲区中由传输长度指定的字节数写入从设备的目地地址。
读取数据时涉及以下 Property:
BslTwiRead:执行此命令,可从从设备的目地地址读取或检索数据。读取的字节数由传输长度指定。
传输状态#
传输状态表示数据传输的当前状态。当传输成功完成时,传输状态显示为 Success。要检索当前的传输状态,请通过执行 BslTwiUpdateTransferStatus 命令来更新传输状态,然后获取 BslTwiTransferStatus.
如果 BslTwiTransferStatus 的值为 Pending的值为 Success。只有当传输状态显示为 Success.
传输状态可以取以下值:
Success:最近一次数据传输成功。Pending:数据传输挂起。NAKAddress:相机在最近一次写入或读取命令后未收到确认目标地址的应答位。在这种情况下,数据传输失败。NAKData:相机在最近一次写入命令后未收到数据传输成功的应答位。在这种情况下,数据传输失败。
将 I/O 线路拉低#
某些 TWI 从设备要求在开始操作前将 I/O 线路拉低。如果您需要将 I/O 线路拉低,请将 BslTwiPullSdaLow Property 或 BslTwiPullSclLow Property(或两者)设置为 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 轻松设置参数。