在qt中调用C++dll一般都可以直接使用,但是在调用C#版dll时,就有些麻烦了。本文采用C++/cli封装C#的dll的方式。实现了qt调用C#dll的方法。
.h文件
#pragma once
#include //stl库
#include // stl中vector库
//dll生成文件定义该宏,在引用该dll文件中,取消该定义
#ifdef CNCNETLIB2CDLL
#define CNCNETLIB2C __declspec(dllexport)
#else
#define CNCNETLIB2C __declspec(dllimport)
#endif
namespace CNCNetLib2CPlus
{
/*6 IP Address Information*/
class CNCNETLIB2C CNCNetClassCPlus
{
public:
CNCNetClassCPlus();
virtual ~CNCNetClassCPlus();
//获取当前电脑可用IP地址
std::vector GetCurrentIPAddress();
//获取IP地址相关的其他信息
int GetNetConfig(std::string IPAddress, std::string& MACAddress, std::string& SubMask, std::string& Gateway, bool& IsDHCP);
//获取cnc的名称及IP
std::vector BroadcastGetCNC(std::string localIP, std::string subMask);
};
/*CNC information*/
class CNCNETLIB2C CNCInfoClassCPlus
{
public:
CNCInfoClassCPlus();
CNCInfoClassCPlus(std::string LocalIP, std::string CNCIP, int Timeout);
virtual ~CNCInfoClassCPlus();
/*7.Connection Setting*/
//设置本地IP
void SetLocalIP(std::string LocalIP);
void GetLocalIP(std::string& LocalIP);
//设置远程CNC-IP
void SetCNCIP(std::string CNCIP);
void GetCNCIP(std::string& CNCIP);
//设置连接超时时长ms
void SetTimeOut(int TimeOut);
void GetTimeOut(int& TimeOut);
//設定連線用資訊
int SetConnectInfo(std::string LocalIP, std::string CNCIP, int Timeout);
//建立連線
int Connect();
//取得連線狀態
bool IsConnect();
//中斷已建立的連線
void Disconnect();
/*8.權限資訊*/
//讀取目前網路連線最高權限等級
int READ_Current_Permission(unsigned short& PermissionLevel);
//檢查目前網路連線是否具備或高於輸入之權限等級
int Check_Current_Permission(unsigned short PermissionLevel);
//鎖定網路連線權限,鎖定後權限等級降為一般使用者
int Lock_Permission();
//取得輸入之權限等級
int UnLock_Permission(unsigned short PermissionLevel, std::string UserName, std::string Password);
//變更權限帳號與密碼
int Change_Password(unsigned short PermissionLevel, std::string OriginalUserName, std::string NewUserName, std::string OriginalPassword, std::string NewPassword);
//新增低於當前權限的帳號
int Permission_Account_Add(unsigned short PermissionCount, std::string UserName, std::string Password);
//刪除低於當前權限的帳號
int Permission_Account_Delete(unsigned short PermissionCount, std::string UserName, std::string Password);
//取得當前低於權限的所有帳號列表
int Permission_Account_List(unsigned short& PermissionCount, std::vector& PermissionLevel , std::vector& UserName, std::vector& Password);
/*9.基本資訊*/
//讀取 CNC 加工與報警狀態
int READ_CNC_Flags(unsigned char& IsProcessing, bool& IsAlarm);
//讀取 CNC 加工、報警與系統動作狀態
int READ_CNC_Flags(unsigned char& IsProcessing, bool& IsAlarm, unsigned char& RestartAct);
//讀取 CNC 基本資訊
int READ_CNC_Information(unsigned short Channel, unsigned int& Model, unsigned short& MaxChannels,
unsigned short& Series,std::string& NCVersion, unsigned short& ScrewUnit,
unsigned short& DisplayUnit, unsigned int& ApiVersion);
//讀取 Channel 基本資訊
int READ_Channel_Information(unsigned int& Channels, std::vector& AxesOfChannel, std::vector& ServoAxesofChannel,
std::vector& MaxAxisOfChannel, std::vector>& AxisNameOfChannel, std::vector& DecPointOfChannel);
//讀取 Channel 伺服軸軸數
int READ_Axis_Count(unsigned short Channels, unsigned short& AxisCount);
//讀取目標通道內已啟用主軸數量
int READ_Spindle_Count(unsigned short Channels, unsigned short& SpindleCount);
//讀取 CNC 狀態
int READ_Status(unsigned short Channel, std::string& MainProgram, std::string& CurrentProgram, std::string& MainProgramPath,
int& CurrentLine, int& MDICurrentLine, int& Mode, int& Status, bool& IsAlarm, bool& IsProcessing, unsigned char& RestartAct);
//讀取 CNC Motion 狀態資訊
int READ_NCMotion(unsigned short Channel, unsigned short Unit, unsigned int& SPSpeed, double& Feed,
short& SPLoad, double& SPActSpeed, double& ActFeed, unsigned int& DwellTime, short& SPToolNo,
unsigned short& MagaNo, unsigned short& CmdToolNo,unsigned short& StandbyToolNo, unsigned short& StandbyPot,
unsigned short& DCode, unsigned short& HCode, unsigned short& TCode, unsigned short& MCode,
std::vector& GCodeGroup, std::vector& GCodeGroupDecimal, unsigned short& FeedRate,
unsigned short& RPDRate,unsigned short& SpindleRate, double& JOGRate, unsigned short& MPGRate);
//讀取進給率 / 轉速
int READ_Feed_Spindle(unsigned short Channel, unsigned short Unit, double& CmdFeed, unsigned int& CmdSPSpeed, double& ActFeed, double& SPActSpeed);
//讀取 G Code Data
int READ_GCode(unsigned short Channel, std::vector& GCode);
//讀取其他 Code Data
int READ_OtherCode(unsigned short Channel, int& HCode, int& DCode, int& TCode, int& MCode);
//讀取 NC 主機名稱
int READ_CNC_HostName(std::string& HostName);
//寫入 NC 主機名稱
int WRITE_CNC_HostName(std::string HostName);
//讀取控制器軸回原點資訊
int READ_HomeStatus(unsigned short Channel , unsigned short& HomeStatus);
/*10.警報資訊*/
//讀取當前警報個數
int READ_Current_Alarm_Count(unsigned int& AlarmCount);
//讀取當前警報內容
int READ_Current_Alarm(unsigned short& AlarmCount, std::vector& AlarmCode,
std::vector& AlarmType, std::vector& AlarmDataLen,
std::vector>& AlarmData, std::vector& AlarmMsg,
std::vector& AlarmDateTime);
//讀取歷史警報個數
int READ_History_Alarm_Count(unsigned int& AlarmCount);
//讀取全部歷史警報訊息
int READ_History_Alarm_All(unsigned short& AlarmCount, std::vector& AlarmCode,
std::vector& AlarmType, std::vector& AlarmDataLen,
std::vector>& AlarmData, std::vector& AlarmMsg,
std::vector& AlarmDateTime);
//讀取歷史警報訊息
int READ_History_Alarm(unsigned short AlarmIndex, unsigned short AlarmLength,
unsigned short& AlarmCount, std::vector& AlarmCode,
std::vector& AlarmType, std::vector& AlarmDataLen,
std::vector>& AlarmData, std::vector& AlarmMsg,
std::vector& AlarmDateTime);
//清除歷史警報訊息
int CLEAR_History_Alarm();
/*11 伺服資訊*/
//讀取進給軸負載峰值
int READ_Servo_Load_Peak(unsigned short Channel, unsigned short& AxisCount,
std::vector& AxisNr, std::vector& Result, std::vector& AxisValue);
//讀取主軸負載峰值
int READ_Spindle_Load_Peak(unsigned short Channel, unsigned short& AxisCount,
std::vector& AxisNr, std::vector& Result, std::vector& AxisValue);
//讀取進給軸負載
int READ_Servo_Load(unsigned short Channel, unsigned short& AxisCount,
std::vector& AxisNr, std::vector& Result, std::vector& AxisValue);
//讀取主軸負載
int READ_Spindle_Load(unsigned short Channel, unsigned short& AxisCount,
std::vector& AxisNr, std::vector& Result, std::vector& AxisValue);
//讀取進給軸轉速
int READ_Servo_Speed(unsigned short Channel, unsigned short& AxisCount,
std::vector& AxisNr, std::vector& Result, std::vector& AxisValue);
//讀取主軸轉速
int READ_Spindle_Speed(unsigned short Channel, unsigned short& AxisCount,
std::vector& AxisNr, std::vector& Result, std::vector& AxisValue);
//讀取進給軸溫度
int READ_Servo_Temperature(unsigned short Channel, unsigned short& AxisCount,
std::vector& AxisNr, std::vector& Result, std::vector& AxisValue);
//讀取主軸溫度
int READ_Spindle_Temperature(unsigned short Channel, unsigned short& AxisCount,
std::vector& AxisNr, std::vector& Result, std::vector& AxisValue);
/*12 座標資訊*/
//讀取 CNC 各軸座標
int READ_Position(unsigned short Channel, unsigned short Unit, std::vector& AxisName,
std::vector& CoordMech, std::vector& CoordAbs, std::vector& CoordRel,
std::vector& CoordRes, std::vector& CoordOffset);
//讀取選擇的座標類型的各軸座標
int READ_Select_Position(unsigned short Channel, unsigned short Unit, unsigned short CoordType,std::vector& CoorCoordArray);
//清除選取軸相對座標
int CLEAR_Relative_Coordinary(unsigned short Channel, unsigned short AxisNr);
//清除所有軸相對座標
int CLEAR_Relative_Coordinary_All(unsigned short Channel);
/*13 工件座標*/
//讀取工件座標標題名稱
int READ_GCoordinary_Title(std::vector& GCoordinaryTitle);
//讀取工件座標資料
int READ_GCoordinary(unsigned short Channel, unsigned short Unit, unsigned short Index, unsigned short Length, std::vector>& GCoordinaryArray);
//写入工件座標資料
int WRITE_GCoordinary(unsigned short Channel, unsigned short Unit, unsigned short Index, unsigned short Length, std::vector> GCoordinaryArray);
//讀取偏移座標資料
int READ_Offset_Coordinary(unsigned short Channel, unsigned short Unit, std::vector& CoordinaryArray);
//写入偏移座標資料
int WRITE_Offset_Coordinary(unsigned short Channel, unsigned short Unit, std::vector CoordinaryArray);
/*14 刀具資訊*/
//讀取刀具標題列
int READ_Tool_Title(unsigned short Channel, std::vector& ToolTitle);
//讀取刀具總筆數
int READ_Tool_Count(unsigned short Channel, unsigned short& ToolCount);
//讀取刀具資訊
int READ_Tool(unsigned short Channel, unsigned short Unit, unsigned short Index, unsigned short Length, unsigned short& SeriesType, std::vector>& ToolData);
//写入刀具資訊
int WRITE_Tool(unsigned short Channel, unsigned short Unit, unsigned short Index, unsigned short Length, std::vector> ToolData);
/*15 刀庫資訊*/
// 讀取刀庫資訊
int READ_Magazine_Info(unsigned short Channel, unsigned short MagaID, unsigned short& ToolNum, unsigned short& CMDToolID,
unsigned short& StandbyToolID, unsigned short& StandbyPotID, short& SPToolID, std::vector& ToolID);
//WRITE_Magazine_Info: 寫入刀庫資訊
int WRITE_Magazine_Info(unsigned short Channel, unsigned short MagaID, unsigned short PotID, short ToolID);
//RESET_Magazine_Info: 重置刀庫
int RESET_Magazine_Info(unsigned short Channel, unsigned short MagaID);
//WRITE_Lock_Magazine:刀庫之刀套封鎖
int WRITE_Lock_Magazine(unsigned short Channel, unsigned short MagaID, unsigned short PotID);
//WRITE_Unlock_Magazine:刀庫之刀套解鎖
int WRITE_Unlock_Magazine(unsigned short Channel, unsigned short MagaID, unsigned short PotID);
/*16 加工資訊*/
//16.1 READ_Process_Time : 讀取加工時間
int READ_Process_Time(unsigned short Channel, unsigned int& TotalWorkTime, unsigned int& SingleWorkTime);
//16.2 CLEAR_Process_Time : 清除加工時間
int CLEAR_Process_Time(unsigned short Channel);
//16.3 READ_Part_Count : 讀取加工數
int READ_Part_Count(unsigned short Channel, unsigned int& target_part_count, unsigned int& finish_part_count);
//16.4 WRITE_Part_Count : 寫入加工數
int WRITE_Part_Count(unsigned short Channel, unsigned int target_part_count, unsigned int finish_part_count);
//16.5 CLEAR_Part_Count : 清除完成加工數
int CLEAR_Part_Count(unsigned short Channel);
/*17. 巨集變數*/
//17.1 READ_Macro_Variable : 讀取巨集變數數值
int READ_Macro_Variable(unsigned short Channel, unsigned int Type, unsigned int StartIndex, unsigned int Length, std::vector& MacroValues);
//17.2 READ_Macro_Variable_By_ID : 根據巨集變數 ID 讀取巨集變數數值
int READ_Macro_Variable_By_ID(unsigned short Channel, unsigned int MacroID, double& MacroValue);
//17.3 WRITE_Macro_Variable : 寫入巨集變數數值
int WRITE_Macro_Variable(unsigned short Channel, unsigned int Type, unsigned int StartIndex, unsigned int Length, std::vector MacroValues);
//17.4 WRITE_Macro_Variable_By_ID : 根據巨集變數 ID 寫入巨集變數數值
int WRITE_Macro_Variable_By_ID(unsigned short Channel, unsigned int MacroID, double MacroValue);
//17.5 WRITE_Macro_Variable_NoPerm : 寫入巨集變數數值,且無權限與加工中限制
int WRITE_Macro_Variable_NoPerm(unsigned short Channel, unsigned int Type, unsigned int StartIndex, unsigned int Length, std::vector MacroValues);
/*18.MLC 變數*/
//18.1 READ_MLC_Address_Buffer_Size : 取得 MLC 變數需要的 Buffer Size
int READ_MLC_Address_Buffer_Size(unsigned int DevType, unsigned int DevStart, unsigned int DevNum);
//18.2 READ_MLC_Address : 讀取 MLC 變數數值
int READ_MLC_Address(unsigned int DevType, unsigned int DevStart, unsigned int DevNum, std::vector& DevValue);
//18.3 WRITE_MLC_Address : 寫入 MLC 變數數值
int WRITE_MLC_Address(unsigned int DevType, unsigned int DevStart, unsigned int DevNum, std::vector DevValue);
//18.4 WRITE_MLC_Address_NoPerm : 寫入 MLC 變數數值,且無權限與加工中限制
int WRITE_MLC_Address_NoPerm(unsigned int DevType, unsigned int DevStart, unsigned int DevNum, std::vector DevValue);
/*19 參數資訊*/
//19.1 READ_Single_CNC_Parameter : 讀取單筆 CNC 參數
int READ_Single_CNC_Parameter(unsigned short Channel, unsigned short NGroup, unsigned int ParaID,
short SubItem, short AXNr, short& SubGroupID, short& DataType, unsigned short& DataSize, std::vector& Data);
//19.2 WRITE_Single_CNC_Parameter : 寫入單筆 CNC 參數 byte[32]
int WRITE_Single_CNC_Parameter(unsigned short Channel, unsigned short NGroup, unsigned int ParaID,
short SubItem, short AXNr, std::vector Data);
/*20 S 設定*/
//20.1 READ_S_Limit : 讀取主軸轉速極限值
int READ_S_Limit(unsigned short Channel, unsigned short SPIndex, int& MinOfS, int& MaxOfS);
//20.2 WRITE_S_Setting : 設定主軸轉速
int WRITE_S_Setting(unsigned short Channel, unsigned short SPIndex, unsigned int SPSpeed);
/*21 系統資訊*/
//21.1 READ_System_Time : 讀取系統日期時間
int READ_System_Time(std::string& SystemTime);
//21.2 Reset_System : 系統重置
int Reset_System(unsigned short Channel);
//21.3 READ_System_Variable : 讀取系統變數
int READ_System_Variable(unsigned short Channel, unsigned short& SysVarNum,
unsigned short& AxisVarNum, std::vector& SysVarChannel,
std::vector& SysVarID,std::vector& SysVarType,
std::vector>& SysVarValue, std::vector& AxisVarChannel,
std::vector& AxisNum, std::vector& AxisVarID,
std::vector& AxisVarType, std::vector>& AxisID,
std::vector>>& AxisVarValue);
//21.4 READ_User_Variable : 讀取用戶變數
int READ_User_Variable(unsigned short& VarNum, std::vector& VarRegAddr, std::vector& VarValue);
//21.5 WRITE_User_Variable_Register : 寫入用戶變數暫存器數值
int WRITE_User_Variable_Register(unsigned short VarID, unsigned short VarRegAddr);
//21.6 WRITE_User_Variable_Value : 寫入用戶變數數值
int WRITE_User_Variable_Value(unsigned short VarID, unsigned short VarRegValue);
//21.7 DELETE_User_Variable: 刪除用戶變數
int DELETE_User_Variable(unsigned short VarID);
//21.8 READ_Equip_Variable : 讀取設備變數
int READ_Equip_Variable(unsigned short& VarNum, std::vector& VarRegAddr, std::vector& VarRegValue);
//21.9 WRITE_Equip_Variable_Register : 寫入設備變數暫存器數值
int WRITE_Equip_Variable_Register(unsigned short VarID, unsigned short VarRegAddr);
//21.10 WRITE_Equip_Variable_Value
int WRITE_Equip_Variable_Value(unsigned short VarID, unsigned short SetRegValue);
//21.11 DELETE_Equip_Variable : 刪除設備變數
int DELETE_Equip_Variable(unsigned short VarID);
//21.12 READ_System_Status : 讀取系統狀態
int READ_System_Status(unsigned short& ParaCount, std::vector& ParaID,
std::vector& ParaName, std::vector& DataType,
std::vector& DataSize, std::vector>& Data);
//21.13 READ_FW_Version : 讀取韌體序號
int READ_FW_Version(unsigned short& ParaCount, std::vector& ParaID,
std::vector& ParaName, std::vector& DataType,
std::vector& DataSize, std::vector>& Data);
/*22 通道設定*/
//22.1 READ_Channel_Setting : 讀取通道設定
int READ_Channel_Setting(unsigned short Channel, unsigned int& SeriesType, unsigned short& AxisNum,
std::vector& AxisID, std::vector& IsEnable,
std::vector& AxisTypeID, std::vector& SpindleID,
std::vector& EtherCATPort, std::vector& SerialPort,
std::vector& AxisName);
//22.2 WRITE_Single_Channel_Setting : 寫入單筆通道設定
int WRITE_Single_Channel_Setting(unsigned short Channel,unsigned short AxisID, bool IsEnable,
unsigned char AxisTypeID, unsigned char SpindleID,unsigned short EtherCATPort, unsigned short SerialPort,
std::string AxisName);
//22.3 WRITE_Channel_Setting : 寫入全部通道設定
int WRITE_Channel_Setting(unsigned short Channel, unsigned int& SeriesType, unsigned short AxisNum,
std::vector AxisID, std::vector IsEnable,
std::vector AxisTypeID, std::vector SpindleID,
std::vector EtherCATPort, std::vector SerialPort,
std::vector AxisName);
/*23 EIO 設定*/
//23.1 READ_Single_EIO_Setting : 根據 EIO ID 讀取單筆 EIO 設定值
int READ_Single_EIO_Setting(unsigned short ID, std::string& Type,
unsigned short& Port, unsigned int& StartAddress, unsigned int& Polarity, bool& IsDisc);
//23.2 READ_EIO_Setting : 讀取 EIO 設定
int READ_EIO_Setting(unsigned short& IONum, std::vector& ID,
std::vector& Type, std::vector& Port,
std::vector& StartAddress, std::vector& Polarity,
std::vector& IsDisc);
//23.3 WRITE_Single_EIO_Setting : 根據 EIO ID 寫入單筆 EIO 設定
int WRITE_Single_EIO_Setting(unsigned short ID, unsigned short Port, unsigned int StartAddress, unsigned int Polarity, bool IsDisc);
//23.4 WRITE_EIO_Setting : 寫入 EIO 設定
int WRITE_EIO_Setting(unsigned short IONum, std::vector ID,std::vector Port,
std::vector StartAddress, std::vector Polarity,std::vector IsDisc);
//23.5 READ_EIO_Status : 讀取 EIO 連線狀態
int READ_EIO_Status(unsigned short& IONum, std::vector& IsConnected);
/*24 檔案程式資訊*/
//24.1 READ_Disk_Quota : 讀取磁碟剩餘空間
int READ_Disk_Quota(char DiskType, int& DiskQuota);
//24.2 READ_Directory_List : 讀取磁碟目錄清單
int READ_Directory_List(std::string Path, std::vector>& FileList);
//24.3 UPLOAD_File : 上傳檔案至 CNC
int UPLOAD_File(std::string SrcFilePath, std::string DstFilePath, unsigned int& Percent);
//24.4 DOWNLOAD_File : 從 CNC 下載檔案
int DOWNLOAD_File(std::string SrcFilePath, std::string DstFilePath, unsigned int& Percent);
//24.5 DELETE_file : 刪除 CNC 上檔案
int DELETE_file(std::string DeleteFilePath);
//24.6 RENAME_File : 修改 CNC 上檔案名稱
int RENAME_File(std::string SrcFilePath, std::string DstFilePath);
//24.7 MAKE_Directory : CNC 上建立目錄
int MAKE_Directory(std::string PathOfDirectory);
//24.8 REMOVE_Directory : CNC 上移除目錄
int REMOVE_Directory(std::string PathOfDirectory);
//24.9 WRITE_NC_Main : 設定加工程式為主程式
int WRITE_NC_Main(unsigned short Channel, std::string MainFilePath);
//24.10 READ_NC_Pointer : 讀取 NC 執行行號
int READ_NC_Pointer(unsigned short Channel, int& LineNo, int& MDILineNo);
//24.11 READ_Preview_Code : 讀取 NC 預讀程式內容
int READ_Preview_Code(unsigned short Channel, unsigned short CodeCount,
std::vector& LineNo, std::vector& LineContent);
//24.12 READ_Current_Code : 讀取 NC 當前程式內容
int READ_Current_Code(unsigned short Channel, unsigned short CodeCount,
std::vector& LineNo, std::vector& LineContent);
/*25 斷點搜尋*/
//25.1 READ_CodeSearch_LineNo : 讀取斷點搜尋行號
int READ_CodeSearch_LineNo(unsigned short Channel, int& LineNo);
//25.2 READ_CodeSearch_Position : 讀取斷點搜尋座標
int READ_CodeSearch_Position(unsigned short Channel, unsigned short Unit, unsigned short& AxisNum,
std::vector& AxisID, std::vector& CoordMech,
std::vector& CoordAbs, std::vector& CoordRel,
std::vector& CoordRes, std::vector& CoordOffset);
//25.3 WRITE_CodeSearch_Execution : 執行斷點搜尋
int WRITE_CodeSearch_Execution(unsigned short Channel, std::string SearchStr);
//25.4 READ_CodeSearch_Status :讀取斷點搜尋狀態
int READ_CodeSearch_Status(unsigned short Channel);
/*26 手動輸入*/
//26.1 WRITE_MDI_Load : MDI 載入
int WRITE_MDI_Load(unsigned short Channel, unsigned short Cursor, std::vectorContent);
//26.2 READ_MDI_code : 讀取 MDI 執行程式的當前行號與內容
int READ_MDI_code(unsigned short Channel, unsigned int& LineNo, std::vector& Content);
//26.3 CLEAR_MDI: MDI 清除
int CLEAR_MDI(unsigned short Channel);
//26.4 READ_MDI_Current_Code : 讀取當前 MDI 執行程式的行號與內容
int READ_MDI_Current_Code(unsigned short Channel, unsigned short CodeCount,
std::vector& LineNo, std::vectorContent);
//26.5 READ_MDI_Current_File: 讀取 MDI 當前執行的檔案名稱
int READ_MDI_Current_File(unsigned short Channel, std::string& FileName);
/*27 伺服監控*/
//27.1 READ_Servo_Monitor: 伺服監控
int READ_Servo_Monitor(unsigned short& Num, std::vector& Channel,
std::vector& AxisID, std::vector& IsConnect,
std::vector& IsServoOn, std::vector& Load,
std::vector& Peak, std::vector& MechCoord,
std::vector& IsHome, std::vector& AbsHomeSet,
std::vector& EncoderType);
//27.2 WRITE_Home_Setting : 絕對歸零設定
int WRITE_Home_Setting(unsigned short Channel, unsigned short AxisID);
/*28 其他函式*/
//28.1 READ_SP_InterMem : 讀取軟體面板內部記憶體
int READ_SP_InterMem(unsigned short MemType, unsigned short Offset,
unsigned short Length, std::vector& MemValue);
//28.2 WRITE_SP_InterMem: 寫入軟體面板內部記憶體
int WRITE_SP_InterMem(unsigned short MemType, unsigned short Offset,
unsigned short Length, std::vector MemValue);
//28.3 READ_SP_BarCodeData: 讀取面板 BarCode 資料
int READ_SP_BarCodeData(unsigned short BarcodeIndex, unsigned short Length, std::vector& BarcodeValue);
//28.4 WRITE_SP_BarCodeData: 寫入軟體面板 BarCode 資料
int WRITE_SP_BarCodeData(unsigned short BarcodeIndex, unsigned short Length, std::vector BarcodeValue);
/*29 通用函式*/
//29.1 TransferUnit : 公英制數值轉換
int TransferUnit(float value, unsigned short Unit, unsigned short Mode, float& TransferValue);
int TransferUnit(double value, unsigned short Unit, unsigned short Mode, double& TransferValue);
//29.3 TransferToString:數值轉換字串
std::string TransferToString(float value, unsigned short DecPoint);
std::string TransferToString(double value, unsigned short DecPoint);
};
}
cpp文件
#using ".\\CNCNetLib2.dll" 该行为加载 C#版dll过程 ,需将 对应dll放置在程序 目录下
在处理托管类时,由于托管类 在C++版 中不能直接 调用,所以得新建一个非托管类 ,通过 非托管类 来调用 该托管类 ,
C#中,当前类对象 调用 了 其他的类对象,并且这个类对象 是单例 的。这时候就需要把这个类对象封装为非托管的静态对象(纯C++版的静态对象)然后在不同的类中调用。
关键点:
marshal_as
marshal_as
String^ vNCVersion=gcnew String(""); 定义一个托管字符串
C++/cli说明请参考微软官方
托管类型 (C++/CLI) | Microsoft Learn
STL/CLR 库参考 | Microsoft Learn
marshal_as | Microsoft Learn
如何:使用 gcnew 创建值类型并使用隐式装箱 | Microsoft Learn
如何:在 C++/CLI 中使用数组 | Microsoft Learn
#include "pch.h"
#include "CNCNetLib2C.h"
#using ".\\CNCNetLib2.dll"
#include //托管数据与非托管数据转换
using namespace CNCNetLib2;
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace msclr::interop;
using namespace CNCNetLib2CPlus;
//定义一个托管类,再将CNCInfoClass对象定义为静态对象
public ref class CNCInfoManage abstract sealed
{
public:
static CNCInfoClass^ obj;
};
CNCNetClassCPlus::CNCNetClassCPlus()
{
}
CNCNetClassCPlus::~CNCNetClassCPlus()
{
}
std::vector CNCNetClassCPlus::GetCurrentIPAddress()
{
std::vector vectorIP; //非托管数组
vectorIP.clear();
cli::array^ Ip; //托管数组
CNCNetLib2::CNCNetClass obj;
Ip = obj.GetCurrentIPAddress();
if (Ip)
{
int iCount = Ip->Length;
for (int i = 0; i < iCount; ++i)
{
auto s = Ip[i];
vectorIP.push_back(marshal_as(s)); //将托管数组元素保存到非托管数组 中
}
}
return vectorIP;
}
int CNCNetClassCPlus::GetNetConfig(std::string IPAddress, std::string& MACAddress, std::string& SubMask, std::string& Gateway, bool& IsDHCP)
{
std::vector vectorCNC;
vectorCNC.clear();
CNCNetLib2::CNCNetClass obj;
String^ IPAddress2 = marshal_as(IPAddress); //托管字符串,非托管字符转托管字符串
String^ MACAddress2=gcnew String("");
String^ SubMask2 = gcnew String("");
String^ Gateway2 = gcnew String("");
bool IsDHCP2;
int iRtn = obj.GetNetConfig(IPAddress2, MACAddress2, SubMask2, Gateway2, IsDHCP2);
if (!IPAddress2|| !SubMask2 ||!SubMask2 || !Gateway2)
{
return iRtn;
}
MACAddress = marshal_as(MACAddress2);
SubMask = marshal_as(SubMask2);
Gateway = marshal_as(Gateway2);
IsDHCP = IsDHCP2;
return iRtn;
}
std::vector CNCNetClassCPlus::BroadcastGetCNC(std::string localIP, std::string subMask)
{
std::vector vectorCNC;
vectorCNC.clear();
CNCNetLib2::CNCNetClass obj;
cli::array^ nameCnc = obj.BroadcastGetCNC(marshal_as(localIP), marshal_as(subMask));
if (nameCnc)
{
int iCount = nameCnc->Length;
for (int i = 0; i < iCount; ++i)
{
auto s = nameCnc[i];
vectorCNC.push_back(marshal_as(s));
}
}
return vectorCNC;
}
CNCInfoClassCPlus::CNCInfoClassCPlus()
{
if (!CNCInfoManage::obj)
CNCInfoManage::obj = gcnew CNCInfoClass();
}
CNCInfoClassCPlus::CNCInfoClassCPlus(std::string LocalIP, std::string CNCIP, int Timeout)
{
if (!CNCInfoManage::obj)
{
String^ StrLocalIp = marshal_as(LocalIP);
String^ StrCNCIP = marshal_as(CNCIP);
CNCInfoManage::obj = gcnew CNCInfoClass(StrLocalIp, StrCNCIP, Timeout);
}
}
CNCInfoClassCPlus::~CNCInfoClassCPlus()
{
}
//设置本地IP
void CNCInfoClassCPlus::SetLocalIP(std::string LocalIP)
{
CNCInfoManage::obj->LocalIPAddress = marshal_as(LocalIP);
}
void CNCInfoClassCPlus::GetLocalIP(std::string& LocalIP)
{
if (CNCInfoManage::obj->LocalIPAddress)
{
LocalIP = marshal_as(CNCInfoManage::obj->LocalIPAddress);
}
}
//设置远程CNC-IP
void CNCInfoClassCPlus::SetCNCIP(std::string CNCIP)
{
CNCInfoManage::obj->RemoteIPAddress = marshal_as(CNCIP);
}
void CNCInfoClassCPlus::GetCNCIP(std::string& CNCIP)
{
if (CNCInfoManage::obj->RemoteIPAddress)
{
CNCIP = marshal_as(CNCInfoManage::obj->RemoteIPAddress);
}
}
//设置连接超时时长ms
void CNCInfoClassCPlus::SetTimeOut(int TimeOut)
{
CNCInfoManage::obj->Timeout = TimeOut;
}
void CNCInfoClassCPlus::GetTimeOut(int& TimeOut)
{
TimeOut = CNCInfoManage::obj->Timeout;
}
//設定連線用資訊
int CNCInfoClassCPlus::SetConnectInfo(std::string LocalIP, std::string CNCIP, int Timeout)
{
String^ StrLocalIp= marshal_as(LocalIP);
String^ StrCNCIP = marshal_as(CNCIP);
int ret = CNCInfoManage::obj->SetConnectInfo(StrLocalIp, StrCNCIP, Timeout);
return ret;
}
int CNCInfoClassCPlus::Connect()
{
int ret = CNCInfoManage::obj->Connect();
return ret;
}
//取得連線狀態
bool CNCInfoClassCPlus::IsConnect()
{
bool ret = CNCInfoManage::obj->IsConnect();
return ret;
}
//中斷已建立的連線
void CNCInfoClassCPlus::Disconnect()
{
CNCInfoManage::obj->Disconnect();
}
/*權限資訊*/
//讀取目前網路連線最高權限等級
int CNCInfoClassCPlus::READ_Current_Permission(unsigned short& PermissionLevel)
{
unsigned short vPermissionLevel{0};
int ret= CNCInfoManage::obj->READ_Current_Permission(vPermissionLevel);
PermissionLevel = vPermissionLevel;
return ret;
}
//檢查目前網路連線是否具備或高於輸入之權限等級
int CNCInfoClassCPlus::Check_Current_Permission(unsigned short PermissionLevel)
{
return CNCInfoManage::obj->Check_Current_Permission(PermissionLevel);
}
//鎖定網路連線權限,鎖定後權限等級降為一般使用者
int CNCInfoClassCPlus::Lock_Permission()
{
return CNCInfoManage::obj->Lock_Permission();
}
//取得輸入之權限等級
int CNCInfoClassCPlus::UnLock_Permission(unsigned short PermissionLevel, std::string UserName, std::string Password)
{
String^ StrUserName = marshal_as(UserName);
String^ StrPassword = marshal_as(Password);
return CNCInfoManage::obj->UnLock_Permission(PermissionLevel, StrUserName, StrPassword);
}
//變更權限帳號與密碼
int CNCInfoClassCPlus::Change_Password(unsigned short PermissionLevel, std::string OriginalUserName, std::string NewUserName, std::string OriginalPassword, std::string NewPassword)
{
String^ StrOriginalUserName = marshal_as(OriginalUserName);
String^ StrNewUserName = marshal_as(NewUserName);
String^ StrOriginalPassword = marshal_as(OriginalPassword);
String^ StrNewPassword = marshal_as(NewPassword);
return CNCInfoManage::obj->Change_Password(PermissionLevel, StrOriginalUserName, StrNewUserName, StrOriginalPassword, StrNewPassword);
}
//新增低於當前權限的帳號
int CNCInfoClassCPlus::Permission_Account_Add(unsigned short PermissionCount, std::string UserName, std::string Password)
{
String^ StrUserName = marshal_as(UserName);
String^ StrPassword = marshal_as(Password);
return CNCInfoManage::obj->Permission_Account_Add(PermissionCount, StrUserName, StrPassword);
}
//刪除低於當前權限的帳號
int CNCInfoClassCPlus::Permission_Account_Delete(unsigned short PermissionCount, std::string UserName, std::string Password)
{
String^ StrUserName = marshal_as(UserName);
String^ StrPassword = marshal_as(Password);
return CNCInfoManage::obj->Permission_Account_Delete(PermissionCount, StrUserName, StrPassword);
}
//取得當前低於權限的所有帳號列表
int CNCInfoClassCPlus::Permission_Account_List(unsigned short& PermissionCount, std::vector& PermissionLevel, std::vector& UserName, std::vector& Password)
{
PermissionLevel.clear();
UserName.clear();
Password.clear();
cli::array^ vPermissionLevel;
cli::array^ vUserName;
cli::array^ vPassword;
int ret = CNCInfoManage::obj->Permission_Account_List(PermissionCount, vPermissionLevel, vUserName, vPassword);
//托管数组转非托管数组
if (!vPermissionLevel || !vUserName || !vPassword)
{
return ret;
}
int iCount_vPermissionLevel = vPermissionLevel->Length;
for (int i = 0; i < iCount_vPermissionLevel; ++i)
{
auto s = vPermissionLevel[i];
PermissionLevel.push_back(s);
}
int iCount_vUserName = vUserName->Length;
for (int i = 0; i < iCount_vUserName; ++i)
{
auto s = vUserName[i];
UserName.push_back(marshal_as(s));
}
int iCount_vPassword = vPassword->Length;
for (int i = 0; i < iCount_vPassword; ++i)
{
auto s = vPassword[i];
Password.push_back(marshal_as(s));
}
return ret;
}
//讀取 CNC 加工與報警狀態
int CNCInfoClassCPlus::READ_CNC_Flags(unsigned char& IsProcessing, bool& IsAlarm)
{
return CNCInfoManage::obj->READ_CNC_Flags(IsProcessing, IsAlarm);
}
//讀取 CNC 加工、報警與系統動作狀態
int CNCInfoClassCPlus::READ_CNC_Flags(unsigned char& IsProcessing, bool& IsAlarm, unsigned char& RestartAct)
{
return CNCInfoManage::obj->READ_CNC_Flags(IsProcessing, IsAlarm, RestartAct);
}
//讀取 CNC 基本資訊
int CNCInfoClassCPlus::READ_CNC_Information(unsigned short Channel, unsigned int& Model, unsigned short& MaxChannels,
unsigned short& Series, std::string& NCVersion, unsigned short& ScrewUnit,unsigned short& DisplayUnit, unsigned int& ApiVersion)
{
unsigned int vModel{0};
unsigned short vMaxChannels{0};
unsigned short vSeries{0};
String^ vNCVersion=gcnew String("");
unsigned short vScrewUnit{0};
unsigned short vDisplayUnit{0};
unsigned int vApiVersion{0};
int ret=CNCInfoManage::obj->READ_CNC_Information(Channel, vModel, vMaxChannels, vSeries, vNCVersion,
vScrewUnit, vDisplayUnit, vApiVersion);
if (!vNCVersion)
{
return ret;
}
Model = vModel;
MaxChannels = vMaxChannels;
Series = vSeries;
NCVersion = marshal_as(vNCVersion);
ScrewUnit=vScrewUnit;
DisplayUnit=vDisplayUnit;
ApiVersion=vApiVersion;
return ret;
}
int CNCInfoClassCPlus::READ_Channel_Information(unsigned int& Channels, std::vector& AxesOfChannel, std::vector& ServoAxesofChannel,
std::vector& MaxAxisOfChannel, std::vector>& AxisNameOfChannel, std::vector& DecPointOfChannel)
{
AxesOfChannel.clear();
ServoAxesofChannel.clear();
MaxAxisOfChannel.clear();
AxisNameOfChannel.clear();
DecPointOfChannel.clear();
cli::array^ vAxesOfChannel;
cli::array^ vServoAxesofChannel;
cli::array^ vMaxAxisOfChannel;
cli::array^ vAxisNameOfChannel;
cli::array^ vDecPointOfChannel;
int ret = CNCInfoManage::obj->READ_Channel_Information(Channels, vAxesOfChannel,
vServoAxesofChannel, vMaxAxisOfChannel, vAxisNameOfChannel, vDecPointOfChannel);
if (!vAxesOfChannel|| !vServoAxesofChannel || !vMaxAxisOfChannel
|| !vAxisNameOfChannel || !vDecPointOfChannel )
{
return ret;
}
for each(auto i in vAxesOfChannel)
{
AxesOfChannel.push_back(i);
}
for each (auto i in vServoAxesofChannel)
{
ServoAxesofChannel.push_back(i);
}
for each (auto i in vMaxAxisOfChannel)
{
MaxAxisOfChannel.push_back(i);
}
for (int i = 0; i < vAxisNameOfChannel->GetLength(0); ++i)
{
std::vector AxisName;
AxisName.clear();
for (int j = 0; j < vAxisNameOfChannel->GetLength(1); ++j)
{
auto Param0 = vAxisNameOfChannel[i, j];
AxisName.push_back(marshal_as(Param0));
}
AxisNameOfChannel.push_back(AxisName);
}
for each (auto i in vDecPointOfChannel)
{
DecPointOfChannel.push_back(i);
}
return ret;
}
//讀取 Channel 伺服軸軸數
int CNCInfoClassCPlus::READ_Axis_Count(unsigned short Channels, unsigned short& AxisCount)
{
int ret = CNCInfoManage::obj->READ_Axis_Count(Channels, AxisCount);
return ret;
}
//讀取目標通道內已啟用主軸數量
int CNCInfoClassCPlus::READ_Spindle_Count(unsigned short Channels, unsigned short& SpindleCount)
{
int ret = CNCInfoManage::obj->READ_Spindle_Count(Channels, SpindleCount);
return ret;
}
//讀取 CNC 狀態
int CNCInfoClassCPlus::READ_Status(unsigned short Channel, std::string& MainProgram, std::string& CurrentProgram, std::string& MainProgramPath,
int& CurrentLine, int& MDICurrentLine, int& Mode, int& Status, bool& IsAlarm, bool& IsProcessing, unsigned char& RestartAct)
{
String^ vMainProgram = gcnew String("");
String^ vCurrentProgram = gcnew String("");
String^ vMainProgramPath= gcnew String("");
int ret = CNCInfoManage::obj->READ_Status(Channel, vMainProgram, vCurrentProgram, vMainProgramPath,
CurrentLine, MDICurrentLine, Mode, Status, IsAlarm, IsProcessing, RestartAct);
if (!vMainProgram|| !vCurrentProgram|| !vMainProgramPath)
{
return ret;
}
MainProgram= marshal_as(vMainProgram);
CurrentProgram = marshal_as(vCurrentProgram);
MainProgramPath = marshal_as(vMainProgramPath);
return ret;
}
//讀取 CNC Motion 狀態資訊
int CNCInfoClassCPlus::READ_NCMotion(unsigned short Channel, unsigned short Unit, unsigned int& SPSpeed, double& Feed,
short& SPLoad, double& SPActSpeed, double& ActFeed, unsigned int& DwellTime, short& SPToolNo,
unsigned short& MagaNo, unsigned short& CmdToolNo, unsigned short& StandbyToolNo, unsigned short& StandbyPot,
unsigned short& DCode, unsigned short& HCode, unsigned short& TCode, unsigned short& MCode,
std::vector& GCodeGroup, std::vector& GCodeGroupDecimal, unsigned short& FeedRate,
unsigned short& RPDRate, unsigned short& SpindleRate, double& JOGRate, unsigned short& MPGRate)
{
GCodeGroup.clear();
GCodeGroupDecimal.clear();
cli::array^ vGCodeGroup;
cli::array^ vGCodeGroupDecimal;
int ret = CNCInfoManage::obj->READ_NCMotion(Channel, Unit, SPSpeed, Feed,
SPLoad, SPActSpeed, ActFeed, DwellTime, SPToolNo, MagaNo, CmdToolNo, StandbyToolNo, StandbyPot,
DCode, HCode, TCode, MCode, vGCodeGroup, vGCodeGroupDecimal, FeedRate,
RPDRate, SpindleRate, JOGRate, MPGRate);
if (!vGCodeGroup|| !vGCodeGroupDecimal)
{
return ret;
}
for each (auto i in vGCodeGroup)
{
GCodeGroup.push_back(i);
}
for each (auto i in vGCodeGroupDecimal)
{
GCodeGroupDecimal.push_back(i);
}
return ret;
}
//讀取主軸負載峰值
int CNCInfoClassCPlus::READ_Spindle_Load_Peak(unsigned short Channel, unsigned short& AxisCount,
std::vector& AxisNr, std::vector& Result, std::vector& AxisValue)
{
AxisNr.clear();
Result.clear();
AxisValue.clear();
cli::array^ vAxisNr;
cli::array^ vResult;
cli::array^ vAxisValue;
int ret = CNCInfoManage::obj->READ_Spindle_Load_Peak(Channel, AxisCount, vAxisNr, vResult, vAxisValue);
if (!vAxisNr || !vResult || !vAxisValue)
{
return ret;
}
for each (auto var in vAxisNr)
{
AxisNr.push_back(var);
}
for each (auto var in vResult)
{
Result.push_back(var);
}
for each (auto var in vAxisValue)
{
AxisValue.push_back(var);
}
return ret;
}
/*12 座標資訊*/
//讀取 CNC 各軸座標
int CNCInfoClassCPlus::READ_Position(unsigned short Channel, unsigned short Unit, std::vector& AxisName,
std::vector& CoordMech, std::vector& CoordAbs, std::vector& CoordRel,
std::vector& CoordRes, std::vector& CoordOffset)
{
AxisName.clear();
CoordMech.clear();
CoordAbs.clear();
CoordRel.clear();
CoordRes.clear();
CoordOffset.clear();
cli::array^ vAxisName;
cli::array^ vCoordMech;
cli::array^ vCoordAbs;
cli::array^ vCoordRel;
cli::array^ vCoordRes;
cli::array^ vCoordOffset;
int ret = CNCInfoManage::obj->READ_Position(Channel, Unit, vAxisName,vCoordMech, vCoordAbs, vCoordRel,
vCoordRes, vCoordOffset);
if (!vAxisName || !vCoordMech || !vCoordAbs || !vCoordRel || !vCoordRes || !vCoordOffset)
{
return ret;
}
for each (String ^ var in vAxisName)
{
auto s = marshal_as(var);
AxisName.push_back(s);
}
for each (auto var in vCoordMech)
{
CoordMech.push_back(var);
}
for each (auto var in vCoordAbs)
{
CoordAbs.push_back(var);
}
for each (auto var in vCoordRel)
{
CoordRel.push_back(var);
}
for each (auto var in vCoordRes)
{
CoordRes.push_back(var);
}
for each (auto var in vCoordOffset)
{
CoordOffset.push_back(var);
}
return ret;
}
//写入工件座標資料,二维数组
int CNCInfoClassCPlus::WRITE_GCoordinary(unsigned short Channel, unsigned short Unit, unsigned short Index,
unsigned short Length, std::vector> GCoordinaryArray)
{
//读取动态二维数组的大小,来定义下面数组大小
int iRow = GCoordinaryArray.size();
int iCol= GCoordinaryArray.at(0).size();
if (iRow==0 || iCol==0)
{
//写入数据错误。
return -1000;
}
cli::array^ vGCoordinaryArray=gcnew cli::array(iRow, iCol);
for (unsigned int i = 0; i < GCoordinaryArray.size(); ++i)
{
for (unsigned int j = 0; j < GCoordinaryArray.at(i).size(); ++j)
{
vGCoordinaryArray[i, j]= GCoordinaryArray.at(i).at(j);
}
}
int ret = CNCInfoManage::obj->WRITE_GCoordinary(Channel, Unit, Index, Length, vGCoordinaryArray);
return ret;
}
//讀取偏移座標資料
int CNCInfoClassCPlus::READ_Offset_Coordinary(unsigned short Channel, unsigned short Unit,
std::vector& CoordinaryArray)
{
CoordinaryArray.clear();
cli::array^ vCoordinaryArray;
int ret = CNCInfoManage::obj->READ_Offset_Coordinary(Channel, Unit, vCoordinaryArray);
if (!vCoordinaryArray)
{
return ret;
}
for each (auto var in vCoordinaryArray)
{
CoordinaryArray.push_back(var);
}
return ret;
}
//写入偏移座標資料
int CNCInfoClassCPlus::WRITE_Offset_Coordinary(unsigned short Channel, unsigned short Unit,
std::vector CoordinaryArray)
{
//读取动态二维数组的大小,来定义下面数组大小
int iRow = CoordinaryArray.size();
if (iRow == 0)
{
//写入数据错误。
return -1000;
}
cli::array^ vCoordinaryArray = gcnew cli::array(iRow);
for (unsigned int i=0; iWRITE_Offset_Coordinary(Channel, Unit, vCoordinaryArray);
return ret;
}