"""--------------------控制器初始化--------------------"""
from pymtn_lib20 import *
def DevInit():
    print("控制器初始化开始")
    ''' 搜索控制器'''
    (devNum, devinfolist, ret) = pymtn.NMC_DevSearch(pymtn.TSearchMode.Internet)
    if ret != 0:
        print("控制器搜索出错,返回值为:", ret)
        exit()
    if ret == 0:
        print("the count of devices: (%s)" % (devNum))
        for i in range(0, devNum):
            print(devinfolist[i].address)
            print(devinfolist[i].idStr)
            print(devinfolist[i].description)
            print(devinfolist[i].ID)
    if devNum <= 0:
        print("没有找到控制器")
        exit()

    ''' 打开第一个控制器'''
    (devHandle, ret) = pymtn.NMC_DevOpenByID(devinfolist[0].idStr)
    if ret != pymtn.RTN_CMD_SUCCESS:
        print("控制器打开出错", ret)
        exit()

    # ''' 打开第一个控制器--只有一张卡的时候，直接打开，不用搜索'''
    # (devHandle, ret) = pymtn.NMC_DevOpen(0)
    # if ret != pymtn.RTN_CMD_SUCCESS:
    #     print("控制器打开出错", ret)
    #     exit()

    (Info, ret) = pymtn.NMC_GetCardInfo(devHandle)
    num = Info.axisNum
    axisHandle = [c_ushort(0)] * num
    for i in range(0, num):
        (axisHandle[i], ret) = pymtn.NMC_MtOpen(devHandle, i)
        if ret != pymtn.RTN_CMD_SUCCESS:
            print("控制器打开出错", ret)
            exit()

    print("控制器初始化结束")
    return (devHandle, axisHandle)

def _IO(devHandle, axisHandle):

    print("IO测试开始")

    # 读取DI组状态
    di_value0, ret = pymtn.NMC_GetDIGroup(devHandle, 0)
    if ret != 0:
        print(f"读取DI组0失败，错误码: {ret}")
        return

    di_value1, ret = pymtn.NMC_GetDIGroup(devHandle, 1)
    if ret != 0:
        print(f"读取DI组1失败，错误码: {ret}")
        return

    # 组合成64位值
    di_group_value = (di_value1.value << 32) | (di_value0.value & 0xFFFFFFFF)

    # 逐个判断每一位
    di_values = []
    for i in range(64):
        di_values.append((di_group_value >> i) & 1)

    print(f"DI0状态: {di_values[0]}")
######################################################################################
    # 设置通用输出组（32位）
    ret = pymtn.NMC_SetDOGroup(devHandle, 0xFFFF0000, 0)  # 设置组0的高16位为1，低16位为0
    if ret != 0:
        print(f"设置DO组失败，错误码: {ret}")
    else:
        print("设置DO组成功")

    # 读取通用输出组
    do_value0, ret = pymtn.NMC_GetDOGroup(devHandle, 0)
    if ret != 0:
        print(f"读取DO组0失败，错误码: {ret}")
        return

    do_value1, ret = pymtn.NMC_GetDOGroup(devHandle, 1)
    if ret != 0:
        print(f"读取DO组1失败，错误码: {ret}")
        return

    # 组合成64位值
    do_group_value = (do_value1.value << 32) | (do_value0.value & 0xFFFFFFFF)

    # 逐个判断每一位
    do_values = []
    for i in range(64):
        do_values.append((do_group_value >> i) & 1)

    print(f"DO0状态: {do_values[0]}")

    ################### 按位 ####################################################
    # 按位设置通用输出
    ret = pymtn.NMC_SetDOBit(devHandle, 5, 1)  # 设置第5位DO为高电平
    if ret != 0:
        print(f"设置DO位失败，错误码: {ret}")
    else:
        print("设置DO位成功")

    # 按位读取通用输入
    di_bit, ret = pymtn.NMC_GetDIBit(devHandle, 3)  # 读取第3位DI状态
    if ret != 0:
        print(f"读取DI位失败，错误码: {ret}")
    else:
        print(f"DI位3状态: {di_bit.value}")

    ######## 扩展IO模块 #################################
    # 设置扩展IO模块有效
    ret = pymtn.NMC_SetIOModuleEn(devHandle, 2)  # 启用站号2的IO模块
    if ret != 0:
        print(f"设置IO模块启用失败，错误码: {ret}")
    else:
        print("设置IO模块启用成功")

    # 设置带类型的扩展IO模块有效
    ret = pymtn.NMC_IOModuleSetEn(devHandle, 3, pymtn.IOMODULE_TYPE_IO64)  # 启用站号3的32DI32DO模块
    if ret != 0:
        print(f"设置IO模块启用失败，错误码: {ret}")
    else:
        print("设置IO模块启用成功")

    # 读取扩展IO模块类型
    module_type, ret = pymtn.NMC_IOModuleGetType(devHandle, 2)  # 读取站号2的模块类型
    if ret != 0:
        print(f"读取IO模块类型失败，错误码: {ret}")
    else:
        print(f"IO模块类型: {module_type.value}")

    print("IO测试结束")
if __name__ == "__main__":

    devHandle, axisHandle = DevInit()
    _IO(devHandle, axisHandle[0])