Skip to content

C#

Guide

Interface名 一覧確認

1
2
3
4
5
6
7
8
    HTuple info;
    HTuple interfaces;

    HOperatorSet.InfoFramegrabber(
        "?",
        "info_boards",
        out info,
        out interfaces);

    private void ShowInstalledInterfaces()
    {
        HOperatorSet.InfoFramegrabber(
            "?",
            "info_boards",
            out HTuple info,
            out HTuple interfaces);

        for (int i = 0; i < interfaces.Length; i++)
        {
            Console.WriteLine(
                interfaces[i].S);
        }
    }

Interface名出力例

1
2
3
4
5
6
7
GigEVision2
GenICamTL
SiliconSoftware
BitFlow
SaperaLT
pylon
USB3Vision

Device名を確認

    HOperatorSet.InfoFramegrabber(
        "GigEVision2",
        "device",
        out HTuple info,
        out HTuple devices);

    for (int i = 0; i < devices.Length; i++)
    {
        Console.WriteLine(devices[i].S);
    }

CameraType候補確認

1
2
3
4
5
    HOperatorSet.InfoFramegrabber(
        "SiliconSoftware",
        "camera_type",
        out HTuple info,
        out HTuple cameraTypes);

SetFramegrabberParam() で使えるキーを確認

1
2
3
4
5
    HOperatorSet.InfoFramegrabber(
        "GigEVision2",
        "parameters",
        out HTuple info,
        out HTuple parameters);

実機・ボード情報確認

1
2
3
4
5
    HOperatorSet.InfoFramegrabber(
        "GigEVision2",
        "info_boards",
        out HTuple info,
        out HTuple boards);

    //AcquisitionLineRate line_rate LineRate
    HOperatorSet.SetFramegrabberParam(_acqHandle,"line_rate",settings.LineRateKHz * 1000.0);
    HOperatorSet.SetFramegrabberParam(_acqHandle,"image_height",settings.Height);



    private void ShowDevices(string halconInterfaceName)
    {
        HOperatorSet.InfoFramegrabber(
            halconInterfaceName,
            "device",
            out HTuple information,
            out HTuple devices);

        for (int i = 0; i < devices.Length; i++)
        {
            Console.WriteLine(devices[i].S);
        }
    }

    //Interface名ごとに問い合わせ例
    // ShowDevices("GigEVision2");
    // ShowDevices("SiliconSoftware");
    // ShowDevices("EuresysCoaxlink");

    // CameraType候補
    HOperatorSet.InfoFramegrabber(
        "SiliconSoftware",
        "camera_type",
        out HTuple information,
        out HTuple cameraTypes);



    //パラメータ名一覧を取得
        HOperatorSet.InfoFramegrabber(
            "GigEVision2",
            "parameters",
            out HTuple info,
            out HTuple parameters);

        //中身確認
        for (int i = 0; i < parameters.Length; i++)
        {
            Console.WriteLine(parameters[i].S);
        }

パラメータ例 所属 意味 ラインカメラでの典型値
Width カメラ 1ライン当たりの画素数 8192など
Height カメラ カメラが1フレームとして送るライン数 1固定の場合が多い
[Stream]BufferHeight Euresys Data Stream 何ラインをHALCONの1画像にまとめるか 1000、5000など
image_width HALCON Consumer HALCONが出力する画像幅 最終的に8192など
image_height HALCON Consumer HALCONが出力する画像高さ 最終的に5000など
[Stream]ScanLength Euresys Data Stream 1回のスキャンとして扱うライン数 撮像方式による

カメラ Width = 8192 カメラ Height = 1 Stream BufferHeight = 5000 ←これを設定(?)

HALCON取得画像 = 8192 × 5000


HALCON内部のパラメータ

小文字+アンダースコア形式

カメラ本体のGenICamパラメータ

CamelCase形式

GenTL Producerの各モジュール

モジュール名が角括弧


最初に試す設定

撮像開始前に、次のような設定を試すのがよいです。

1
2
3
4
HOperatorSet.SetFramegrabberParam(
    acqHandle,
    "[Stream]BufferHeight",
    5000);
その後で取得を開始します。
1
2
3
4
5
6
HOperatorSet.GrabImageStart(acqHandle, -1);

HOperatorSet.GrabImageAsync(
    out HObject image,
    acqHandle,
    -1);
取得後に実際の画像サイズを確認します。
1
2
3
4
5
6
HOperatorSet.GetImageSize(
    image,
    out HTuple width,
    out HTuple height);

Console.WriteLine($"Width={width.I}, Height={height.I}");

取得可能なパラメータ名を表示

1
2
3
4
5
6
7
8
9
HOperatorSet.GetFramegrabberParam(
    acqHandle,
    "available_param_names",
    out HTuple paramNames);

for (int i = 0; i < paramNames.Length; i++)
{
    Console.WriteLine(paramNames[i].S);
}

現在値を個別に取得する

string[] names =
{
    "Width",
    "Height",
    "image_width",
    "image_height",
    "[Stream]BufferHeight",
    "[Stream]ScanLength"
};

