VS2015新建发那科数控机床focas数据采集C++项目

本文介绍了如何在Visual Studio 2015中配置并使用Focas库来实现与CNC数控机床的通信。首先详细说明了Focas库的下载与安装步骤,接着通过示例代码展示了如何读取CNC系统的系统信息及轴的位置数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:下载focas库文件

可以自己搜一下,这是我上传的,积分多的大佬可以支持一下

\FOCAS2 Library\Fwlib这个目录下有Fwlib32.lib和.dll文件

\FOCAS2 Library\Fwlib\0iD里有fwlib32.h头文件

第二步:在vs2015中新建c++项目,并配置第三方库

这里主要简绍如何在vs2015中配置Focas库

参考,VS2015第三方库的配置!

添加头文件fwlib32.h,添加现有项

项目-属性-链接器-常规-附加库目录(填入.lib文件所在文件夹)

项目-属性-链接器-输入-附加依赖项(填入.lib文件名)

把.dll文件拷贝到编译生成的文件夹

必须用到Fwlib32.dll,fwlibe1.dll

第三步:cnc数控机床配置or虚拟机

要设置好cnc系统的ip地址

系统>内嵌(内藏口)>公共

第四步:示例代码

// cppFocastest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include "fwlib32.h"
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <windows.h>



using namespace std;

void sys_info(unsigned short handle) {
	short ret;
	ODBSYS sysinfo;
	ret = cnc_sysinfo(handle, &sysinfo);
	if (!ret)
	{
		cout << "read info" << endl;
		cout << sysinfo.addinfo << endl;
		cout << sysinfo.cnc_type << endl;

	}
	else {
		cout << "error";
	}

}

void absolute(unsigned short handle)
{
	/*读取轴的绝对位置坐标
	输入:
	(1)FlibHndl:句柄值;
	(2)a【轴数(axis)】:设置轴数
	ALL_AXES	对所有轴(ALL_AXES:-1)
	1,..,m	对单个轴(m:控制轴数)
	(3)b【长度(length)】:数据块的长度及ODBAXIS结构体的大小
	输出:ODBAXIS
	*/
	short ret;
	ODBAXIS  out;
	int axis;
	int n;
	for (axis = -1; axis <= MAX_AXIS; axis++)
	{
		if (axis == 0) continue;
		for (n = 1; n <= MAX_AXIS; n++)
		{
			ret = cnc_absolute(handle, axis, 4 + 4 * n, &out);
			cout << ret << endl;

			string absoluteX = to_string(out.data[0]);
			string absoluteY = to_string(out.data[1]);
			string absoluteZ = to_string(out.data[2]);
			cout << absoluteX << " " << absoluteY << " " << absoluteZ << endl;
			Sleep(1000);
		}

	}
}

void relative(unsigned short handle)
{
	/*读取轴的相对位置坐标
	输入:
	(1)FlibHndl:句柄值;
	(2)a【轴数(axis)】:设置轴数
	ALL_AXES	对所有轴(ALL_AXES:-1)
	1,..,m	对单个轴(m:控制轴数)
	(3)b【长度(length)】:数据块的长度及ODBAXIS结构体的大小
	输出:ODBAXIS
	*/
	short ret;
	ODBAXIS  out;
	int axis, n;
	for (axis = -1; axis <= MAX_AXIS; axis++)
	{
		if (axis == 0) continue;
		for (n = 1; n <= MAX_AXIS; n++)
		{
			ret = cnc_relative(handle, axis, 4 + 4 * n, &out);
			cout << ret << endl;

			string relativeX = to_string(out.data[0]);
			string relativeY = to_string(out.data[1]);
			string relativeZ = to_string(out.data[2]);
			cout << relativeX << " " << relativeY << " " << relativeZ << endl;
			Sleep(1000);
		}
	}
}

int main()
{
	//SetConsoleOutputCP(65001);


	unsigned short h;
	short ret;
	string ip = "192.168.8.130";

	ret = cnc_allclibhndl3(ip.c_str(), 8193, 10, &h);
	if (ret)	/// Allocate the library handle(for Ethernet connection)
	{
		printf("Connect Failed!\n");
		system("Pause");
		return -1;
	}
	else {
		cout << "machine: " << ip << endl;
		//system("Pause");
	}


	sys_info(h);
	//absolute(h);
	//relative(h);
	system("Pause");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值