#include <stdlib.h>
#include <windows.h>
#include <wchar.h>
#include <stdio.h>

#include "sc_io.h"
//将TCHAR转为char   
//*tchar是TCHAR类型指针，*_char是char类型指针   
void TcharToChar (const TCHAR * tchar, char * _char)  
{  
    int iLength ;  
	//获取字节长度   
	iLength = WideCharToMultiByte(CP_ACP, 0, tchar, -1, NULL, 0, NULL, NULL);  
	//将tchar值赋给_char    
	WideCharToMultiByte(CP_ACP, 0, tchar, -1, _char, iLength, NULL, NULL);   
}  

int main(int argc, char* argv[])
{
    short ret = 0;
    int i = 0;
    U16 dev_cnt = 0;
	U32 set_val = 0;
	U32 set_val2 = 0;
    TDevInfo dev_info_list[32] = {0};
    HAND dev_handles[32] = {0};
    ret = NIO_Search(&dev_cnt, dev_info_list);

    if (dev_cnt == 0)
    {
        printf("Not found moudle\n");
        return 0;
    }
    
    printf("found %d modules: \n", dev_cnt);
    for (i = 0; i < dev_cnt; ++i)
    {
        printf("%d: %s, %s\n", dev_info_list[i].address, dev_info_list[i].idStr, dev_info_list[i].description);
        ret = NIO_OpenByID((const char *)dev_info_list[i].idStr, &dev_handles[i]);
        if (ret != RTN_CMD_SUCCESS)
        {
            printf("cannot open module\n");
        }
    }

    i = 0;
    while (1)
    {
        set_val = 1 << i;
        i++;
        if (i == 32)
        {
            i = 0;
        }
        ret = NIO_SetDO(dev_handles[0], 0, 2, (const unsigned short *)&set_val);
        if (ret != RTN_CMD_SUCCESS)
        {
            printf("NIO_SetDO error : %d\n", ret);
        }
	/*	set_val2 = ~set_val;
		ret = NIO_SetDO(dev_handles[1], 0, 2, (const unsigned short *)&set_val2);
		if (ret != RTN_CMD_SUCCESS)
		{
			printf("NIO_SetDO error : %d\n", ret);
		}*/
        //printf("set val 0x%04x\n");
        Sleep(200);
    }

	system( "PAUSE ");
	
	return 0;
}

