Serial Communication#
這表示相機在主機與另一台裝置之間充當中介的UART裝置。
資訊
Serial Communication 功能僅適用於以下 Basler 相機型號:ace 2 R GigEPRO(僅限 1GigE)、ace 2 R USBPRO 、dart M 以及 boost R 其他相機型號。
使用功能#
運作原理#
此攝影機提供兩個獨立的串列埠FIFO:一個用於傳出(Tx)資料,另一個用於傳入(Rx)資料。在與主機通訊時,這兩個 FIFO 皆使用共同的傳輸緩衝區。
若要使用上述所述的介面,您必須實作一個合適的通訊協定。該協定的實際實作方式,取決於您的系統設定以及裝置的特性。
以下列出有助於您實作該通訊協定的相機參數。
設定Serial Communication#
若要設定相機與外部裝置之間的序列通訊:
- 將相機中的
BslSerialBaudRate參數需設定為外部裝置的波特率,例如:Baud9600.
資訊
若您將光耦合 I/O 線用於接收(Rx)資料,請注意,最大波特率上限為 19200 Bd。若您使用光耦合 I/O 線,請將 BslSerialBaudRate 請據此調整參數。
- 將相機中的
BslSerialNumberOfDataBits參數應設定為外部裝置所使用的資料位元數,例如:Bits8. - 將相機中的
BslSerialNumberOfStopBits參數應設定為外部裝置所使用的停止位元數,例如:Bits1. - 將相機中的
BslSerialParity參數,用於指定外部裝置所執行的奇偶校驗類型,例如:Even. - 設定攝影機的輸入訊號。要執行此操作,請設定
BslSerialRxSource傳遞至將接收序列資料的 I/O 線的參數,例如:Line2. 該行必須是 設定為輸入. - 將相機中的
BslSerialTransmitMode參數設為HalfDuplex或FullDuplex中。BslSerialTransmitMode該參數及其值在 傳輸參數 章節。 - 設定攝影機的輸出訊號:
- 將相機中的
LineSelector傳輸串列資料的 I/O 線路之參數,例如:Line3. 該行必須是 設定為輸出. - 將相機中的
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: 此參數決定在相機向外部裝置傳輸資料期間,相機的接收 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直到您透過執行BslSerialReceive指令。BslSerialRxStopBitError: 表示相機是否在接收 FIFO 的資料中偵測到停止位元錯誤。若偵測到錯誤,此參數將維持true直到您透過執行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.
您也可以 use the pylon Viewer 來輕鬆設定參數。