foreach (string name in names)
{
    try
    {
        HOperatorSet.GetFramegrabberParam(
            acqHandle,
            name,
            out HTuple value);

        Console.WriteLine($"{name} = {value}");
    }
    catch (HOperatorException ex)
    {
        Console.WriteLine(
            $"{name}: 取得不可 / ErrorCode={ex.GetErrorCode()}, Message={ex.Message}");
    }
}

using System;
using HalconDotNet;

public sealed class CoaxpressLineCamera : IDisposable
{
    private HTuple? _acqHandle;

    public void Open(string deviceName)
    {
        try
        {
            // GenICamTLデバイスをオープン
            HOperatorSet.OpenFramegrabber(
                "GenICamTL",       // Name
                0,                 // HorizontalResolution
                0,                 // VerticalResolution
                0,                 // ImageWidth
                0,                 // ImageHeight
                0,                 // StartRow
                0,                 // StartColumn
                "progressive",     // Field
                -1,                // BitsPerChannel
                "default",         // ColorSpace
                -1,                // Generic
                "false",           // ExternalTrigger
                "default",         // CameraType
                deviceName,        // Device
                0,                 // Port
                -1,                // LineIn
                out HTuple handle);

            _acqHandle = handle;

            // 撮像開始前にROI・バッファサイズを設定
            ConfigureImageSize(width: 100, height: 100);
        }
        catch
        {
            Close();
            throw;
        }
    }

    private void ConfigureImageSize(int width, int height)
    {
        EnsureOpened();

        // 撮像中の場合に備えて停止を試みる
        TrySetParameter("do_abort_grab", 1);

        /*
         * ラインカメラ本体側
         *
         * Width  = 1ライン当たりの画素数
         * Height = 通常1固定なので変更しない
         */
        HOperatorSet.SetFramegrabberParam(
            _acqHandle!,
            "Width",
            width);

        /*
         * Euresys Data Stream側
         *
         * BufferHeight = 1枚の画像としてまとめるライン数
         */
        HOperatorSet.SetFramegrabberParam(
            _acqHandle!,
            "[Stream]BufferHeight",
            height);

        // 設定された実値を確認
        PrintParameter("Width");
        PrintParameter("Height");
        PrintParameter("[Stream]BufferHeight");
        PrintParameter("image_width");
        PrintParameter("image_height");
    }

    public HObject GrabOneImage()
    {
        EnsureOpened();

        // 非同期取得の準備
        HOperatorSet.GrabImageStart(_acqHandle!, -1);

        // 100ラインが蓄積されると、100×100画像として返る想定
        HOperatorSet.GrabImageAsync(
            out HObject image,
            _acqHandle!,
            -1);

        // 実際に返されたHALCON画像のサイズを確認
        HOperatorSet.GetImageSize(
            image,
            out HTuple actualWidth,
            out HTuple actualHeight);

        Console.WriteLine(
            $"取得画像サイズ: {actualWidth.I} × {actualHeight.I}");

        if (actualWidth.I != 100 || actualHeight.I != 100)
        {
            image.Dispose();

            throw new InvalidOperationException(
                $"期待サイズは100×100ですが、" +
                $"{actualWidth.I}×{actualHeight.I}が返されました。");
        }

        return image;
    }

    private void PrintParameter(string parameterName)
    {
        try
        {
            HOperatorSet.GetFramegrabberParam(
                _acqHandle!,
                parameterName,
                out HTuple value);

            Console.WriteLine($"{parameterName} = {value}");
        }
        catch (HOperatorException ex)
        {
            Console.WriteLine(
                $"{parameterName}: 取得不可 " +
                $"HALCON Error={ex.GetErrorCode()}, {ex.Message}");
        }
    }

    private void TrySetParameter(string parameterName, HTuple value)
    {
        try
        {
            HOperatorSet.SetFramegrabberParam(
                _acqHandle!,
                parameterName,
                value);
        }
        catch (HOperatorException)
        {
            // 現在Grab中でない場合などは失敗しても問題ないため無視
        }
    }

    private void EnsureOpened()
    {
        if (_acqHandle is null || _acqHandle.Length == 0)
        {
            throw new InvalidOperationException(
                "カメラがOpenされていません。");
        }
    }

    public void Close()
    {
        if (_acqHandle is null || _acqHandle.Length == 0)
            return;

        try
        {
            TrySetParameter("do_abort_grab", 1);
            HOperatorSet.CloseFramegrabber(_acqHandle);
        }
        finally
        {
            _acqHandle.Dispose();
            _acqHandle = null;
        }
    }

    public void Dispose()
    {
        Close();
    }
}

設定順序

OpenFramegrabber
Width = 100
[Stream]BufferHeight = 100
GrabImageStart
GrabImageAsync
GetImageSizeで100×100を確認

実際に使える名前はコードで確認

HOperatorSet.GetFramegrabberParam(
    acqHandle,
    "available_param_names",
    out HTuple names);

for (int i = 0; i < names.Length; i++)
{
    string name = names[i].S;

    if (name.Contains("Width", StringComparison.OrdinalIgnoreCase) ||
        name.Contains("Height", StringComparison.OrdinalIgnoreCase))
    {
        Console.WriteLine(name);
    }
}