基于C#的Baumer相机二次开发教程

发布于:2025-06-20 ⋅ 阅读:(18) ⋅ 点赞:(0)

环境配置

  1. 新建项目
  2. 右键【依赖项】-> 选择【bin】文件夹中的【bgapi2_genicam_dotnet.dll】文件
  3. 当dll文件被添加到【依赖项\程序集】中之后,右键->【属性】,将其【复制本地】设为【True】

此动态链接库自身也有很多依赖项,为了省事儿,可以将其同文件夹下的内容,全部复制粘贴到debug或release文件夹。

接口层级

Baumer相机提供了多个不同的system,每个system下分为多个API,每个API又对应多个设备。因此,在二次开发时,需要先遍历所有的system,再遍历system下的api,最后遍历api下的device,才能获取相应的设备。

一般来说,每个system对应一个.cti格式的文件,例如我所用的SDK中,探测到了【bgapi2_usb.cti_2.15.2】和【bgapi2_gige.cti_2.15.2】这两个system。通过下面的代码,可以获取这些System

using BGAPI2;

public string getSystemList()
{
   string txtInfo = "";
   SystemList sysList;
   BGAPI2.System mSystem = null;
   try
   {
       sysList = SystemList.Instance;
       sysList.Refresh();
       txtInfo += $"5.1.2 Detected systems: {sysList.Count}\n";
   }
   catch (BGAPI2.Exceptions.IException ex)
   {
       txtInfo += $"ErrorType: {ex.GetType()}\n " +
           $"ErrorException {ex.GetErrorDescription()}\n " +
           $"in function: {ex.GetFunctionName()}";
       return txtInfo;
   }
   foreach (KeyValuePair<string, BGAPI2.System> sys in sysList)
   {
       txtInfo += $"the {sys.Key} will be opened!\n";
       mSystem = sys.Value;
       mSystem.Open();
       txtInfo += //$"\nSystem ID: {mSystem.Id}\n" +
                  $"System Name: {mSystem.FileName}\n" +
                  $"System Vendor: {mSystem.Vendor}\n" +
                  $"System Version: {mSystem.Version}\n" +
                  $"System Model: {mSystem.Model}\n";
        mSystem.Close();

   }
       return txtInfo;
}

其返回内容表示,探测到两个system,列表如下

System Name bgapi2_usb.cti bgapi2_gige.cti
System Vendor Baumer Baumer
System Version 2.15.2 2.15.2
System Model bgapi2_usb bgapi2_gige

USB和GigE分别是Baumer GAPI支持的接口类型,分别使用USB和网口进行通信。

启动相机

相机的启动流程为

  1. 打开system
  2. 打开api
  3. 打开设备
  4. 打开设备的数据流
  5. 打开数据流对应的buffer

每个system中,提供了不同的API,每个API又适配不同类型的设备,所以想获得设备信息,需要先便利system,再遍历相应的API,最后对设备进行遍历,代码如下。


BGAPI2.System workSystem;
BGAPI2.Device workDevice;
BGAPI2.Interface workApi;
BGAPI2.DataStream workDS;
BGAPI2.BufferList workBL;


public void startSystem()
{
   SystemList sysList;
   BGAPI2.System mSystem;
   sysList = SystemList.Instance;
   sysList.Refresh();
   foreach (KeyValuePair<string, BGAPI2.System> sys in sysList)
   {
       if (sys.Key.Contains("gige"))
       {
           workSystem = sys.Value;
           workSystem.Open();
           break;
       }
   }
}

public void startAPI()
{
   string txtInfo = "";
   InterfaceList iList = workSystem.Interfaces;
   iList.Refresh(1);

   foreach (KeyValuePair<string, BGAPI2.Interface> ifc_pair in iList)
   {
       workApi = ifc_pair.Value;
       workApi.Open();
   }
}

public void startDevice()
{
   BGAPI2.DeviceList devLst = workApi.Devices;
   devLst.Refresh(1);
   foreach (KeyValuePair<string, BGAPI2.Device> dev_pair in devLst)
   {
       workDevice = dev_pair.Value;
       workDevice.Open();
       break;
   }
}

public void startDataStream()
{
   BGAPI2.DataStreamList dsList = workDevice.DataStreams;
   dsList.Refresh();
   foreach (KeyValuePair<string, BGAPI2.DataStream> ds_pair in dsList)
   {
       workDS = ds_pair.Value;
       workDS.Open();
       break;
   }
}

public void startBuffer()
{
   workBL = workDS.BufferList;
   BGAPI2.Buffer bf;


   long iDevicePayloadsize = (long)workDevice.RemoteNodeList["PayloadSize"].Value;
   IntPtr mUserBuffer = new IntPtr(0);
   ulong uPayloadSize = workDS.IsDefinedPayloadSize ? workDS.PayloadSize : (ulong)iDevicePayloadsize;


   List<IntPtr> LUserBuffer = new List<IntPtr>();
   for (int i = 0; i < 4; i++)
   {
       mUserBuffer = Marshal.AllocHGlobal((int)uPayloadSize);
       LUserBuffer.Add(mUserBuffer);
       IntPtr pUserObj = new IntPtr(0); // NULL pointer, not used
       bf = new BGAPI2.Buffer(mUserBuffer, uPayloadSize, pUserObj);
       workBL.Add(bf); // 1MB buffer size
   }

   foreach (KeyValuePair<string, BGAPI2.Buffer> buf_pair in workBL)
   {
       buf_pair.Value.QueueBuffer();
   }

   workDS.StartAcquisition();  //开始采集数据流
   workDevice.RemoteNodeList["AcquisitionStart"].Execute(); //开始采集数据
}

读取相机数据

在开启数据采集之后,接下来就是把数据装载到buffer中,如果想要读取这些buffer数据,可将其进一步存储为BGAPI2中的Image格式,示例如下

BGAPI2.Image imgWork = null;
public string fillBuffer()
{
    string info = "";
    ImageProcessor imgPro = new ImageProcessor(); //创建图像处理器对象
    BGAPI2.Buffer bfFilled = null;
    bfFilled = workDS.GetFilledBuffer(1000);
    if (bfFilled == null)
        info += "Error: Buffer Timeout after 1000 ms\n";
    else
    {
        info += $"图像{bfFilled.FrameID}的内存地址为{(ulong)bfFilled.MemPtr}";
        imgWork = imgPro.CreateImage((uint)bfFilled.Width,
                                     (uint)bfFilled.Height,
                                     (string)bfFilled.PixelFormat,
                                     bfFilled.MemPtr,
                                     (ulong)bfFilled.MemSize); //创建图像对象

        bfFilled.QueueBuffer(); //将缓冲区重新放回队列中,等待下一次使用
    }

   return info;
}

网站公告

今日签到

点亮在社区的每一天
去签到