Mazak 免授权采集 --- C/C++动态库dll方式

最近在做各种机床的数据采集,但发现国内很多数控类型的采集协议在网上都没法找到,基本都是被个人握在手里,所以想打破国内的这个局面,非电气专业,里面描述有不对的望指正,有写的不好的望勿喷。

//MazakDll.h
#pragma once
#include <iostream>
#include <windows.h>
#include <string>
#include <vector>

using namespace std;

struct MAZ_TOOLINFO
{
	unsigned short tno;
	byte suf;
	byte sufatr;
	byte name;
	byte part;
	byte sts;

	string dummy;
	int yob;
	string dummy2;
};

struct MAZ_FEED
{
	int fmin;
	int frev;
};

struct MAZ_PROINFO
{
	char wno[33];
	char dummy[7];
	char comment[49];
	char dummy2[7];
	byte type;
	char dummy3[7];
};

struct MAZ_NCPOS
{
	char status[16];
	int data[16];
};

struct AxisNameEntry
{
	char name[4];
};
struct MAZ_AXISNAME
{
	char status[16];
	AxisNameEntry axisname[16];
};

struct MAZ_ALARM
{
	short eno;
	byte sts;
	char dummy[5];
	char pa1[32];
	char pa2[16];
	char pa3[16];
	byte mon;
	byte day;
	byte hor;
	byte min;
	char dummy2[4];
	char extmes[32];
	char head[4];
	char dummy3[4];
	char message[64];
};
struct MAZ_ALARMALL
{
	MAZ_ALARM alarm[24];
};

//连接
typedef int (*MazConnect)(unsigned short& handle, const char* ip, int port, int times);
//当前刀具信息
typedef int (*GetCurrentTool)(unsigned short handle, unsigned short param1, MAZ_TOOLINFO& param2);
//断开连接
typedef int (*MazDisconnect)(unsigned short handle);
//实际进给速度
typedef int (*MazGetFeed)(unsigned short handle, int param1, MAZ_FEED& value);
//主轴实际转速
typedef int (*MazGetCurrentSpindleRev)(unsigned short handle, unsigned short axis, long& value);
//正在运行的程序信息
typedef int (*MazGetRunningPro)(unsigned short handle, unsigned short param1, MAZ_PROINFO& info);
//主程序信息
typedef int (*MazGetMainPro)(unsigned short handle, int param1, MAZ_PROINFO& info);
//坐标
typedef int (*MazGetCurrentPos)(unsigned short handle, MAZ_NCPOS& pos);
//轴名称
typedef int (*MazGetAxisName)(unsigned short handle, MAZ_AXISNAME& name);
//当前报警
typedef int (*MazGetAlarm)(unsigned short handle, MAZ_ALARMALL& info);
//运行模式
typedef int (*MazGetRunMode)(unsigned short handle, unsigned short param1, unsigned short& value);
//主轴负载
typedef int (*MazGetSpindleLoad)(unsigned short handle, unsigned short param1, unsigned short& value);
//进给倍率
typedef int (*MazGetFeedOverRide)(unsigned short handle, unsigned short param1, unsigned short& value);
//快速进给倍率
typedef int (*MazGetRapidOverRide)(unsigned short handle, unsigned short param1, unsigned short& value); 
//加工时间
typedef int (*MazGetCurrentCycleTime)(unsigned short handle, long& value);
//各轴负载
typedef int (*MazGetAxisLoad)(unsigned short handle, unsigned short param1, unsigned short& value);
//工件坐标系
typedef int (*MazGetAddWorkCoordSysNum)(unsigned short handle, unsigned short param1, long& value);
//伺服电机
typedef int (*MazGetServoMonitor)(unsigned short handle, short& value);
//主轴倍率
typedef int (*MazGetSpindleOverRide)(unsigned short handle, unsigned short param1, unsigned short& value);

unsigned short handle = 0;
const std::string strIp = "192.168.105.5";
int nPort = 50100;
//main.cpp
#include "MazakDll.h"

//连接
bool Connect(const HMODULE& hMoudle)
{
	MazConnect _connect = (MazConnect)GetProcAddress(hMoudle, "MazConnect");
	if (_connect == NULL)
	{
		cout << "do not find MazConnect function" << endl;
		return false;
	}
	int res = _connect(handle, strIp.c_str(), nPort, 10);
	return res;
}

//断开连接
void disConnect(const HMODULE& hMoudle)
{
	MazDisconnect _disconnect = (MazDisconnect)GetProcAddress(hMoudle, "MazDisconnect");
	if (_disconnect == NULL)
	{
		cout << "do not find MazDisconnect function" << endl;
		return;
	}
	_disconnect(handle);
}

