testDIO.py 4.36 KB
Newer Older
manuel's avatar
manuel committed
1 2 3
#!/usr/bin/env python3
# @file testDIO.py
#
manuel's avatar
manuel committed
4
# @brief Check all ports of the boards
manuel's avatar
manuel committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#
# @author Manuel Castilla
# @date 25/04/2019
#
###########################################################################

import configparser
from dio import DIO
import time

maxOffset = 0.000000017 #offset 17 nanoseconds


def findTimestamp(listStamp1,listStamp2,offset):
    """
    This function compare two list and search if two timestamps match

    Args:
        listStamp1: first list of timestamps
        listStamp2: second list of timestamps
        offset: maximum offset between each comparison

    Return:
        True if it find a match in the two lists, False otherwise
    """
    for i in listStamp1:
        for j in listStamp2:
            value = i - j
            if (value <= offset and value >= -offset):
                return True

    return False

38
def tryChannel(hostName1,userName1,password1,busId1,hostName2,userName2,password2,busId2,channel):
manuel's avatar
manuel committed
39 40 41 42 43 44 45
    """
    Try the input and output of channel

    Args:
        hostName1: ip remote host 1
        username1: user name host 1
        password1: password
46
        busId1: number PCIe bus PC 1
manuel's avatar
manuel committed
47 48 49
        hostName2: ip remote host 2
        username2: user name host 2
        password2: password
50
        busId2: number PCIe bus PC 2
manuel's avatar
manuel committed
51
        channel: number of channel to try(0 to 4)
52 53
    Return:
        True if the channel fail, False otherwise
manuel's avatar
manuel committed
54 55
    """
    try:
56 57
        dio1 = DIO(hostName1,userName1,password1,busId1)
        dio2 = DIO(hostName2,userName2,password2,busId2)
manuel's avatar
manuel committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        dio1.configurePorts("pdddd")
        dio2.configurePorts("iiiii")
        dio1.clearTimestamps()
        dio2.clearTimestamps()

        if (channel != 0):
            dio1.armPulse(channel,0.1,"now")
        else:
            time.sleep(2) #wait 2 seconds to ensure some timestamp

        if(findTimestamp(dio1.getTimestamp(channel),dio2.getTimestamp(channel),maxOffset)):
            dio1.configurePorts("iiiii")
            dio2.configurePorts("pdddd")
            dio1.clearTimestamps()
            dio2.clearTimestamps()

            if (channel != 0):
                dio2.armPulse(channel,0.1,"now")
            else:
                time.sleep(2) #wait 2 seconds to ensure some timestamp

            if(findTimestamp(dio1.getTimestamp(channel),dio2.getTimestamp(channel),maxOffset)):
                print("Channel",channel,": ok in host:",hostName1,"and host:",hostName2)
81
                return False
manuel's avatar
manuel committed
82 83 84 85 86 87
            else:
                raise
        else:
            raise
    except:
        print("Channel",channel,": error in host:",hostName1,"or host:",hostName2)
88
        return True
manuel's avatar
manuel committed
89 90 91 92 93 94 95 96 97 98 99 100


#--------------------------------------------------#
#--------------------------------------------------#
# MAIN
#--------------------------------------------------#
#--------------------------------------------------#

def main(configuration):
    hostName1 = configuration["hostName1"]
    userName1 = configuration["userName1"]
    password1 = configuration["password1"]
101
    busIdSpec1 = configuration["busIdSpec1"]
manuel's avatar
manuel committed
102 103 104
    hostName2 = configuration["hostName2"]
    userName2 = configuration["userName2"]
    password2 = configuration["password2"]
105
    busIdSpec2 = configuration["busIdSpec2"]
manuel's avatar
manuel committed
106

107 108
    error = False
    numberChannels = 5
109 110

    print("\nExecuting DIO test")
111 112 113 114 115
    print("This test needs White Rabbit working")
    for i in range(numberChannels):
        error |= tryChannel(hostName1,userName1,password1,busIdSpec1,hostName2,userName2,password2,busIdSpec2,i)

    if(not error):
manuel's avatar
manuel committed
116
        print("Test DIO: successful")
117
    else:
manuel's avatar
manuel committed
118 119 120 121 122 123 124 125 126 127
        print("Test DIO: error")

if __name__ == '__main__':
    try:
        configuration = {}
        nameConfig = "configuration.cfg"

        config = configparser.ConfigParser()
        config.readfp(open(nameConfig))

manuel's avatar
manuel committed
128 129 130
        configuration["hostName1"] = config.get('pc1',"hostName")
        configuration["userName1"] = config.get('pc1',"userName")
        configuration["password1"] = config.get('pc1',"password")
131
        configuration["busIdSpec1"] = config.get('pc1',"busIdSpec")
manuel's avatar
manuel committed
132 133 134
        configuration["hostName2"] = config.get('pc2',"hostName")
        configuration["userName2"] = config.get('pc2',"userName")
        configuration["password2"] = config.get('pc2',"password")
135
        configuration["busIdSpec2"] = config.get('pc2',"busIdSpec")
manuel's avatar
manuel committed
136 137 138

        main(configuration)
    except configparser.Error as e:
139
        print("Error load configuration:",e.message)