跳转到内容

Serial Communication#

相机的 Serial Communication 功能允许您通过相机的 I/O 线路在主机和外部设备之间建立串行通信。

这意味着相机充当主机和另一设备之间的中间 UART 设备。

该功能的使用#

运作原理#

相机提供两个独立的串行端口 FIFO:一个用于输出 (Tx) 数据,另一个用于输入 (Rx) 数据。为了与主机进行通信,两个 FIFO 都使用一个公共的传输缓冲区。

Serial Communication 图

要使用上述接口,必须使用合适的通信协议。协议的具体实施方式取决于您的系统设置和设备的特性。

下面列出了可帮助您执行该协议的相机参数。

配置 Serial Communication#

要配置相机和外部设备之间的串行通信:

  1. BslSerialBaudRate 参数设置为外部设备的波特率,例如,Baud9600
  2. BslSerialNumberOfDataBits 参数设置为外部设备使用的数据位数,例如,Bits8
  3. BslSerialNumberOfStopBits 参数设置为外部设备使用的停止位数,例如,Bits1
  4. BslSerialParity 参数设置为外部设备执行的奇偶校验的类型,例如,Even
  5. 配置相机的输入信号。为此,请将 BslSerialRxSource 参数设置为要接收串行数据的 I/O 线路,例如 Line2。该线路必须配置为输入
  6. Set the BslSerialTransmitMode parameter to one of the following values: BslSerialTransmitMode_HalfDuplex or BslSerialTransmitMode_FullDuplex. For a description of the parameter and values, see the Transmit Parameters section.
  7. 配置相机的输出信号:
    1. LineSelector 参数设置为传输串行数据的 I/O 线路,例如,Line3。必须将该线路配置为输出
    2. LineSource 参数设置为 SerialTx

测试 Serial Communication#

要在未将外部设备连接至相机的情况下测试串行通信,或排除由外部设备引起的错误,请将 BslSerialRxSource 参数设置为 SerialTx

这会产生回送:串行输入已在内部连接到串行输出,因此相机可以准确接收其传输的内容。

发送和接收数据#

配置串行通信后,可以将数据发送到外部设备并从其接收数据。

信息

如需了解详情,请参阅 pylon API 文档中的 ParametrizeCamera_SerialCommunication (C++) 代码示例。

发送和接收参数#

发送和接收数据均涉及以下参数:

  • BslSerialTransferBuffer:传输缓冲区保存要发送的数据,并且还包含接收的数据。它最多可容纳 16 个符号。一个符号由 7 或 8 位组成,具体取决于 BslSerialNumberOfDataBits 设置。在 GigE 相机上,即使传输长度不是 4 的倍数,也必须以 DWORD(4 字节的倍数)访问传输缓冲区。例如,如果将 BslSerialTransferLength 参数设置为 9,则每次事务处理必须从缓冲区读取 12 个字节。在 USB 相机和 CoaXPress 相机,此限制不适用。
  • BslSerialTransferLength:接收数据时,表示相机在上次事务处理期间已接收的符号数。发送数据时,指定相机在每次事务处理时应发送的符号数。最大值为 16。

发送参数#

