跳转至

MDI控制接口应用案例

1.新建项目并附加函数库

(1)在VS2017菜单“文件”→“新建”→ “项目”,启动创建项目向导。

image1

  1. 选择开发语言为“Visual C++”和程序类型“空项目”。
    image2

  2. 找到厂家提供的SDK库,路径如下(64位release库为例)。
    image3

    image4

函数库具体路径如下

image5

image6

  1. 复制【cnc_sdk】到项目目录。

image7

为了保证可以在VS调试器中运行,将相关平台的dll库复制到代码目录(如,项目名称为CncSdkDemo,则放在【{你的工程目录}\CncSdkDemo\CncSdkDemo】目录下),这里以64位平台为例

(将【{你的工程目录}\CncSdkDemo\CncSdkDemo\cnc_sdk\lib\x64】下的“CncApi.dll”和“CncApid.dll”复制到【{你的工程目录}\CncSdkDemo\CncSdkDemo\

image8

(5)进入项目的属性页面。

image9 在【VC++目录】选项中配置【包含目录】、【库目录】
image10 image11 image12 在【链接器】-【输入】中配置【附加依赖项】,需要用到CncApi.lib

image13 【确认】并【应用】
image14 2.编程

(1)【源文件】新建一个main.cpp
image15 image16 (2)查看函数手册,熟悉相关函数接口

image17 连接控制器

函数名 功能
connect 连接控制器

运动参数配置相关接口。

函数名 功能
setEstop 该接口设置当前系统的急停状态
setEnable 该接口设置当前系统的使能状态
setFeedOverride 该接口设置当前系统进给率
stopProgram 停止当前系统正在运行的程序

MDI控制接口:

函数名 功能
execMDI 该接口使用MDI指令进行运动

3.MDI指令控制机械运动

  1. 连接控制器
    void connect()
    {
        weconcnc::CCommApi *commApi = new weconcnc::CCommApi();
        std::string ipAddress = "192.168.54.98";
        uint16_t port = 9995;
        weconcnc::WECONCNC_ERROR_E result = commApi->connect(ipAddress, port);
    }
    
  2. 确保机械处于使能状态,返回值为4
    void updateMotionState()
    {
        weconcnc::CProxyStatus* pStatus = weconcnc::CProxyStatus::getInstance(commApi);
        weconcnc::WECONCNC_ERROR_E ret;
        weconcnc::MACHINE_STATE_E motionState;
    
        ret = pStatus->getMotionState(motionState);
        if (ret != weconcnc::WECONCNC_ERROR_SUCCESS) {
            std::cout << "get motion state failed!" << std::endl;
        }
        else {
            std::cout << "Motion State: " << motionState << std::endl;
        }
    }
    
    如果处于未使能状态,可以通过设置急停和使能方法改变状态
    void on_setEnableButton_clicked()
    {
        weconcnc::CProxyMotion *proxyMotion = weconcnc::CProxyMotion::getInstance(pComm);
        weconcnc::WECONCNC_ERROR_E ret = weconcnc::WECONCNC_ERROR_SUCCESS;
        ret = proxyMotion->setEnable(true);
    }
    
    void on_setEstopButton_clicked()
    {
        weconcnc::CProxyMotion *proxyMotion = weconcnc::CProxyMotion::getInstance(pComm);
        weconcnc::WECONCNC_ERROR_E ret = weconcnc::WECONCNC_ERROR_SUCCESS;
        ret = proxyMotion->setEstop(false);
    }
    
  3. 调用MDI接口控制机械移动

    execMDI:

    定义: WECONCNC_ERROR_E execMDI(std::string sMdi);

    说明: 该接口使用MDI指令进行运动

    参数:【IN】std::string sMdi,MDI指令

    返回值: WECONCNC_ERROR_E,请参见WECONCNC_ERROR_E定义

    void on_execMDIButton_clicked()
    {
        std::string sMdi = "G0 X100 Y100 F500";
        weconcnc::CProxyMotion *proxyMotion = weconcnc::CProxyMotion::getInstance(pComm);
        weconcnc::WECONCNC_ERROR_E ret = weconcnc::WECONCNC_ERROR_SUCCESS;
    
        ret = proxyMotion->execMDI(sMdi);
    }