提交位移台和对焦系统代码

This commit is contained in:
ldh
2026-11-18 08:55:40 +08:00
parent 29e90bbc7b
commit dd6edce089
21 changed files with 2083 additions and 0 deletions
@@ -0,0 +1,30 @@
namespace HardwareControl.Core.FocusSystem
{
/// <summary>实时应答类型,对应 Focus2RealTimeResp.type。</summary>
public enum FocusRealTimeResultType
{
/// <summary>消息应答(0x01),命令执行回执。</summary>
MessageAck = 0x01,
/// <summary>运行结果(0x02)。</summary>
RunningResult = 0x02,
/// <summary>查询结果(0x03),如同步延时获取。</summary>
QueryResult = 0x03,
Unknown = 0xFF,
}
/// <summary>设备缓存的运行结果/查询结果(focus_query_result)。</summary>
/// <param name="Type">应答类型。</param>
/// <param name="Success">执行状态是否为成功(sta=0x01)。</param>
/// <param name="DeviceError">设备端错误码。</param>
/// <param name="Data">附加数据;仅查询结果(0x03)携带,其余为 null。</param>
/// <param name="ReceivedAt">本机解析到该帧的时间。</param>
public sealed record FocusRealTimeResult(
FocusRealTimeResultType Type,
bool Success,
int DeviceError,
int? Data,
DateTimeOffset ReceivedAt);
}
@@ -0,0 +1,11 @@
namespace HardwareControl.Core.FocusSystem
{
/// <summary>
/// 光谱仪同步延时参数(对应 Focus2SyncTimeParams),单位微秒。
/// </summary>
public sealed record FocusSyncTime(
ushort DelayUs,
ushort OffsetUs,
ushort HoldUs,
ushort HoldOffsetUs);
}
@@ -0,0 +1,169 @@
using System.Runtime.InteropServices;
namespace HardwareControl.Core.FocusSystem
{
/// <summary>
/// FocusSystem2.dll 的 P/Invoke 声明。
/// 所有结构体按 C 端 #pragma pack(1) 布局,字段顺序与 focus_system2.h 一致。
/// 除查询类函数外,命令函数内部已完成"组包→发送→收应答"。
/// </summary>
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);
/// <summary>读取 DLL 内部记录的最后一次错误描述;无错误或句柄为空时返回空串。</summary>
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;
}
}
/// <summary>系统信息,对应 Focus2SysInfo(15 字节载荷)。</summary>
[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;
}
/// <summary>实时应答,对应 Focus2RealTimeResp(3~5 字节载荷)。</summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct Focus2RealTimeResp
{
public byte Type;
public byte Sta;
public byte Error;
public ushort Data;
}
/// <summary>光谱仪同步参数,对应 Focus2SyncTimeParams(8 字节)。</summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct Focus2SyncTimeParams
{
public ushort DelayUs;
public ushort OffsetUs;
public ushort HoldUs;
public ushort HoldOffsetUs;
}
}
@@ -0,0 +1,25 @@
namespace HardwareControl.Core.FocusSystem
{
/// <summary>
/// 定焦系统配置,对应 appsettings.json 的 "FocusSystem" 节。
/// 串口优先从系统参数 focus_com 读取,未配置时才使用 <see cref="ComPort"/>。
/// </summary>
public sealed class FocusSystemOptions
{
public const string SectionName = "FocusSystem";
/// <summary>
/// 回退串口名(例如 "COM6"),仅当系统参数 focus_com 为空时使用。
/// </summary>
public string? ComPort { get; set; }
/// <summary>校准位移未显式传速度时使用的默认速度。</summary>
public ushort DefaultCalibrationSpeed { get; set; } = 50;
/// <summary>读取系统信息/运行结果的默认超时,单位毫秒。</summary>
public int ReadTimeoutMs { get; set; } = 1000;
/// <summary>读取失败时的重试间隔,单位毫秒。</summary>
public int ReadRetryIntervalMs { get; set; } = 100;
}
}
@@ -0,0 +1,43 @@
namespace HardwareControl.Core.FocusSystem
{
/// <summary>
/// 定焦系统上传的系统信息(0xF5)解析结果。
/// </summary>
/// <param name="FocusPosition">对焦校准位置。</param>
/// <param name="Error">设备端错误码:0=无错误,11=测距超时,12=超负极限,13=超正极限。</param>
/// <param name="ErrorDescription">错误码的中文描述。</param>
/// <param name="Location">滑台位置。</param>
/// <param name="LaserOnline">激光器是否在线。</param>
/// <param name="VoltageVolts">电源电压(V)。</param>
/// <param name="ExtendedVoltageVolts">扩展板电压(V)。</param>
/// <param name="HardwareVersion">硬件版本号。</param>
/// <param name="SoftwareVersion">软件版本号。</param>
/// <param name="StartTimeMinutes">开机时间(分钟)。</param>
/// <param name="SyncTime">同步延时。</param>
/// <param name="IsDebugMode">是否处于 Debug 模式(sys_sta 非 0)。</param>
/// <param name="ReceivedAt">本机解析到该帧的时间。</param>
public sealed record FocusSystemStatus(
int FocusPosition,
int Error,
string ErrorDescription,
int Location,
bool LaserOnline,
double VoltageVolts,
double ExtendedVoltageVolts,
int HardwareVersion,
int SoftwareVersion,
int StartTimeMinutes,
int SyncTime,
bool IsDebugMode,
DateTimeOffset ReceivedAt)
{
public static string DescribeError(int error) => error switch
{
0 => "无错误",
11 => "测距超时",
12 => "超负极限",
13 => "超正极限",
_ => $"未知错误({error})",
};
}
}
@@ -0,0 +1,14 @@
namespace HardwareControl.Core.FocusSystem
{
public class FocusSystemStatusChangedEventArgs : EventArgs
{
public bool IsConnected { get; }
public string Message { get; }
public FocusSystemStatusChangedEventArgs(bool isConnected, string message)
{
IsConnected = isConnected;
Message = message;
}
}
}