发送数据涉及以下参数:

  • BslSerialTransmitMode: This parameter determines whether the receiving FIFO (Rx) of the camera is switched off or on during transmissions from the camera to an external device. If a single I/O line was shared for transmitting and receiving data (half-duplex), the receiving FIFO (Rx) of the camera would also receive the data that is transmitted to the external device. This would fill the receiving FIFO (Rx) very fast.
    Set the BslSerialTransmitMode parameter to BslSerialTransmitMode_HalfDuplex to switch off the receiving FIFO (Rx) during transmissions from the camera to an external device. This way, only data transmitted by the external device will be copied into the receiving FIFO (Rx) of the camera.
    If you work with two separate I/O lines, you can set the BslSerialTransmitMode parameter to BslTrasmitMode_FullDuplex.
  • BslSerialTransmit:执行此命令以通过串行输出线路发送传输缓冲区的当前值。这还将更新 BslSerialTxFifoEmptyBslSerialTxFifoOverflow 参数的状态。请参见下文。
  • BslSerialTxFifoEmpty:表示发送 FIFO 是否为空,即相机是否准备好发送数据。
  • BslSerialTxFifoOverflow:表示发送 FIFO 是否溢出。如果新数据传输至发送 FIFO 的速度快于先前数据的发送速度(即,如果执行 BslSerialTransmit 命令的速度过快),则会发生溢出。溢出期间发送的数据将不会写入发送 FIFO。
  • BslSerialTxBreak:向外部设备发出串行中断信号。如果将此参数设置为 true,则相机会将串行输出设置为低电平(空格)以发出中断信号。如果将此参数设置为 false,则相机会将串行输出重置为高电平(标记)。

接收参数#

接收数据涉及以下参数:

  • BslSerialReceive:执行此命令以通过串行输入线路接收数据。事务处理完成后,BslSerialTransferBuffer 参数会包含接收到的数据,而 BslSerialTransferLength 参数则表示接收到的有效符号数。另外,执行此命令将更新 BslSerialRxFifoOverflowBslSerialRxParityErrorBslSerialRxStopBitError 参数的状态。请参见下文。
  • BslSerialRxFifoOverflow:表示接收 FIFO 是否溢出。如果来自外部设备的新数据到达的速度快于先前数据移动到传输缓冲区并发送到主机的速度(即,如果执行 BslSerialReceive 命令的速度过慢),则会发生溢出。溢出期间接收的数据将丢失。
  • BslSerialRxParityError:表示相机是否已在接收 FIFO 的数据中检测到奇偶校验错误。如果检测到错误,则该参数将保持为 true,直至您通过执行 BslSerialReceive 命令清空接收 FIFO。
  • BslSerialRxStopBitError:表示相机是否已在接收 FIFO 的数据中检测到停止位错误。如果检测到错误,则该参数将保持为 true。直至您通过执行 BslSerialReceive 命令清空接收 FIFO。
  • BslSerialRxBreak:表示外部设备是否已发出串行中断信号。如果串行输入为低电平(空格)的情况至少持续 12 位周期,则相机会检测到中断信号并将该参数设置为 true。由于相机不“知道”中断信号将持续多久,所以该参数值将保持 true。要将其重置为 false,请确保外部设备已完成中断信号发送,然后执行 BslSerialRxBreakReset 命令。
  • BslSerialRxBreakReset:将 BslSerialRxBreak 参数设置为 false。请参见上文。

示例代码#

