数字量输入输出例程

数字量输入输出例程
// hardware.cpp : 定义控制台应用程序的入口点。

// 该例程仅用于功能演示,请保证安全的情况下使用

// 测试功能:控制器数字量输入输出功能
// 测试平台:网络型运动控制器
// 测试环境:Windows
// 测试流程:
//           (1)初始化控制器
// 注意事项:
//           (1)本例程使用的“例程专用.xml”、“例程专用.cfg”,仅用于本例程
//           (2)实际使用时,需要使用MotionStudio生成网络配置xml
#include "stdafx.h"

// 加载固高运动控制库头文件
#include "gxn.h"
// 动态加载固高运动控制gxn.lib库
#pragma comment(lib,"gxn.lib")

/**
 * @brief 指令出错打印函数
 * @param command 打印信息字符串
 * @param error 错误码
 * @return 错误码
*/
short CommandHandler(char *command ,short error)
{
    printf("%s = %d\n", command, error);
    getchar();
    return error;
}

int _tmain(int argc, _TCHAR* argv[])
{
    short core = 1;
    short rtn;
    int i;
    long status;
    short overTime;
    short diType,diIndex,count;
    short diValue[10];
    TDigitalOutputPro outputPro;
    short doValue[10];
    //打开控制器
    rtn = GTN_OpenCard(CHANNEL_PCIE,NULL,NULL);
    if (CMD_SUCCESS != rtn)
    {
        return CommandHandler("GTN_OpenCard",rtn);
    }
     // 初始化网络
    // 注意:(1)“例程专用.xml”仅用于本例程
    //       (2)实际使用时,需要使用MotionStudio生成对应的网络配置文件
    // overTime:网络初始化超时时间,单位:秒
    overTime = 120;
    rtn = GTN_NetInit(NET_INIT_MODE_XML_STRICT,"例程专用.xml",overTime,&status);
    if ( 0 != rtn )
    {
        printf("status = %d\n",status);
        return CommandHandler("GTN_NetInit",rtn);
    }
    printf("Init Net Success !\n");

    //读取数字量输入状态,从第1路开始连续读取10路。
    diType = MC_GPI;
    diIndex = 1;
    count = 10;
    rtn = GTN_ReadDigitalInput(core,diType,diIndex,diValue,10);
    if (CMD_SUCCESS != rtn)
    {
        return CommandHandler("GTN_ReadDigitalInput",rtn);
    }
    for (i = 1; i <= count;i++)
    {
        // 显示DI输入数值
        printf("diIndex = %d,value = %d\n",i,diValue[i-1]);
    }

    //设置数字量输出状态,从第1路开始连续设置10路输出立即输出为0。
    memset(&outputPro,0,sizeof(outputPro));
    outputPro.doType = MC_GPO;
    outputPro.doIndex = 1;
    outputPro.doCount = 10;
    outputPro.mode = 1; 
    outputPro.pValue = &doValue[0];
    rtn = GTN_WriteDigitalOutputPro(core,&outputPro,NULL);
    if (CMD_SUCCESS != rtn)
    {
        return CommandHandler("GTN_WriteDigitalOutputPro",rtn);
    }

    rtn = GTN_ReadDigitalInput(core,diType,diIndex,diValue,10);
    if (CMD_SUCCESS != rtn)
    {
        return CommandHandler("GTN_ReadDigitalInput",rtn);
    }
    for (i = 1; i <= count;i++)
    {
        // 显示DI输入数值
        printf("diIndex = %d,value = %d\n",i,diValue[i-1]);
    }
    printf("Press Any Key To Exit !\n");
    getchar();
    return 0;
}