跳转到内容

两线接口#

通过双线接口相机功能,您可以利用双线接口 (TWI) 总线在相机和一个或多个外部设备之间传输数据。

例如,您可以使用相机来控制 TWI 兼容镜头的焦距。

该功能的使用#

运作原理#

TWI 总线是一种双线总线,由一根串行数据线 (SDA) 和一根串行时钟线 (SCL) 组成,属于双向线路。

相机(主控)和外部设备(从属)连接到数据线和时钟线。相机通过数据线发送或请求和检索数据。时钟线用于发送时钟信号,并且由相机控制。

双线接口图

要使用上述 TWI 接口,相机的 GenICam 参数必须符合从属设备的规格。

下面列出了可帮助您实施合适的通信协议的相机参数。

配置 TWI 通信#

配置相机和外部设备之间的 TWI 通信:

  1. 设置 BslTwiBitrate parameter to the bit rate you want to use for TWI, e.g., 50 kbps.
  2. 配置相机的输入-输出信号。
    1. 设置 LineSelector parameter to the I/O line that should write and read serial data, e.g., Line2. The line must be configured as an input-output line.
    2. 设置 BslLineConnection parameter to TwiSda.
    3. 设置 LineSelector parameter to the I/O line that should be used for sending clock signals, e.g., Line3. The line must be configured as an input-output line.
    4. 设置 BslLineConnection parameter to TwiScl.

写入和读取数据#

配置 TWI 通信后,您可以向外部设备写入数据并从中读取数据。

写入和读取参数#

写入和读取数据都涉及以下参数:

  • BslTwiTransferBuffer: The transfer buffer holds the data to be written, and also contains the data that has been read. It can hold up to 16 bytes.
  • BslTwiTransferLength: Indicates the number of bytes that should be transmitted from or to the transfer buffer. When writing, the transfer length specifies the number of bytes the camera should transmit per transaction. When reading, it specifies the number of bytes the camera has received during the last transaction. The maximum value is 16.
  • BslTwiTargetAddress: Indicates the 7-bit target address of the external slave device.

写入数据涉及以下参数:

  • BslTwiWrite: Execute this command to write the number of bytes specified by the transfer length from the transfer buffer to the target address of the slave device.

读取数据涉及以下参数:

  • BslTwiRead: Execute this command to read or retrieve the data from the target address of the slave device. The number of bytes read is specified by the transfer length.

传输状态#

The transfer status indicates the current status of the data transmission. When the transmission has been completed successfully, the transfer status indicates Success. To retrieve the current transfer status, update the transfer status by executing the BslTwiUpdateTransferStatus command and then get the value of BslTwiTransferStatus.

If the value of BslTwiTransferStatus is Pending, you repeatedly have to update and read the transfer status until the transfer status indicates Success. New data can only be written or read if the transfer status indicates Success.

传输状态可以采用以下值:

  • Success: The latest data transmission was successful.
  • Pending: A data transmission is pending.
  • NAKAddress: The camera didn't receive an acknowledge bit that confirms the target address after the latest write or read command. In this case, data transmission failed.
  • NAKData: The camera didn't receive an acknowledge bit for successful data transmission after the latest write command. In this case, data transmission failed.

将 I/O 线拉低#

Some TWI slave devices require pulling the I/O lines low before starting operation. If you need to pull an I/O line low, set the BslTwiPullSdaLow parameter or the BslTwiPullSclLow parameter or both to true. While one of them is true, data transmission is disabled.

示例代码#

// 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 轻松设置参数。