// Configure serial communication
// Example: 19200 Baud, 8 data bits, 2 stop bits, even parity
camera.BslSerialBaudRate.SetValue(BslSerialBaudRate_Baud19200);
camera.BslSerialNumberOfDataBits.SetValue(BslSerialNumberOfDataBits_Bits8);
camera.BslSerialNumberOfStopBits.SetValue(BslSerialNumberOfStopBits_Bits2);
camera.BslSerialParity.SetValue(BslSerialParity_Even);
// Configure the camera's input signal
camera.BslSerialRxSource.SetValue(BslSerialRxSource_Line2);
// Configure the camera's output signal
camera.LineSelector.SetValue(LineSelector_Line3);
camera.LineSource.SetValue(LineSource_SerialTx);
// Now, you must implement a suitable communication protocol.
// Camera parameters that help you implement the protocol are listed
// in the "Transmitting and Receiving Data" section above.
INodeMap& nodemap = camera.GetNodeMap();
// Configure serial communication
// Example: 19200 Baud, 8 data bits, 2 stop bits, even parity
CEnumParameter(nodemap, "BslSerialBaudRate").SetValue("Baud19200");
CEnumParameter(nodemap, "BslSerialNumberOfDataBits").SetValue("Bits8");
CEnumParameter(nodemap, "BslSerialNumberOfStopBits").SetValue("Bits2");
CEnumParameter(nodemap, "BslSerialParity").SetValue("Even");
// Configure the camera's input signal
CEnumParameter(nodemap, "BslSerialRxSource").SetValue("Line2");
// Configure the camera's output signal
CEnumParameter(nodemap, "LineSelector").SetValue("Line3");
CEnumParameter(nodemap, "LineSource").SetValue("SerialTx");
// Now, you must implement a suitable communication protocol.
// Camera parameters that help you implement the protocol are listed
// in the "Transmitting and Receiving Data" section above.
// Configure serial communication
// Example: 19200 Baud, 8 data bits, 2 stop bits, even parity
camera.Parameters[PLCamera.BslSerialBaudRate].SetValue(PLCamera.BslSerialBaudRate.Baud19200);
camera.Parameters[PLCamera.BslSerialNumberOfDataBits].SetValue(PLCamera.BslSerialNumberOfDataBits.Bits8);
camera.Parameters[PLCamera.BslSerialNumberOfStopBits].SetValue(PLCamera.BslSerialNumberOfStopBits.Bits2);
camera.Parameters[PLCamera.BslSerialParity].SetValue(PLCamera.BslSerialParity.Even);
// Configure the camera's input signal
camera.Parameters[PLCamera.BslSerialRxSource].SetValue(PLCamera.BslSerialRxSource.Line2);
// Configure the camera's output signal
camera.Parameters[PLCamera.LineSelector].SetValue(PLCamera.LineSelector.Line3);
camera.Parameters[PLCamera.LineSource].SetValue(PLCamera.LineSource.SerialTx);
// Now, you must implement a suitable communication protocol.
// Camera parameters that help you implement the protocol are listed
// in the "Transmitting and Receiving 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 serial communication */
/* Example: 19200 Baud, 8 data bits, 2 stop bits, even parity */
errRes = PylonDeviceFeatureFromString(hdev, "BslSerialBaudRate", "Baud19200");
CHECK(errRes);
errRes = PylonDeviceFeatureFromString(hdev, "BslSerialNumberOfDataBits", "Bits8");
CHECK(errRes);
errRes = PylonDeviceFeatureFromString(hdev, "BslSerialNumberOfStopBits", "Bits2");
CHECK(errRes);
errRes = PylonDeviceFeatureFromString(hdev, "BslSerialParity", "Even");
CHECK(errRes);
/* Configure the camera's input signal */
errRes = PylonDeviceFeatureFromString(hdev, "BslSerialRxSource", "Line2");
CHECK(errRes);
/* Configure the camera's output signal */
errRes = PylonDeviceFeatureFromString(hdev, "LineSelector", "Line3");
CHECK(errRes);
errRes = PylonDeviceFeatureFromString(hdev, "LineSource", "SerialTx");
CHECK(errRes);
/* Now, you must implement a suitable communication protocol. */
/* Camera parameters that help you implement the protocol are listed */
/* in the "Transmitting and Receiving Data" section above. */
# Configure serial communication
# Example: 19200 Baud, 8 data bits, 2 stop bits, even parity
camera.BslSerialBaudRate.Value = "Baud19200"
camera.BslSerialNumberOfDataBits.Value = "Bits8"
camera.BslSerialNumberOfStopBits.Value = "Bits2"
camera.BslSerialParity.Value = "Even"
# Configure the camera's input signal
camera.BslSerialRxSource.Value = "Line2"
# Configure the camera's output signal
camera.LineSelector.Value = "Line3"
camera.LineSource.Value = "SerialTx"
# Now, you must implement a suitable communication protocol.
# Camera parameters that help you implement the protocol are listed
# in the "Transmitting and Receiving Data" section above.

您也可以使用 pylon Viewer 轻松设置参数。