跳至內容
測試伺服器
開發伺服器

Serial Communication#

透過 Serial Communication camera feature,您可以透過相機的 I/O 線路在主機與外部裝置之間建立 Serial Communication。

這意味著相機在主機與另一個裝置之間充當中間 UART 裝置。

資訊

Serial Communication 功能僅適用於以下 Basler 相機型號:ace 2 R GigE PRO(僅限 1GigE)、ace 2 R USB PRO、dart M 以及 boost R 相機。

使用此功能#

運作原理#

相機提供兩個獨立的序列埠 FIFOs:一個用於傳出 (Tx) 資料,另一個用於傳入 (Rx) 資料。為了與主機通訊,兩個 FIFO 使用共同的傳輸緩衝區。

Serial Communication 圖表

若要使用上述介面,您必須實作適當的通訊協定。協定的實際實作取決於您的系統設定和裝置的特性。

協助您實作協定的相機參數列於下方。

設定 Serial Communication#

若要設定相機與外部裝置之間的 Serial Communication:

  1. 將 BslSerialBaudRate 參數設為外部裝置的鮑率,例如: Baud9600.

資訊

如果您使用光耦合 I/O 線路進行傳入 (Rx) 資料傳輸,請注意最大鮑率限制為 19200 Bd。如果您使用光耦合 I/O 線路,請將 BslSerialBaudRate 參數相應設定。

  1. 將 BslSerialNumberOfDataBits 參數設為外部裝置使用的資料位元數,例如: Bits8.
  2. 將 BslSerialNumberOfStopBits 參數設為外部裝置使用的停止位元數,例如: Bits1.
  3. 將 BslSerialParity 參數設為外部裝置執行的同位檢查類型,例如: Even.
  4. 設定相機的輸入訊號。為此,請將 BslSerialRxSource 參數設為要接收序列資料的 I/O 線路,例如: Line2。該線路必須 設定為輸入.
  5. 將 BslSerialTransmitMode 參數設為 HalfDuplex 或 FullDuplex。Vignetting Correction BslSerialTransmitMode 參數及其值在 傳輸參數 章節中說明。
  6. 設定相機的輸出訊號:
    1. 將 LineSelector 參數設為要傳輸序列資料的 I/O 線路,例如: Line3。該線路必須 設定為輸出.
    2. 將 LineSource 參數設為 SerialTx.

測試 Serial Communication#

若要測試 Serial Communication 而不需要將外部裝置連接到相機,或排除由外部裝置引起的錯誤,請將 BslSerialRxSource 參數設為 SerialTx.

這會建立一個迴圈:序列輸入在內部連接到序列輸出,因此相機接收到的內容與其傳輸的內容完全相同。

傳送與接收資料#

在您 設定 Serial Communication 之後,您可以向外部裝置發送資料並從其接收資料。

資訊

如需詳細資訊,請參閱 pylon API Documentation 中的 ParametrizeCamera_SerialCommunication (C++) 程式碼範例。

傳輸與接收參數#

以下參數同時涉及資料的發送與接收:

  • BslSerialTransferBuffer:傳輸緩衝區保留要傳輸的資料,也包含接收到的資料。它最多可容納 16 個符號。一個符號由 7 或 8 個位元組成,具體取決於 BslSerialNumberOfDataBits 設定。在 GigE 相機上,必須以 DWORD(4 位元的倍數)存取傳輸緩衝區,即使傳輸長度不是 4 的倍數也是如此。例如,如果您將 BslSerialTransferLength 參數設為 9,則每次傳輸必須從緩衝區讀取 12 個位元組。在 USB 和 CoaXPress 相機上,沒有此限制。
  • BslSerialTransferLength:接收時,表示相機在上一次傳輸期間收到的符號數量。傳輸時,指定相機每次傳輸應發送多少個符號。最大值為 16。

傳輸參數#

以下參數涉及資料的發送:

  • BslSerialTransmitMode:此參數決定在從相機傳輸資料到外部裝置期間,相機的接收 FIFO (Rx) 是關閉還是開啟。如果共用單一 I/O 線路來發送和接收資料(半雙工),相機的接收 FIFO (Rx) 也會接收傳輸到外部裝置的資料。這會非常快速地填滿接收 FIFO (Rx)。
    將 BslSerialTransmitMode 參數設為 HalfDuplex 以在從相機傳輸資料到外部裝置期間關閉接收 FIFO (Rx)。這樣一來,只有外部裝置傳輸的資料才會被複製到相機的接收 FIFO (Rx) 中。
    如果您使用兩條獨立的 I/O 線路,您可以將 BslSerialTransmitMode 參數設為 FullDuplex.
  • BslSerialTransmit:執行此命令可透過序列輸出線路傳輸傳輸緩衝區的當前值。這也會更新 BslSerialTxFifoEmpty 和 BslSerialTxFifoOverflow 參數的狀態。
  • BslSerialTxFifoEmpty:表示傳輸 FIFO 是否為空,即相機是否準備好傳輸資料。
  • BslSerialTxFifoOverflow:表示傳輸 FIFO 是否溢位。如果將新資料傳輸到傳輸 FIFO 的速度快於先前資料發送出去的速度,就會發生溢位,也就是說,如果您執行 BslSerialTransmit 命令的速度太快。在溢位期間發送的資料不會寫入傳輸 FIFO。
  • BslSerialTxBreak:向外部裝置發送信號中斷。如果此參數設為 true,相機將序列輸出設為低電平(空間)以發送信號中斷。如果此參數設為 false,相機將序列輸出重設為高電平(標記)。

接收參數#

以下參數涉及資料的接收:

  • BslSerialReceive:執行此命令透過序列輸入線路接收資料。傳輸完成後, BslSerialTransferBuffer 參數包含收到的資料,且 BslSerialTransferLength 參數表示收到的有效符號數量。此外,執行該命令會更新 BslSerialRxFifoOverflow, BslSerialRxParityError的狀態,以及 BslSerialRxStopBitError 參數。請參見下文。
  • BslSerialRxFifoOverflow:指示接收 FIFO 是否溢位。如果外部裝置傳送新資料的速度大於先前資料移至傳送緩衝區並傳送至主機的速度,即當您執行 BslSerialReceive 指令的速度過慢時,就會發生溢位。溢位期間接收到的資料將會遺失。
  • BslSerialRxParityError:指示相機是否在接收 FIFO 的資料中偵測到奇偶校驗錯誤。如果偵測到錯誤,該參數將保持 true ,直到您透過執行以下操作清空接收 FIFO: BslSerialReceive 命令。
  • BslSerialRxStopBitError:指示相機是否在接收 FIFO 的資料中偵測到停止位元錯誤。如果偵測到錯誤,該參數將保持 true ,直到您透過執行以下操作清空接收 FIFO: BslSerialReceive 命令。
  • 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 來輕鬆設定參數。