第一步:下载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");
}