using System.Runtime.InteropServices;
namespace HardwareControl.Core.FocusSystem
{
///
/// FocusSystem2.dll 的 P/Invoke 声明。
/// 所有结构体按 C 端 #pragma pack(1) 布局,字段顺序与 focus_system2.h 一致。
/// 除查询类函数外,命令函数内部已完成"组包→发送→收应答"。
///
internal static class FocusSystemNative
{
private const string DllName = "FocusSystem2.dll";
// 错误码,对应 FOCUS2_ERR_*
internal const int Ok = 0;
internal const int ErrNullPtr = -1;
internal const int ErrBufTooSmall = -2;
internal const int ErrFrameTooShort = -3;
internal const int ErrComOpenFailed = -4;
internal const int ErrComNotOpen = -5;
internal const int ErrComSendFailed = -6;
internal const int ErrParseFailed = -7;
internal const int ErrTimeout = -8;
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IntPtr focus_init([MarshalAs(UnmanagedType.LPStr)] string comPort);
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern void focus_close(IntPtr handle);
// ---------- 对焦 ----------
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_single_focus(IntPtr handle);
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_continuous_focus(IntPtr handle);
// ---------- 校准 ----------
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_calib_mode(IntPtr handle);
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_calib_move(IntPtr handle, sbyte distance, ushort speed);
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_calib_save(IntPtr handle);
// ---------- 激光器 ----------
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_laser_on(IntPtr handle, byte freq);
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_laser_off(IntPtr handle);
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_laser_power(IntPtr handle, byte power);
// ---------- 测距 ----------
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_single_range(IntPtr handle);
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_continuous_range(IntPtr handle);
// ---------- 升降机构 ----------
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_lift_control(IntPtr handle, sbyte position);
// ---------- 外部触发 ----------
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_ext_trigger_set(IntPtr handle);
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_ext_trigger_fire(IntPtr handle);
// ---------- 自动检测 ----------
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_auto_descend(IntPtr handle);
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_fixed_cycle(IntPtr handle, ushort timeMs, ushort count);
// ---------- 光谱仪同步 ----------
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_sync_time_set(
IntPtr handle,
ref Focus2SyncTimeParams parameters);
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_sync_time_get(IntPtr handle);
// ---------- 电源 ----------
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_power_off(IntPtr handle);
// ---------- 查询 ----------
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_read_sys_info(IntPtr handle, out Focus2SysInfo info);
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int focus_query_result(IntPtr handle, out Focus2RealTimeResp result);
// ---------- 错误 ----------
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr focus_get_error(IntPtr handle);
/// 读取 DLL 内部记录的最后一次错误描述;无错误或句柄为空时返回空串。
internal static string GetError(IntPtr handle)
{
if (handle == IntPtr.Zero)
{
return string.Empty;
}
var pointer = focus_get_error(handle);
return pointer == IntPtr.Zero
? string.Empty
: Marshal.PtrToStringAnsi(pointer) ?? string.Empty;
}
}
/// 系统信息,对应 Focus2SysInfo(15 字节载荷)。
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct Focus2SysInfo
{
public byte SysSta;
public ushort FocusPos;
public byte Error;
public sbyte Location;
public byte LaserSta;
public byte Voltage;
public ushort ExtVoltage;
public byte HwVersion;
public byte SwVersion;
public ushort StartTime;
public ushort SyncTime;
}
/// 实时应答,对应 Focus2RealTimeResp(3~5 字节载荷)。
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct Focus2RealTimeResp
{
public byte Type;
public byte Sta;
public byte Error;
public ushort Data;
}
/// 光谱仪同步参数,对应 Focus2SyncTimeParams(8 字节)。
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct Focus2SyncTimeParams
{
public ushort DelayUs;
public ushort OffsetUs;
public ushort HoldUs;
public ushort HoldOffsetUs;
}
}