//当前刀具信息
void getCurrentTool(const HMODULE& hMoudle)
{
	GetCurrentTool _getCurrentTool = (GetCurrentTool)GetProcAddress(hMoudle, "MazGetCurrentTool");
	if (_getCurrentTool == NULL)
	{
		cout << "do not find MazGetCurrentTool function" << endl;
		return;
	}
	MAZ_TOOLINFO info;
	int res = _getCurrentTool(handle, 0, info);
	std::cout << "tool suf " << static_cast<int>(info.suf)
		<< " current tool " << static_cast<int>(info.name)
		<< " tool tno " << info.tno << std::endl;
}

//进给速度
void getFeed(const HMODULE& hMoudle)
{

	MazGetFeed _getFeed = (MazGetFeed)GetProcAddress(hMoudle, "MazGetFeed");
	if (_getFeed == NULL)
	{
		cout << "do not find MazGetFeed function" << endl;
		return;
	}
	MAZ_FEED feed;
	int res = _getFeed(handle, 0, feed);
	std::cout << "feed " << feed.fmin << "---" << feed.frev << std::endl;
}

//实际转速
void getCurrentSpindleRev(const HMODULE& hMoudle)
{
	MazGetCurrentSpindleRev _getCurrentSpindleRev = (MazGetCurrentSpindleRev)GetProcAddress(hMoudle, "MazGetCurrentSpindleRev");
	if (_getCurrentSpindleRev == NULL)
	{
		cout << "do not find MazGetCurrentSpindleRev function" << endl;
		return;
	}
	for (int i = 0; i < 8; i++)
	{
		long value;
		_getCurrentSpindleRev(handle, i, value);
		std::cout << "spindle " << i << "---" << value << std::endl;
	}
}

//正在运行的程序信息
void getRunningPro(const HMODULE& hMoudle)
{
	MazGetRunningPro _getRunningPro = (MazGetRunningPro)GetProcAddress(hMoudle, "MazGetRunningPro");
	if (_getRunningPro == NULL)
	{
		cout << "do not find MazGetRunningPro function" << endl;
		return;
	}
	MAZ_PROINFO runPro;

	auto res = _getRunningPro(handle, 0, runPro);
	Sleep(5);
	std::cout << "run pro name " << runPro.wno << "--" << runPro.comment << "---" << static_cast<int>(runPro.type) << std::endl;
}

//主程序信息
void getMainProInfo(const HMODULE& hMoudle)
{

	MazGetMainPro _getMainPro = (MazGetMainPro)GetProcAddress(hMoudle, "MazGetMainPro");
	if (_getMainPro == NULL)
	{
		cout << "do not find MazGetMainPro function" << endl;
		return;
	}
	MAZ_PROINFO proInfo;
	_getMainPro(handle, 0, proInfo);
	std::cout << "pro name " << proInfo.wno << " comment " << proInfo.comment << " type " << static_cast<int>(proInfo.type) << std::endl;
}

//坐标,需要先获取轴名称
void getPos(const HMODULE& hMoudle, const std::vector<std::string>& vecAxisNams)
{
	MazGetCurrentPos _getCurrentPos = (MazGetCurrentPos)GetProcAddress(hMoudle, "MazGetCurrentPos");
	if (_getCurrentPos == NULL)
	{
		cout << "do not find MazGetCurrentPos function" << endl;
		return;
	}
	MAZ_NCPOS pos;
	_getCurrentPos(handle, pos);
	Sleep(5);
	for (int i = 0; i < vecAxisNams.size(); i++)
	{
		std::cout << "name " << vecAxisNams[i] << "---" << pos.data[i] << std::endl;
	}
}

//轴名称
void getAxisName(const HMODULE& hMoudle, std::vector<std::string>& vecAxisNams)
{
	MazGetAxisName _getAxisName = (MazGetAxisName)GetProcAddress(hMoudle, "MazGetAxisName");
	if (_getAxisName == NULL)
	{
		cout << "do not find MazGetAxisName function" << endl;
		return;
	}
	MAZ_AXISNAME axisName;
	_getAxisName(handle, axisName);
	for (int i = 0; i < 16; i++)
	{
		if (axisName.axisname[i].name[0] == 0x00) continue;
		std::string name(axisName.axisname[i].name);
		std::cout << "axis name " << name << std::endl;
		vecAxisNams.push_back(name);
	}
}

//当前报警
void getAlarm(const HMODULE& hMoudle)
{
	MazGetAlarm _getAlarm = (MazGetAlarm)GetProcAddress(hMoudle, "MazGetAlarm");
	if (_getAlarm == NULL)
	{
		cout << "do not find MazGetAlarm function" << endl;
		return;
	}
	MAZ_ALARMALL alarms;
	_getAlarm(handle, alarms);
	for (int i = 0; i < 24; i++)
	{
		std::cout << "eno " << alarms.alarm[i].eno
			<< "sts" << static_cast<int>(alarms.alarm[i].sts)
			<< "mon " << static_cast<int>(alarms.alarm[i].mon)
			<< " day " << static_cast<int>(alarms.alarm[i].day)
			<< " hor " << static_cast<int>(alarms.alarm[i].hor)
			<< " min " << static_cast<int>(alarms.alarm[i].min)
			<< " message" << alarms.alarm[i].message << std::endl;
	}
}

