跳转到内容

Serial Communication#

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

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

该功能的使用#

运作原理#

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

Serial Communication 图

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

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

配置 Serial Communication#

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

  1. 设置 BslSerialBaudRate parameter to the baud rate of the external device, e.g., Baud9600.
  2. 设置 BslSerialNumberOfDataBits parameter to the number of data bits used by the external device, e.g., Bits8.
  3. 设置 BslSerialNumberOfStopBits parameter to the number of stop bits used by the external device, e.g., Bits1.
  4. 设置 BslSerialParity parameter to the kind of parity check performed by the external device, e.g., Even.
  5. Configure the camera's input signal. To do so, set the BslSerialRxSource parameter to the I/O line that is to receive serial data, e.g., Line2. The line must be configured as input.
  6. 配置相机的输出信号:
    1. 设置 LineSelector parameter to the I/O line that is to transmit serial data, e.g., Line3. The line must be configured as output.
    2. 设置 LineSource parameter to SerialTx.

测试 Serial Communication#

To test serial communication without having an external device connected to the camera, or to rule out errors caused by the external device, set the BslSerialRxSource parameter to SerialTx.

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

发送和接收数据#

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

信息

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

发送和接收参数#

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

  • BslSerialTransferBuffer: The transfer buffer holds the data to be transmitted, and also contains the data received. It can hold up to 16 symbols. A symbol consists of 7 or 8 bits, depending on the BslSerialNumberOfDataBits setting. On GigE cameras, the transfer buffer must be accessed in DWORDs (multiples of 4 bytes), even if the transfer length isn't a multiple of 4. For example, if you set the BslSerialTransferLength parameter to 9, you must read 12 bytes from the buffer for each transaction. On USB and CoaXPress cameras, this restriction does not apply.
  • BslSerialTransferLength: When receiving, indicates the number of symbols the camera has received during the last transaction. When transmitting, specifies how many symbols the camera should transmit per transaction. The maximum is 16.

发送参数#

发送数据涉及以下参数:

  • BslSerialTransmit: Execute this command to transmit the current value of the transfer buffer via the serial output line. This also updates the state of the BslSerialTxFifoEmpty and BslSerialTxFifoOverflow parameters. See below.
  • BslSerialTxFifoEmpty: Indicates whether the transmitting FIFO is empty, i.e., whether the camera is ready to transmit data.
  • BslSerialTxFifoOverflow: Indicates whether the transmitting FIFO has overflown. An overflow occurs if new data is transferred to the transmitting FIFO faster than the previous data can be sent out, i.e., if you execute the BslSerialTransmit command too fast. Data sent during an overflow will not be written to the transmitting FIFO.
  • BslSerialTxBreak: Signals a serial break to the external device. If this parameter is set to true, the camera sets the serial output to low level (space) to signal a break. If this parameter is set to false, the camera resets the serial output to high (mark).

接收参数#

接收数据涉及以下参数:

  • BslSerialReceive: Execute this command to receive data via the serial input line. After the transaction is completed, the BslSerialTransferBuffer parameter contains the received data, and the BslSerialTransferLength parameter indicates the number of valid symbols received. Also, executing the command updates the state of the BslSerialRxFifoOverflow, BslSerialRxParityError, and BslSerialRxStopBitError parameters. See below.
  • BslSerialRxFifoOverflow: Indicates whether the receiving FIFO has overflown. An overflow occurs if new data from the external device arrives faster than the previous data can be moved to the transfer buffer and sent to the host, i.e., if you execute the BslSerialReceive command too slowly. Data received during an overflow will be lost.
  • BslSerialRxParityError: Indicates whether the camera has detected a parity error in the data of the receiving FIFO. If an error is detected, the parameter remains true until you empty the receiving FIFO by executing the BslSerialReceive command.
  • BslSerialRxStopBitError: Indicates whether the camera has detected a stop bit error in the data of the receiving FIFO. If an error is detected, the parameter remains true until you empty the receiving FIFO by executing the BslSerialReceive command.
  • BslSerialRxBreak: Indicates whether the external device has signaled a serial break. If the serial input is low (space) for at least 12 bit periods, the camera detects a break and sets the parameter to true. Because the camera doesn't "know" how long the break signal will be, the parameter value will remain true. To reset it to false, make sure the external device has finished sending the break, then execute the BslSerialRxBreakReset command.
  • BslSerialRxBreakReset: Sets the BslSerialRxBreak parameter to false. See above.

示例代码#

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