using System.Runtime.InteropServices; using System.Threading; namespace Model { public class MS_MB_API { static string IP = "192.168.0.8"; static bool s_link; static int c_id = 0; static Comm_Config config = new Comm_Config(); static byte[] bit_In = new byte[64]; static byte[] bit_Out = new byte[64]; static byte[] zero = new byte[64]; public static byte[] Inputs = new byte[64]; public static byte[] Outputs = new byte[64]; static Thread IO; static MS_MB_API() { IO = new Thread(IOUpdate); IO.IsBackground = true; IO.Start(); } public static void IOUpdate() { while (true) { Thread.Sleep(10); if (Connect()) { var errorCode = MS_ReadBits_X(c_id, 0, 64, ref bit_In[0]); if (errorCode != MS_MB_ERR_NONE) { s_link = false; return; } Inputs = bit_In; bit_Out = Outputs; errorCode = MS_WriteBits_Y(c_id, 0, 64, bit_Out); if (errorCode != MS_MB_ERR_NONE) { s_link = false; } } else { Inputs = zero; } } } public static bool Connect() { if (s_link == false) { config.enet.ip = IP; config.enet.port = 502; config.type = COMM_TYPE_ENET; if (MS_OpenComm(config, ref c_id) == MS_MB_ERR_NONE) { MS_CommSetSlave(c_id, 1); s_link = true; } } return s_link; } public bool Connect(string ip) { IP = ip; return Connect(); } public MS_MB_API() { } [StructLayout(LayoutKind.Sequential)] public struct MS_AIXS_INFO { public byte motion_state; public byte enable; public byte busy; public byte limit_positive; public byte limit_negative; public byte home_switch; public byte cmd_done; public byte[] reserve; public int position; public int velocity; public MS_AIXS_INFO(int dummy) { motion_state = 0; enable = 0; busy = 0; limit_positive = 0; limit_negative = 0; home_switch = 0; cmd_done = 0; reserve = new byte[9]; position = 0; velocity = 0; } } public const byte MotionState_Idle = 0; public const byte MotionState_Moving = 1; public const byte MotionState_Homing = 2; public const byte MotionState_Jogging = 3; public const int HomeMode_NegativeLimit = 1; //负限位查询 public const int HomeMode_PositiveLimit = 2; //正限位查询 public const int HomeMode_PositiveLimitHomeSw = 7; //正限位+零位查询 public const int HomeMode_NegativeLimitHomeSw = 11; //负限位+零位查询 public const int HomeMode_CurrentPos = 135; //当前位置值零位 [StructLayout(LayoutKind.Sequential)] public struct Serial_Comm { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)] public string portName; //填入串口号:"COMxx",xx为串口序号,如:"COM0","COM12","COM23" public uint baudrate; public ushort dataBits; //7,8 public ushort stopBits; //1,2 public ushort parity; //0-N,1-O,2-E public ushort rts; //0-disable,1-enable } [StructLayout(LayoutKind.Sequential)] public struct Enet_Comm { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)] public string ip; //填入IP地址,如:"192.168.0.8" public ushort port; } public const ushort COMM_TYPE_NONE = 0; public const ushort COMM_TYPE_SER = 1; public const ushort COMM_TYPE_ENET = 2; [StructLayout(LayoutKind.Sequential)] public struct Comm_Config { public ushort type; public ushort timeOut; //ms public int c_id; [MarshalAs(UnmanagedType.Struct)] public Serial_Comm serial; [MarshalAs(UnmanagedType.Struct)] public Enet_Comm enet; } //==== 通信 [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_OpenComm", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_OpenComm(Comm_Config config, ref int c_id); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_CommSetSlave", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_CommSetSlave(int c_id, int slave); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_CloseComm", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_CloseComm(int c_id); //==== 读位寄存器 [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_ReadBits_X", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_ReadBits_X(int c_id, ushort start, ushort num, ref byte pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_ReadBits_Y", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_ReadBits_Y(int c_id, ushort start, ushort num, ref byte pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_ReadBits_M", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_ReadBits_M(int c_id, ushort start, ushort num, ref byte pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_ReadBits_S", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_ReadBits_S(int c_id, ushort start, ushort num, ref byte pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_ReadBits_T", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_ReadBits_T(int c_id, ushort start, ushort num, ref byte pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_ReadBits_C", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_ReadBits_C(int c_id, ushort start, ushort num, ref byte pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_ReadBits_F", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_ReadBits_F(int c_id, ushort start, ushort num, ref byte pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_ReadBits_B", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_ReadBits_B(int c_id, ushort start, ushort num, ref byte pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_ReadBits_L", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_ReadBits_L(int c_id, ushort start, ushort num, ref byte pData); //==== 写位寄存器 [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_WriteBits_Y", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_WriteBits_Y(int c_id, ushort start, ushort num, byte[] pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_WriteBits_M", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_WriteBits_M(int c_id, ushort start, ushort num, byte[] pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_WriteBits_S", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_WriteBits_S(int c_id, ushort start, ushort num, byte[] pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_WriteBits_F", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_WriteBits_F(int c_id, ushort start, ushort num, byte[] pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_WriteBits_B", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_WriteBits_B(int c_id, ushort start, ushort num, byte[] pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_WriteBits_L", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_WriteBits_L(int c_id, ushort start, ushort num, byte[] pData); //==== 读字寄存器 [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_ReadWord_D", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_ReadWord_D(int c_id, ushort start, ushort num, ref ushort pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_ReadWord_R", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_ReadWord_R(int c_id, ushort start, ushort num, ref ushort pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_ReadWord_VD", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_ReadWord_VD(int c_id, ushort start, ushort num, ref ushort pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_ReadWord_RD", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_ReadWord_RD(int c_id, ushort start, ushort num, ref ushort pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_ReadWord_T", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_ReadWord_T(int c_id, ushort start, ushort num, ref ushort pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_ReadWord_C16", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_ReadWord_C16(int c_id, ushort start, ushort num, ref ushort pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_ReadWord_C32", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_ReadWord_C32(int c_id, ushort start, ushort num, ref uint pData); //==== 写字寄存器 [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_WriteWord_D", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_WriteWord_D(int c_id, ushort start, ushort num, ushort[] pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_WriteWord_R", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_WriteWord_R(int c_id, ushort start, ushort num, ushort[] pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_WriteWord_VD", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_WriteWord_VD(int c_id, ushort start, ushort num, ushort[] pData); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_WriteWord_RD", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_WriteWord_RD(int c_id, ushort start, ushort num, ushort[] pData); //==== 运动控制轴获取 [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionGetAxisCount", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionGetAxisCount(int c_id, ref ushort axis_cnt); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionGetAxisState", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionGetAxisState(int c_id, int axis_idx, ref byte state); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionGetAxisEnable", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionGetAxisEnable(int c_id, int axis_idx, ref byte enable); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionGetAxisBusy", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionGetAxisBusy(int c_id, int axis_idx, ref byte busy); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionGetAxisLimitPos", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionGetAxisLimitPos(int c_id, int axis_idx, ref byte limit_pos); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionGetAxisLimitNeg", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionGetAxisLimitNeg(int c_id, int axis_idx, ref byte limit_neg); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionGetAxisHomeSwitch", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionGetAxisHomeSwitch(int c_id, int axis_idx, ref byte home_sw); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionGetAxisCmdDone", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionGetAxisCmdDone(int c_id, int axis_idx, ref byte cmd_done); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionGetAxisPosition", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionGetAxisPosition(int c_id, int axis_idx, ref int position); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionGetAxisVelocity", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionGetAxisVelocity(int c_id, int axis_idx, ref int velocity); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionGetAxisInfo", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionGetAxisInfo(int c_id, int axis_idx, ref MS_AIXS_INFO axis_info); //==== 运动控制轴设置 [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionSetAxisEnable", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionSetAxisEnable(int c_id, int axis_idx, byte enable); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionSetAxisPosition", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionSetAxisPosition(int c_id, int axis_idx, int position); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionSetAxisMoveAbs", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionSetAxisMoveAbs(int c_id, int axis_idx, int pos, int vel, int acc_ms, int dec_ms); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionSetAxisMoveInc", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionSetAxisMoveInc(int c_id, int axis_idx, int dis, int vel, int acc_ms, int dec_ms); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionSetAxisMoveVel", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionSetAxisMoveVel(int c_id, int axis_idx, int vel, int acc_ms, int dec_ms); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionSetAxisHome", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionSetAxisHome(int c_id, int axis_idx, int home_mode, int vel_fast, int vel_slow, int acc_ms, int dec_ms, int offset); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionSetAxisJogging", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionSetAxisJogging(int c_id, int axis_idx, int vel, int acc_ms, int dec_ms); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionSetAxisStop", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionSetAxisStop(int c_id, int axis_idx); [DllImport("MS_ModbusApi.dll", EntryPoint = "MS_MotionSetAxisDirReverse", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public extern static int MS_MotionSetAxisDirReverse(int c_id, int axis_idx, byte reverse); //错误码 public const int MS_MB_ERR_NONE = 0; public const int MS_MB_ERR_CONFIG = 1000; public const int MS_MB_ERR_OPEN = 1001; public const int MS_MB_ERR_NODATA = 1002; public const int MS_MB_ERR_NOTFOUND = 1003; public const int MS_MB_ERR_MEM = 1004; public const int MS_MB_ERR_FUNCPARAM = 1005; public const int MS_MB_ERR_COMM = 1006; public const int MS_MB_ERR_AXIS_DISABLED = 1100; } }