Grabbing 2 cameras with same Program

Hi,
I made Python program from https://help.commonvisionblox.com/API/Python/cvb_2_streaming_simple-example.html with parameter 0 or 1 for a system with 2 cameras at port 0 and 1:
with cvb.DeviceFactory.open(os.path.join(cvb.install_path(), "Drivers", "GenICam.vin"), port=CAM_PORT) as device:

I call it from 2 scripts parallel

grab0.cmd: 
python grab.py 0

grab1.cmd: 
python grab.py 1

This works only if I start first grab0.cmd and then grab1.cmd

If I start grab1.cmd first then in grab0.cmd an error occure:
with cvb.DeviceFactory.open(os.path.join(cvb.install_path(), “Drivers”, “GenICam.vin”), port=CAM_PORT) as device:
RuntimeError: {C-API call failed} ({LoadImageFileW})

A similar problem is with the .Net example Cvb.Net\GenICam.
I appended a parameter for the port in program.cs

   public static int port = 0;
    [STAThread]
    static void Main(string[] args)
    {
        if (args.Length != 0)
            int.TryParse(args[0], out port);

and in Mainform.cs:

  public MainForm()         {
    InitializeComponent();
    this.Text += "  Port:"+Program.port;
...
    var device = FileDialogs.LoadByDialog(path => DeviceFactory.Open(path,board:0, port:Program.port), "Video Interface Drivers (*.vin)|*.vin");

But in this case I get an Error by opening driver for port 1 if the driver of camera on Port 0 was opened in the other instance of the program:

Error loading driver:

Loading the Device from file C:\Program Files\STEMMER IMAGING\Common Vision Blox\Drivers\GenICam.vin failed.

How can a make a program which can start twice to grab on port 0 and port 1 independent?

Peter

Hi,

I found OpenPort with vin-driver and order of ports:

The solution is enable the following flag in GenICam.ini

AutoSwitchEnable=1

which is located in “c:\ProgramData\STEMMER IMAGING\Common Vision Blox\Drivers”

Now I can start the python and .net program independently.

Peter

3 Likes

with this function I check the GenICam.ini for AutoSwitchEnable=0 and change it if necessary:

import cvb
import os
import shutil
import re


def check_autoswitchenable():
    ini = os.path.join(cvb.data_path(), "Drivers", "GenICam.ini")
    with open(ini, 'r+') as f:
        text = f.read()
        replaced = re.sub(r'^\s*AutoSwitchEnable\s*=\s*0\s*$', 'AutoSwitchEnable=1', text, 1, re.M | re.IGNORECASE)
        if text != replaced:
            bak = ini + '.bak'
            if not os.path.exists(bak):  # check if the file doesn't exist
                shutil.copy(ini, bak)
            # write replaced in GenICam.ini at pos 0
            f.seek(0)
            f.truncate()
            f.write(replaced)

The python module configparser does not support comments, which are gone after read, change, update.

1 Like