問題描述:

雙攝影機在使用上, 由於每次抓到裝置索引順序的不同,

有可能這一次先抓到cam1, 下次可能先抓到cam2,

因此得想個辦法讓每次抓到的順序一致,

可依照使用者需求來定義先後順序

------------------------------------------------------

解決方法:

const uint NUM_DEVICES = 2;                // 裝置數量    
m_deviceID = new string[NUM_DEVICES];      //實際硬體抓到裝置ID
m_deviceIDUser = new string[NUM_DEVICES];  // 使用者定義裝置ID(由ini檔案載入更新)  

第一次使用:

1.  取得攝影機 DeviceID,

記錄在成員變數 m_diviceID[deviceIndex]

/* DeviceID */
     isReadable = Pylon.DeviceFeatureIsReadable(hDev[deviceIndex], "DeviceID");
     if (isReadable)
     {
         string name = Pylon.DeviceFeatureToString(hDev[deviceIndex], "DeviceID");
         m_deviceID[deviceIndex] = name;
         //MessageBox.Show(name);
     }


2. 將成員變數 m_diviceID結果回傳至人機介面,

public string[] DeviceID
{
    get
    {
        return m_deviceID;
    }
}

假設目前硬體抓到攝影機索引的序列如下:

m_deviceID[0] = “123456”

m_deviceID[1] = “654321”

使用者若進行硬體索引對調, 並儲存成ini

下次載入該設定至變數 m_deviceIDUSER

// set: 使用者定義裝置ID(由ini檔案載入更新) 
       public string[] DeviceIDUser
       {
           set
           {
               m_deviceIDUser = value;
           }
           get
           {
               return m_deviceIDUser;
           }
       }

m_deviceIDUSER[0] = “654321”

m_deviceIDUSER[1] = “123456”

下次啟動時假設抓到的順序如下

m_deviceID[0] = “123456”

m_deviceID[1] = “654321”

利用變數map進行硬體索引更新

int[] map = new int[2] { 0, 1 };           // 硬體索引重新mapping

如何更新呢?

依序比對m_deviceID[]和第幾個m_deviceIDUser匹配

/* DeviceID */
        isReadable = Pylon.DeviceFeatureIsReadable(hDev[deviceIndex], "DeviceID");
        if (isReadable)
        {
            string name = Pylon.DeviceFeatureToString(hDev[deviceIndex], "DeviceID");
            m_deviceID[deviceIndex] = name;
            for (int i = 0; i < NUM_DEVICES; i++)
            {
                if (m_deviceID[deviceIndex] == m_deviceIDUser[i])
                {
                    m_map[deviceIndex] = i;
                }
            }
        }

一旦m_deviiceID與m_deviceIDUser順序不一致,

則會更新m_map[deviceIndex];

否則不更新m_map[deviceIndex]

int deviceIndexM;                       // 根據攝影機抓到的順序重新mapping
            for (deviceIndex = 0; deviceIndex < NUM_DEVICES; ++deviceIndex)
            {
                deviceIndexM = m_map[deviceIndex];  // 根據攝影機抓到的順序重新mapping
                #region Allocate and register buffers for grab
                /* Determine the required size for the grab buffer. */
                payloadSize[deviceIndexM] = checked((uint)Pylon.DeviceGetIntegerFeature(hDev[deviceIndexM], "PayloadSize"));
 
                /* Image grabbing is done using a stream grabber.  
                  A device may be able to provide different streams. A separate stream grabber must 
                  be used for each stream. In this sample, we create a stream grabber for the default 
                  stream, i.e., the first stream ( index == 0 ).
                  */
 
                /* Get the number of streams supported by the device and the transport layer. */
                nStreams[deviceIndexM] = Pylon.DeviceGetNumStreamGrabberChannels(hDev[deviceIndexM]);
 
                if (nStreams[deviceIndexM] < 1)
                {
                    throw new Exception("The transport layer doesn't support image streams.");
                }
 
                /* Create and open a stream grabber for the first channel. */
                hGrabber[deviceIndexM] = Pylon.DeviceGetStreamGrabber(hDev[deviceIndexM], 0);
 
                Pylon.StreamGrabberOpen(hGrabber[deviceIndexM]);
 
 
                /* Get a handle for the stream grabber's wait object. The wait object
                   allows waiting for buffers to be filled with grabbed data. */
                hWait[deviceIndexM] = Pylon.StreamGrabberGetWaitObject(hGrabber[deviceIndexM]);
 
                /* Add the stream grabber's wait object to our wait objects.
                   This is needed to be able to wait until all cameras have 
                   grabbed an image in our grab loop below. */
                Pylon.WaitObjectsAdd(wos, hWait[deviceIndexM]);
 
                /* We must tell the stream grabber the number and size of the buffers 
                    we are using. */
                /* .. We will not use more than NUM_BUFFERS for grabbing. */
                Pylon.StreamGrabberSetMaxNumBuffer(hGrabber[deviceIndexM], NUM_BUFFERS);
 
                /* .. We will not use buffers bigger than payloadSize bytes. */
                Pylon.StreamGrabberSetMaxBufferSize(hGrabber[deviceIndexM], payloadSize[deviceIndexM]);
 
                /*  Allocate the resources required for grabbing. After this, critical parameters 
                    that impact the payload size must not be changed until FinishGrab() is called. */
                Pylon.StreamGrabberPrepareGrab(hGrabber[deviceIndexM]);
 
                /* Before using the buffers for grabbing, they must be registered at
                   the stream grabber. For each registered buffer, a buffer handle
                   is returned. After registering, these handles are used instead of the
                   buffer objects pointers. The buffer objects are held in a dictionary,
                   that provides access to the buffer using a handle as key.
                 */
                buffers[deviceIndexM] = new Dictionary<PYLON_STREAMBUFFER_HANDLE, PylonBuffer<Byte>>();
                for (i = 0; i < NUM_BUFFERS; ++i)
                {
                    PylonBuffer<Byte> buffer = new PylonBuffer<byte>(payloadSize[deviceIndexM], true);
                    PYLON_STREAMBUFFER_HANDLE handle = Pylon.StreamGrabberRegisterBuffer(hGrabber[deviceIndexM], ref buffer);
                    buffers[deviceIndexM].Add(handle, buffer);
                }
 
                /* Feed the buffers into the stream grabber's input queue. For each buffer, the API 
                   allows passing in an integer as additional context information. This integer
                   will be returned unchanged when the grab is finished. In our example, we use the index of the 
                   buffer as context information. */
                i = 0;
                foreach (KeyValuePair<PYLON_STREAMBUFFER_HANDLE, PylonBuffer<Byte>> pair in buffers[deviceIndexM])
                {
                    Pylon.StreamGrabberQueueBuffer(hGrabber[deviceIndexM], pair.Key, i++);
                }
                #endregion
            }    
全站熱搜
創作者介紹
創作者 me1237guy 的頭像
me1237guy

天天向上

me1237guy 發表在 痞客邦 留言(1) 人氣()