//当前模式
/*
	home--4
	rapid --3
	手动 --0
	memory --1
	tape --2
	mdi --6
*/
void getRunMode(const HMODULE& hMoudle)
{
	MazGetRunMode _getRunMode = (MazGetRunMode)GetProcAddress(hMoudle, "MazGetRunMode");
	if (_getRunMode == NULL)
	{
		cout << "do not find MazGetRunMode function" << endl;
		return;
	}
	unsigned short value;
	_getRunMode(handle, 0, value);
	std::cout << "mode " << value << std::endl;
}

//主轴负载
void getSpindleLoad(const HMODULE& hMoudle)
{
	MazGetSpindleLoad _getSpindleLoad = (MazGetSpindleLoad)GetProcAddress(hMoudle, "MazGetSpindleLoad");
	if (_getSpindleLoad == NULL)
	{
		cout << "do not find MazGetSpindleLoad function" << endl;
		return;
	}
	unsigned short value;
	_getSpindleLoad(handle, 0, value);
	std::cout << "SpindleLoad " << value << std::endl;
}

//轴负载
void getAxisLoad(const HMODULE& hMoudle)
{
	MazGetAxisLoad _getAxisLoad = (MazGetAxisLoad)GetProcAddress(hMoudle, "MazGetAxisLoad");
	if (_getAxisLoad == NULL)
	{
		cout << "do not find MazGetAxisLoad function" << endl;
		return;
	}
	for (unsigned int i = 0; i < 16; i++) 
	{
		unsigned short value;
		_getAxisLoad(handle, i, value);
		std::cout << "AxisLoad " << value << std::endl;
	}
}

//进给倍率
void getFeedOverRide(const HMODULE& hMoudle)
{
	MazGetFeedOverRide _getFeedOverRide = (MazGetFeedOverRide)GetProcAddress(hMoudle, "MazGetFeedOverRide");
	if (_getFeedOverRide == NULL)
	{
		cout << "do not find MazGetFeedOverRide function" << endl;
		return;
	}
	unsigned short value;
	_getFeedOverRide(handle, 0, value);
	std::cout << "feed over ride " << value << std::endl;
}

//快速进给倍率
void getRapidOverRide(const HMODULE& hMoudle)
{
	MazGetRapidOverRide _getRapidOverRide = (MazGetRapidOverRide)GetProcAddress(hMoudle, "MazGetRapidOverRide");
	if(_getRapidOverRide == NULL)
	{
		cout << "do not find MazGetRapidOverRide function" << endl;
		return;
	}
	unsigned short value;
	_getRapidOverRide(handle, 0, value);
	std::cout << "RapidOverRide " << value << std::endl;
}

//主轴倍率
void getSpindleOverRide(const HMODULE& hMoudle)
{
	MazGetSpindleOverRide _getSpindleOverRide = (MazGetSpindleOverRide)GetProcAddress(hMoudle, "MazGetSpindleOverRide");
	if (_getSpindleOverRide == NULL)
	{
		cout << "do not find MazGetSpindleOverRide function" << endl;
		return;
	}
	unsigned short value;
	_getSpindleOverRide(handle, 0, value);
	std::cout << "SpindleOverRide " << value << std::endl;
}

//加工时间
void getCurrentCycleTime(const HMODULE& hMoudle)
{
	MazGetCurrentCycleTime _getCurrentCycleTime = (MazGetCurrentCycleTime)GetProcAddress(hMoudle, "MazGetCurrentCycleTime");
	if (_getCurrentCycleTime == NULL)
	{
		cout << "do not find MazGetCurrentCycleTime function" << endl;
		return;
	}
	long value;
	_getCurrentCycleTime(handle, value);
	std::cout << "cycle time " << value << std::endl;
}

int main()
{
	//加载动态库
	HMODULE hMoudle = LoadLibrary(L"./NTIFDLL.dll");
	if (hMoudle == NULL)
	{
		cout << "dll load fail" << endl;
		return -1;
	}

	//连接
	int res = Connect(hMoudle);
	if (res == 0)
	{
		cout << "connect suc" << endl;
	}
	else
	{
		cout << "connect fail" << endl;
		FreeLibrary(hMoudle);
		return -1;
	}

	//采集
	getCurrentTool(hMoudle);

	//采集完成后释放资源
	if (hMoudle)
	{
		FreeLibrary(hMoudle);
	}
	disConnect(hMoudle);

	system("pause");
	return 0;
}

采集函数不止这些,还在继续研究,有想法的可以一起探究。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小龙学IT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值