117 lines
4.3 KiB
C#
117 lines
4.3 KiB
C#
using System.Runtime.InteropServices;
|
||
|
||
namespace HardwareControl.Core.AxisPlatform
|
||
{
|
||
/// <summary>
|
||
/// AxisPlatform2.dll 的 P/Invoke 声明。
|
||
/// 所有结构体按 C 端 #pragma pack(1) 布局,字段顺序与 axis_platform2.h 一致。
|
||
/// </summary>
|
||
internal static class AxisPlatformNative
|
||
{
|
||
private const string DllName = "AxisPlatform2.dll";
|
||
|
||
// 错误码,对应 AXIS2_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 axis_init(
|
||
[MarshalAs(UnmanagedType.LPStr)] string comPort,
|
||
byte headType,
|
||
IntPtr limits);
|
||
|
||
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
||
internal static extern void axis_close(IntPtr handle);
|
||
|
||
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
||
internal static extern int axis_x_move(IntPtr handle, short distance, ushort speed, ushort accel);
|
||
|
||
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
||
internal static extern int axis_y_move(IntPtr handle, short distance, ushort speed, ushort accel);
|
||
|
||
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
||
internal static extern int axis_z_move(IntPtr handle, short distance, ushort speed, ushort accel);
|
||
|
||
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
||
internal static extern int axis_xyz_rel_move(
|
||
IntPtr handle,
|
||
ref Axis2MoveParams x,
|
||
ref Axis2MoveParams y,
|
||
ref Axis2MoveParams z);
|
||
|
||
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
||
internal static extern int axis_xyz_abs_move(
|
||
IntPtr handle,
|
||
ref Axis2MoveParams x,
|
||
ref Axis2MoveParams y,
|
||
ref Axis2MoveParams z);
|
||
|
||
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
||
internal static extern void axis_flush(IntPtr handle);
|
||
|
||
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
||
internal static extern int axis_read_upload(IntPtr handle, out Axis2UploadData data);
|
||
|
||
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
||
internal static extern IntPtr axis_get_error(IntPtr handle);
|
||
|
||
/// <summary>读取 DLL 内部记录的最后一次错误描述;无错误或句柄为空时返回空串。</summary>
|
||
internal static string GetError(IntPtr handle)
|
||
{
|
||
if (handle == IntPtr.Zero)
|
||
{
|
||
return string.Empty;
|
||
}
|
||
|
||
var pointer = axis_get_error(handle);
|
||
return pointer == IntPtr.Zero
|
||
? string.Empty
|
||
: Marshal.PtrToStringAnsi(pointer) ?? string.Empty;
|
||
}
|
||
}
|
||
|
||
/// <summary>限位参数,对应 Axis2Limits(6 × int32)。</summary>
|
||
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||
internal struct Axis2Limits
|
||
{
|
||
public int XPosLimit;
|
||
public int XNegLimit;
|
||
public int YPosLimit;
|
||
public int YNegLimit;
|
||
public int ZPosLimit;
|
||
public int ZNegLimit;
|
||
}
|
||
|
||
/// <summary>单轴运动参数,对应 Axis2MoveParams(6 字节)。</summary>
|
||
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||
internal struct Axis2MoveParams
|
||
{
|
||
public short Distance;
|
||
public ushort Speed;
|
||
public ushort Accel;
|
||
}
|
||
|
||
/// <summary>上传数据,对应 Axis2UploadData(13 字节载荷)。</summary>
|
||
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||
internal struct Axis2UploadData
|
||
{
|
||
public byte SysSta;
|
||
public short XPos;
|
||
public short YPos;
|
||
public short ZPos;
|
||
public byte XSta;
|
||
public byte YSta;
|
||
public byte ZSta;
|
||
public byte Error;
|
||
public byte HwVersion;
|
||
public byte SwVersion;
|
||
}
|
||
}
|