def test_WriteTenPointsThenWriteSinglePointInBankB_ReturnExpectedReadBack(self): AO0 = AnalogOutput({'bank': Bank.B, 'channel': AOChannel.AO0}) AI0 = AnalogInput({'bank': Bank.B, 'channel': AIChannel.AI0}) def __multipleWriteAndRead(): sample_rate = 1000 input_value = [7,7,7,7,7,7,7,5,5,10] expected_value = '7,7,7,7,7,7,7,5,5,10' irq_thread = threading.Thread(target=self.__reading_thread, args=(AI0, len(input_value), sample_rate, expected_value)) self.__start_thread(irq_thread) AO0.write(input_value, sample_rate) self.__close_thread(irq_thread) def __singleWriteAndRead(): input_value = 3.2 AO0.write(input_value) value = AI0.read()[0] self.assertEqual(value, pytest.approx(input_value, 0.1)) __multipleWriteAndRead() __singleWriteAndRead() AO0.close() AI0.close()
def setUp(self): self.AI_single_channel = AnalogInput({ 'bank': bankA, 'channel': AIChannel.AI2, 'range': AIRange.PLUS_OR_MINUS_5V, 'mode': AIMode.SINGLE_ENDED })
def setUp(self): self.AO_single_channel = AnalogOutput({ 'bank': bank, 'channel': AOChannel.AO0 }) self.AI_single_channel = AnalogInput({ 'bank': bank, 'channel': AIChannel.AI0 })
def readWaveformFromAI(p_sampleRate, p_sampleSize, p_bank=Bank.A, p_channel=AIChannel.AI0, p_channelRef=None): ai_range = AIRange.PLUS_OR_MINUS_1V ai_mode = AIMode.SINGLE_ENDED isNewChannel = p_channelRef is None value_array = [] if isNewChannel: p_channelRef = AnalogInput({ 'bank': p_bank, 'channel': p_channel, 'range': ai_range, 'mode': ai_mode }) # configure the sample rate and start the acquisition p_channelRef.start_continuous_mode(p_sampleRate) printString('开始10秒录音...') # read the value timeout = -1 value_array = p_channelRef.read(p_sampleSize, timeout) printString('结束录音') # stop signal acquisition p_channelRef.stop_continuous_mode() if isNewChannel: p_channelRef.close() return value_array[0]
def open_first_ai(self): self.first_ai = AnalogInput( { 'bank': bankA, 'channel': AIChannel.AI2, 'range': AIRange.PLUS_OR_MINUS_10V }, { 'bank': bankA, 'channel': AIChannel.AI2, 'mode': AIMode.DIFFERENTIAL })
def setUp(self): self.AI_multiple_channels = AnalogInput( { 'bank': bankA, 'channel': AIChannel.AI2, 'range': AIRange.PLUS_OR_MINUS_10V }, { 'bank': bankA, 'channel': AIChannel.AI2, 'mode': AIMode.DIFFERENTIAL })
class Test_AnalogInputAndOutput_WriteTwoChannels(unittest.TestCase): def setUp(self): self.AO_multiple_channels = AnalogOutput({'bank': bank, 'channel': AOChannel.AO0}, {'bank': bank, 'channel': AOChannel.AO1}) self.AI_multiple_channels = AnalogInput({'bank': bank, 'channel': AIChannel.AI0}, {'bank': bank, 'channel': AIChannel.AI3}) def tearDown(self): self.AO_multiple_channels.close() self.AI_multiple_channels.close() def __start_thread(self, irq_thread): irq_thread.start() self.run = True def __close_thread(self, irq_thread): self.run = False irq_thread.join() def __reading_thread(self, number_of_samples, sample_rate, expected_value): value_array = None while self.run: value_array = self.AI_multiple_channels.read(number_of_samples, sample_rate) value_array_in_bank_A = value_array[0] AI1_array = value_array_in_bank_A[0] AI3_array = value_array_in_bank_A[1] AI1_values = [ round(value) for value in AI1_array ] AI3_values = [ round(value) for value in AI3_array ] print('AO0 %s should almost equal to %s' % (AI1_values, expected_value)) print('AO3 %s should almost equal to %s' % (AI3_values, expected_value)) def test_WriteSinglePoint_ReturnExpectedReadBack(self): input_value = 3.5 self.AO_multiple_channels.write(input_value) value_array = self.AI_multiple_channels.read() for value in value_array: self.assertEqual(value, pytest.approx(input_value, 0.1)) def test_WriteSixteenPointsWithOneThousandSampleRate_ReturnExpectedReadBack(self): sample_rate = 1000 input_value = [9,9,9,9,9,9,9,9,9,9,5,5,5,2,2,2] expected_value = '9,9,9,9,9,9,9,9,9,9,5,5,5,2,2,2' irq_thread = threading.Thread(target=self.__reading_thread, args=(len(input_value), sample_rate, expected_value)) self.__start_thread(irq_thread) self.AO_multiple_channels.write(input_value, sample_rate) self.__close_thread(irq_thread)
def test_OpenWithInvalidRange_ShowAssertion(self): with self.assertRaises(AssertionError): AnalogInput({ 'bank': bankA, 'channel': AIChannel.AI2, 'range': '+/-20V' })
def test_OpenWithInvalidBank_ShowAssertion(self): with self.assertRaises(AssertionError): AnalogInput({ 'bank': 'C', 'channel': AIChannel.AI0, 'mode': AIMode.SINGLE_ENDED })
def test_OpenWithInvalidChannelInSingleEndedMode_ShowAssertion(self): with self.assertRaises(AssertionError): AnalogInput({ 'bank': bankA, 'channel': 8, 'mode': AIMode.SINGLE_ENDED })
def readWaveformFromAI(p_sampleRate, p_sampleSize): ai_bank = Bank.A ai_channel = AIChannel.AI0 ai_range = AIRange.PLUS_OR_MINUS_1V ai_mode = AIMode.SINGLE_ENDED value_array = [] with AnalogInput({ 'bank': ai_bank, 'channel': ai_channel, 'range': ai_range, 'mode': ai_mode }) as AI_single_channel: # configure the sample rate and start the acquisition AI_single_channel.start_continuous_mode(p_sampleRate) print('start recording...') # read the value timeout = -1 value_array = AI_single_channel.read(p_sampleSize, timeout) print('stop recording') # stop signal acquisition AI_single_channel.stop_continuous_mode() return value_array[0]
def test_OpenWithInvalidChannelInDifferentialMode_ShowAssertion(self): with self.assertRaises(AssertionError): AnalogInput({ 'bank': bankA, 'channel': AIChannel.AI4, 'mode': AIMode.DIFFERENTIAL })
class Test_AnalogInput_ReadSingleChannelFromBankB(unittest.TestCase): def setUp(self): self.AI_single_channel = AnalogInput({ 'bank': bankB, 'channel': AIChannel.AI7, 'range': AIRange.PLUS_OR_MINUS_5V, 'mode': AIMode.SINGLE_ENDED }) def tearDown(self): self.AI_single_channel.close() def checkMultiplePointsReadBackValues(self, value_array, number_of_samples): value_array_for_first_channel = value_array[0][0] self.assertEqual(len(value_array_for_first_channel), number_of_samples) for value in value_array_for_first_channel: self.assertEqual(float(value), pytest.approx(3.3, 0.1)) def test_ContinuousMode_Read2222PointsWith500SampleRateAnd2000Timeout_ReturnExpectedReadBack( self): number_of_samples = 2222 sample_rate = 1200 timeout = -1 self.AI_single_channel.start_continuous_mode(sample_rate) value_array = self.AI_single_channel.read(number_of_samples, timeout) self.AI_single_channel.stop_continuous_mode() self.checkMultiplePointsReadBackValues(value_array, number_of_samples)
def test_WriteSinglePoint_ReturnDifferentValueFromEachChannel(self): AO_multiple_channels = AnalogOutput({'bank': bank, 'channel': AOChannel.AO0}, {'bank': bank, 'channel': AOChannel.AO1}) AI_multiple_channels = AnalogInput({'bank': bank, 'channel': AIChannel.AI0}, {'bank': bank, 'channel': AIChannel.AI3}) input_value_for_single_channels = 2.5 input_value_for_multiple_channels = 3.3 AO_multiple_channels.write(input_value_for_multiple_channels) self.AO_single_channel.write(input_value_for_single_channels) value_array = AI_multiple_channels.read() for index, value in enumerate(value_array): if index == 0: self.assertEqual(value, pytest.approx(input_value_for_single_channels, 0.1)) else: self.assertEqual(value, pytest.approx(input_value_for_multiple_channels, 0.1)) AO_multiple_channels.close() AI_multiple_channels.close()
class Test_AnalogOutput_WriteSingleChannel(unittest.TestCase): def setUp(self): self.AO_single_channel = AnalogOutput({ 'bank': bank, 'channel': AOChannel.AO0 }) self.AI_single_channel = AnalogInput({ 'bank': bank, 'channel': AIChannel.AI0 }) def tearDown(self): self.AO_single_channel.close() self.AI_single_channel.close() def test_OpeWriteMultiplePointsCloseRepeatedly_DoesnotShowAnyError(self): sample_rate = 1000 input_value = [1.1, 2.2] AO0_in_bank_A = AnalogOutput({ 'bank': Bank.A, 'channel': AOChannel.AO0 }) AO0_in_bank_B = AnalogOutput({ 'bank': Bank.B, 'channel': AOChannel.AO0 }) AO0_in_bank_A.write(input_value, sample_rate) AO0_in_bank_B.write(input_value, sample_rate) AO0_in_bank_B.close() AO0_in_bank_A.write(input_value, sample_rate) AO0_in_bank_A.close() def test_WriteLargeNumberOfValuesWithTenThousandSampleRate_DoesnotShowAnyError( self): sample_rate = 10000 input_value = [3.3 for i in range(55555)] self.AO_single_channel.write(input_value, sample_rate)
class Test_AnalogInput_CalculateSampleRateToTicks(unittest.TestCase): @classmethod def setUpClass(self): self.AI = AnalogInput({'bank': bankA, 'channel': AIChannel.AI2}) @classmethod def tearDownClass(self): self.AI.close() def test_GivenMin_ReturnExpectedCountAndSampleRate(self): count, actual_sample_rate = self.AI.calculate_sample_rate_to_ticks( limits['sampleRate']['min']) self.assertEqual(count, 40000) self.assertEqual(actual_sample_rate, 1000) def test_GivenAThousand_ReturnExpectedCountAndSampleRate(self): count, actual_sample_rate = self.AI.calculate_sample_rate_to_ticks( 1000) self.assertEqual(count, 40000) self.assertEqual(actual_sample_rate, 1000) def test_GivenFiveThousand_ReturnExpectedCountAndSampleRate(self): count, actual_sample_rate = self.AI.calculate_sample_rate_to_ticks( 5000) self.assertEqual(count, 8000) self.assertEqual(actual_sample_rate, 5000) def test_GivenTenThousand_ReturnExpectedCountAndSampleRate(self): count, actual_sample_rate = self.AI.calculate_sample_rate_to_ticks( 10000) self.assertEqual(count, 4000) self.assertEqual(actual_sample_rate, 10000) def test_GivenFiftyThousand_ReturnExpectedCountAndSampleRate(self): count, actual_sample_rate = self.AI.calculate_sample_rate_to_ticks( 50000) self.assertEqual(count, 1333) self.assertEqual(actual_sample_rate, pytest.approx(30007.5, 0.01)) def test_GivenMax_ReturnExpectedCountAndSampleRate(self): count, actual_sample_rate = self.AI.calculate_sample_rate_to_ticks( limits['sampleRate']['max']) self.assertEqual(count, 1333) self.assertEqual(actual_sample_rate, pytest.approx(30007.5, 0.01))
def test_GivenBankAAI0AI1AndBankBAI0AI1_Write_ReturnExpectedReadBack(self): def __read(number_of_times_to_read, read_object, number_of_samples_to_read, timeout, expected_value): title = {'A': 'Bank A AI0 - AO0 and AI1 - AO3', 'B': 'Bank B AI0 - AO1 and AI0 - AO3'} result = {'A': [[],[]], 'B': [[],[]]} for i in range(0, number_of_times_to_read): value_array = read_object.read(number_of_samples_to_read, timeout) # print(value_array) bank_A_values = value_array[0] bank_B_values = value_array[1] result['A'][0].append(bank_A_values[0]) result['A'][1].append(bank_A_values[1]) result['B'][0].append(bank_B_values[0]) result['B'][1].append(bank_B_values[1]) def __print(bank): print(title[bank]) for index, channel in enumerate(result[bank]): for values_per_channel in channel: values = [ round(value) for value in values_per_channel ] print('continuous: %s should almost equal to %s and continuous from the last read' % (values, expected_value[bank][index])) __print(Bank.A.value) __print(Bank.B.value) AO = AnalogOutput({'bank': Bank.B, 'channel': AOChannel.AO0}, {'bank': Bank.B, 'channel': AOChannel.AO1}, {'bank': bank, 'channel': AOChannel.AO0}, {'bank': bank, 'channel': AOChannel.AO1}) AI = AnalogInput({'bank': Bank.B, 'channel': AIChannel.AI0}, {'bank': Bank.B, 'channel': AIChannel.AI3}, {'bank': bank, 'channel': AIChannel.AI0}, {'bank': bank, 'channel': AIChannel.AI3}) timeout = -1 sample_rate = 1000 input_value = [[1,2,3,4,5,6,7,8,9,10], [10,9,8,7,6,5,4,3,2,1], [2,3,4,5,6,2,3,4,5,6], [7,6,5,4,3,7,6,5,4,3]] number_of_samples_to_read = 5 expected_value = { 'A': [self.__convert_list_to_string(input_value[2]), self.__convert_list_to_string(input_value[3])], 'B': [self.__convert_list_to_string(input_value[0]), self.__convert_list_to_string(input_value[1])] } irq_thread = threading.Thread(target=self.__writing_thread, args=(AO, input_value, timeout)) AO.start_continuous_mode(input_value, sample_rate, timeout) self.__start_thread(irq_thread) AI.start_continuous_mode(sample_rate) __read(2, AI, number_of_samples_to_read, timeout, expected_value) self.__close_thread(irq_thread) AO.close() AI.close()
def test_OpenTwoChannelsInBankAAndThreeChannelsInBankB_HaveExpectedCnfgvalue( self): expectedResults = [{ 'bank': 'A', 'channel': AIChannel.AI0, 'cnfgval': 8 }, { 'bank': 'A', 'channel': AIChannel.AI5, 'cnfgval': 13 }, { 'bank': 'B', 'channel': AIChannel.AI3, 'cnfgval': 11 }, { 'bank': 'B', 'channel': AIChannel.AI2 + 8, 'cnfgval': 2 }, { 'bank': 'B', 'channel': AIChannel.AI4, 'cnfgval': 12 }] AI = AnalogInput({ 'bank': bankA, 'channel': AIChannel.AI0 }, { 'bank': bankB, 'channel': AIChannel.AI3 }, { 'bank': bankB, 'channel': AIChannel.AI2, 'mode': AIMode.DIFFERENTIAL }, { 'bank': bankA, 'channel': AIChannel.AI5 }, { 'bank': bankB, 'channel': AIChannel.AI4 }) AI.read() for index, channel_settings in enumerate(AI.channel_list): self.assertEqual(channel_settings['bank'], expectedResults[index]['bank']) self.assertEqual(channel_settings['channel'], expectedResults[index]['channel']) self.assertEqual(channel_settings['cnfgval'], expectedResults[index]['cnfgval']) AI.close()
def test_GivenBankAAI0AI1AndBankBAI0_WriteWithDifferentValuesLength_ReturnExpectedReadBack(self): def __read(number_of_times_to_read, read_object, number_of_samples_to_read, timeout, expected_value): # result = [A: [AO0, AO1], B: [AO0]] result = [[[],[]], [[],[]]] for i in range(0, number_of_times_to_read): value_array = read_object.read(number_of_samples_to_read, timeout) bank_A_values = value_array[0] bank_B_values = value_array[1] result[0][0].append(bank_A_values[0]) result[0][1].append(bank_A_values[1]) result[1][0].append(bank_B_values[0]) def __print(channels, bank): for channel in channels: for index, values_per_channel in enumerate(channel): values = [ round(value) for value in values_per_channel ] if bank == Bank.A.value: print('continuous: %s should almost equal to %s and continuous from the last read' % (values, expected_value[index])) else: print('continuous: %s should almost equal to %s and continuous from the last read' % (values, expected_value[2])) print('Bank A AI0 - AO0 and AI1 - AO3') __print(result[0], Bank.A.value) print('Bank B AI0 - AO0') __print(result[1], Bank.B.value) AO = AnalogOutput({'bank': Bank.B, 'channel': AOChannel.AO0}, {'bank': bank, 'channel': AOChannel.AO0}, {'bank': bank, 'channel': AOChannel.AO1}) AI = AnalogInput({'bank': Bank.B, 'channel': AIChannel.AI0}, {'bank': bank, 'channel': AIChannel.AI0}, {'bank': bank, 'channel': AIChannel.AI3}) timeout = -1 sample_rate = 1000 input_value = [[0,1,2,3,4,5,0,1,2,3,4,5], [7,8,9,10,7,8,9,10,7,8,9,10], [1,2,3,4,5,1,2,3,4,5]] number_of_samples_to_read = 4 expected_value = [self.__convert_list_to_string(input_value[1]), self.__convert_list_to_string(input_value[2]), self.__convert_list_to_string(input_value[0])] irq_thread = threading.Thread(target=self.__writing_thread, args=(AO, input_value, timeout)) AO.start_continuous_mode(input_value, sample_rate, timeout) self.__start_thread(irq_thread) AI.start_continuous_mode(sample_rate) __read(2, AI, number_of_samples_to_read, timeout, expected_value) self.__close_thread(irq_thread) AO.close() AI.close()
def openAndCheck(bank): AI = AnalogInput({ 'bank': bank, 'channel': AIChannel.AI0 }, { 'bank': bank, 'channel': AIChannel.AI1 }, { 'bank': bank, 'channel': AIChannel.AI2 }, { 'bank': bank, 'channel': AIChannel.AI3 }, { 'bank': bank, 'channel': AIChannel.AI4 }, { 'bank': bank, 'channel': AIChannel.AI5 }, { 'bank': bank, 'channel': AIChannel.AI6 }, { 'bank': bank, 'channel': AIChannel.AI7 }, { 'bank': bank, 'channel': AIChannel.AI0, 'mode': AIMode.DIFFERENTIAL }, { 'bank': bank, 'channel': AIChannel.AI1, 'mode': AIMode.DIFFERENTIAL }, { 'bank': bank, 'channel': AIChannel.AI2, 'mode': AIMode.DIFFERENTIAL }, { 'bank': bank, 'channel': AIChannel.AI3, 'mode': AIMode.DIFFERENTIAL }) AI.read() for index, channel_settings in enumerate(AI.channel_list): self.assertEqual(channel_settings['channel'], expectedResults['channel'][index]) self.assertEqual(channel_settings['cnfgval'], expectedResults['cnfgval'][index]) AI.close()
def test_GivenBankBAI0AndBankAAI1_Write_ReturnExpectedReadBack(self): def __read(number_of_times_to_read, read_object, number_of_samples_to_read, timeout, expected_value_for_bank_A, expected_value_for_bank_B): result = [[], []] for i in range(0, number_of_times_to_read): value_array = read_object.read(number_of_samples_to_read, timeout) result[0].append(value_array[0]) result[1].append(value_array[1]) def __print(channels, expected_value): for channel in channels: for values_per_channel in channel: values = [ round(value) for value in values_per_channel ] print('continuous: %s should almost equal to %s and continuous from the last read' % (values, expected_value)) print('Bank A AI3 - AO1') __print(result[0], expected_value_for_bank_A) print('Bank B AI0 - AO0') __print(result[1], expected_value_for_bank_B) AO = AnalogOutput({'bank': Bank.B, 'channel': AOChannel.AO0}, {'bank': bank, 'channel': AOChannel.AO1}) AI = AnalogInput({'bank': Bank.B, 'channel': AIChannel.AI0}, {'bank': bank, 'channel': AIChannel.AI3}) timeout = -1 sample_rate = 1000 input_value = [[1,2,3,4,5,6,7,8,9,10], [10,9,8,7,6,5,4,3,2,1]] number_of_samples_to_read = 5 expected_value_for_bank_B = self.__convert_list_to_string(input_value[0]) expected_value_for_bank_A = self.__convert_list_to_string(input_value[1]) irq_thread = threading.Thread(target=self.__writing_thread, args=(AO, input_value, timeout)) AO.start_continuous_mode(input_value, sample_rate, timeout) self.__start_thread(irq_thread) AI.start_continuous_mode(sample_rate) __read(2, AI, number_of_samples_to_read, timeout, expected_value_for_bank_A, expected_value_for_bank_B) self.__close_thread(irq_thread) AO.close() AI.close()
def __write_and_read(input_value): AO_single_channel = AnalogOutput({'bank': Bank.B, 'channel': AOChannel.AO0}) AI_single_channel = AnalogInput({'bank': Bank.B, 'channel': AIChannel.AI0}) timeout = -1 sample_rate = 1000 number_of_samples_to_read = 10 expected_value = self.__convert_list_to_string(input_value) irq_thread = threading.Thread(target=self.__writing_thread, args=(AO_single_channel, input_value, timeout)) AO_single_channel.start_continuous_mode(input_value, sample_rate, timeout) self.__start_thread(irq_thread) AI_single_channel.start_continuous_mode(sample_rate) self.__read(2, AI_single_channel, number_of_samples_to_read, timeout, expected_value) self.__close_thread(irq_thread) AO_single_channel.close() AI_single_channel.close()
def test_Given7PointsWith1000SampleRateOnBankA_WriteThenCloseWithoutStop_ReturnExpectedReadBack(self): AO_single_channel = AnalogOutput({'bank': bank, 'channel': AOChannel.AO0}) AI_single_channel = AnalogInput({'bank': bank, 'channel': AIChannel.AI0}) timeout = -1 sample_rate = 2000 input_value = [[9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0]] number_of_samples_to_read = 7 expected_value = self.__convert_list_to_string(input_value) irq_thread = threading.Thread(target=self.__writing_thread, args=(AO_single_channel, input_value, timeout)) AO_single_channel.start_continuous_mode(input_value, sample_rate, timeout) self.__start_thread(irq_thread) AI_single_channel.start_continuous_mode(sample_rate) self.__read(3, AI_single_channel, number_of_samples_to_read, timeout, expected_value) self.__close_thread(irq_thread) AO_single_channel.close() AI_single_channel.close()
class Test_AnalogInput_ReadAssertion(unittest.TestCase): def setUp(self): self.AI_single_channel = AnalogInput({ 'bank': bankA, 'channel': AIChannel.AI2, 'range': AIRange.PLUS_OR_MINUS_5V, 'mode': AIMode.SINGLE_ENDED }) self.AI_multiple_channels = AnalogInput( { 'bank': bankA, 'channel': AIChannel.AI2, 'range': AIRange.PLUS_OR_MINUS_10V }, { 'bank': bankA, 'channel': AIChannel.AI2, 'mode': AIMode.DIFFERENTIAL }) @classmethod def setUpClass(self): self.approx_number_of_samples = 10 self.approx_samples_rate = 1000 def tearDown(self): self.AI_single_channel.close() self.AI_multiple_channels.close() def test_PassOneArgumentToReadOneChannel_ShowAssertion(self): with self.assertRaises(TypeError): self.AI_single_channel.read(10) def test_PassThreeArgumentsToReadOneChannel_ShowAssertion(self): with self.assertRaises(TypeError): self.AI_single_channel.read(10, 10, 10) def test_PassNumberOfSamplesThatGreaterThanMaxToReadOneChannel_ShowAssertion( self): with self.assertRaises(AssertionError): self.AI_single_channel.read(limits['numberOfSamples']['max'] + 1, self.approx_samples_rate) def test_PassNumberOfSamplesThatLessThanMinToReadOneChannel_ShowAssertion( self): with self.assertRaises(AssertionError): self.AI_single_channel.read(limits['numberOfSamples']['min'] - 1, self.approx_samples_rate) def test_PassMaxSampleRateToReadTwoChannels_ShowAssertion(self): with self.assertRaises(AssertionError): self.AI_multiple_channels.read(self.approx_number_of_samples, limits['sampleRate']['max']) def test_PassSampleRateThatGreaterThanMaxToReadOneChannel_ShowAssertion( self): with self.assertRaises(AssertionError): self.AI_single_channel.read(self.approx_number_of_samples, limits['sampleRate']['max'] + 1) def test_PassSampleRateThatLessThanMinToReadOneChannel_ShowAssertion(self): with self.assertRaises(AssertionError): self.AI_single_channel.read(self.approx_number_of_samples, limits['sampleRate']['min'] - 1)
# specify the bank, channel, range, and mode ai_bank = Bank.A ai_channel = AIChannel.AI0 ai_range = AIRange.PLUS_OR_MINUS_10V ai_mode = AIMode.SINGLE_ENDED ############################################################################## # Section 1: Single Point (1 Sample) # Use the read function to read a single point of data back from the channel. The # hardware acquires one sample for a channel. ############################################################################## print('Single Point, Single Channel') # configure the AI channel with AnalogInput({ 'bank': ai_bank, 'channel': ai_channel, 'range': ai_range, 'mode': ai_mode }) as AI_single_channel: # read from the AI channel and display the values 20 times for i in range(0, 20): # read the value value_array = AI_single_channel.read() # print the value print(value_array[0]) # add a short delay before acquiring the next data point time.sleep(0.001) ############################################################################## # Section 2: Multiple Points (n samples) # Use the read function to read multiple points of data from the channel. The
def test_OpenWithoutRange_DoesnotShowAssertion(self): AnalogInput({ 'bank': bankA, 'channel': AIChannel.AI0, 'mode': AIMode.SINGLE_ENDED })
def setUpClass(self): self.AI = AnalogInput({'bank': bankA, 'channel': AIChannel.AI2})
class Test_AnalogInput_StartAssertion(unittest.TestCase): def setUp(self): self.AI_single_channel = AnalogInput({ 'bank': bankA, 'channel': AIChannel.AI2, 'range': AIRange.PLUS_OR_MINUS_5V, 'mode': AIMode.SINGLE_ENDED }) self.AI_multiple_channels = AnalogInput( { 'bank': bankA, 'channel': AIChannel.AI2, 'range': AIRange.PLUS_OR_MINUS_10V }, { 'bank': bankA, 'channel': AIChannel.AI2, 'mode': AIMode.DIFFERENTIAL }) @classmethod def setUpClass(self): self.sample_rate_limits_for_multiple_channels = { 'max': 250000, 'min': 1 } def tearDown(self): self.AI_single_channel.close() self.AI_multiple_channels.close() def test_PassSampleRateThatLessThanMinToSingleChannel_ShowAssertion(self): with self.assertRaises(AssertionError): self.AI_single_channel.start_continuous_mode( limits['sampleRate']['min'] - 1) def test_PassSampleRateThatGreaterThanMaxToSingleChannel_ShowAssertion( self): with self.assertRaises(AssertionError): self.AI_single_channel.start_continuous_mode( limits['sampleRate']['max'] + 1) def test_PassSampleRateThatLessThanMinToTwoChannels_ShowAssertion(self): with self.assertRaises(AssertionError): self.AI_multiple_channels.start_continuous_mode( self.sample_rate_limits_for_multiple_channels['min'] - 1) def test_PassSampleRateThatGreaterThanMaxToTwoChannels_ShowAssertion(self): with self.assertRaises(AssertionError): self.AI_multiple_channels.start_continuous_mode( self.sample_rate_limits_for_multiple_channels['max'] + 1)
def setUp(self): self.AO_multiple_channels = AnalogOutput({'bank': bank, 'channel': AOChannel.AO0}, {'bank': bank, 'channel': AOChannel.AO1}) self.AI_multiple_channels = AnalogInput({'bank': bank, 'channel': AIChannel.AI0}, {'bank': bank, 'channel': AIChannel.AI3})
class Test_AnalogInputAndOutput_WriteSingleChannel(unittest.TestCase): def setUp(self): self.AO_single_channel = AnalogOutput({'bank': bank, 'channel': AOChannel.AO0}) self.AI_single_channel = AnalogInput({'bank': bank, 'channel': AIChannel.AI0}) def tearDown(self): self.AO_single_channel.close() self.AI_single_channel.close() def __start_thread(self, irq_thread): irq_thread.start() self.run = True def __close_thread(self, irq_thread): self.run = False irq_thread.join() def __reading_thread(self, read_object, number_of_samples, sample_rate, expected_value): value_array = None while self.run: value_array = read_object.read(number_of_samples, sample_rate)[0][0] values = [ round(value) for value in value_array ] print('%s should almost equal to %s' % (values, expected_value)) def test_WriteSinglePoint_ReturnExpectedReadBack(self): input_value = 2.0 self.AO_single_channel.write(input_value) value_array = self.AI_single_channel.read() for value in value_array: self.assertEqual(value, pytest.approx(input_value, 0.1)) def test_WriteSinglePoint_ReturnDifferentValueFromEachChannel(self): AO_multiple_channels = AnalogOutput({'bank': bank, 'channel': AOChannel.AO0}, {'bank': bank, 'channel': AOChannel.AO1}) AI_multiple_channels = AnalogInput({'bank': bank, 'channel': AIChannel.AI0}, {'bank': bank, 'channel': AIChannel.AI3}) input_value_for_single_channels = 2.5 input_value_for_multiple_channels = 3.3 AO_multiple_channels.write(input_value_for_multiple_channels) self.AO_single_channel.write(input_value_for_single_channels) value_array = AI_multiple_channels.read() for index, value in enumerate(value_array): if index == 0: self.assertEqual(value, pytest.approx(input_value_for_single_channels, 0.1)) else: self.assertEqual(value, pytest.approx(input_value_for_multiple_channels, 0.1)) AO_multiple_channels.close() AI_multiple_channels.close() def test_WriteTensPointWithOneThousandSampleRate_ReturnExpectedReadBack(self): sample_rate = 1000 input_value = [1,1,2,2,3,3,4,4,5,5] expected_value = '1,1,2,2,3,3,4,4,5,5' irq_thread = threading.Thread(target=self.__reading_thread, args=(self.AI_single_channel, len(input_value), sample_rate, expected_value)) self.__start_thread(irq_thread) self.AO_single_channel.write(input_value, sample_rate) self.__close_thread(irq_thread) def test_WriteTenPointsThenWriteSinglePointInBankB_ReturnExpectedReadBack(self): AO0 = AnalogOutput({'bank': Bank.B, 'channel': AOChannel.AO0}) AI0 = AnalogInput({'bank': Bank.B, 'channel': AIChannel.AI0}) def __multipleWriteAndRead(): sample_rate = 1000 input_value = [7,7,7,7,7,7,7,5,5,10] expected_value = '7,7,7,7,7,7,7,5,5,10' irq_thread = threading.Thread(target=self.__reading_thread, args=(AI0, len(input_value), sample_rate, expected_value)) self.__start_thread(irq_thread) AO0.write(input_value, sample_rate) self.__close_thread(irq_thread) def __singleWriteAndRead(): input_value = 3.2 AO0.write(input_value) value = AI0.read()[0] self.assertEqual(value, pytest.approx(input_value, 0.1)) __multipleWriteAndRead() __singleWriteAndRead() AO0.close() AI0.close()