Read Functions

HIL.read_analog(channels, num_channels, buffer)

Reads from analog inputs immediately. The function does not return until the data has been read.

Parameters
  • channels (array_like) – An array containing the channel numbers of the analog inputs to be read.

  • num_channels (int) – The number of channels specified in the channels array.

  • buffer (array_like) – An array for receiving the voltage values read from the analog inputs. The array must contain num_channels elements. Each element in the returned buffer array will correspond to the same element in the channels array.

Raises

HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.

Examples

Read the first four analog input channels.

Using array:

>>> from array import array
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   channels = array('I', [0, 1, 2, 3])
>>>   num_channels = len(channels)
>>>   buffer = array('d', [0.0] * num_channels)
>>>   card.read_analog(channels, num_channels, buffer)
>>>   # ...
...
>>> finally:
>>>   card.close()

Using numpy:

>>> import numpy as np
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   channels = np.array([0, 1, 2, 3], dtype=np.uint32)
>>>   num_channels = len(channels)
>>>   buffer = np.zeros(num_channels, dtype=np.float64)
>>>   card.read_analog(channels, num_channels, buffer)
>>>   # ...
...
>>> finally:
>>>   card.close()
HIL.read_analog_codes(channels, num_channels, buffer)

Reads from the specified analog input channels immediately. The function does not return until the data has been read. The data returned is the raw integer A/D converter values, not voltages.

Parameters
  • channels (array_like) – An array containing the channel numbers of the analog inputs to be read.

  • num_channels (int) – The number of channels specified in the channels array.

  • buffer (array_like) – An array for receiving the raw A/D converter values read from the analog inputs. The array must contain num_channels elements. Each element in the returned buffer array will correspond to the same element in the channels array.

Raises

HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.

Examples

Read the first four analog input channels as raw A/D converter values.

Using array:

>>> from array import array
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   channels = array('I', [0, 1, 2, 3])
>>>   num_channels = len(channels)
>>>   buffer = array('i', [0] * num_channels)
>>>   card.read_analog_codes(channels, num_channels, buffer)
>>>   # ...
...
>>> finally:
>>>   card.close()

Using numpy:

>>> import numpy as np
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   channels = np.array([0, 1, 2, 3], dtype=np.uint32)
>>>   num_channels = len(channels)
>>>   buffer = np.zeros(num_channels, dtype=np.int32)
>>>   card.read_analog_codes(channels, num_channels, buffer)
>>>   # ...
...
>>> finally:
>>>   card.close()
HIL.read_encoder(channels, num_channels, buffer)

Reads from encoder inputs immediately. The function does not return until the data has been read.

Parameters
  • channels (array_like) – An array containing the channel numbers of the encoder inputs to be read.

  • num_channels (int) – The number of channels specified in the channels array.

  • buffer (array_like) – An array for receiving the count values read from the encoder inputs. The array must contain num_channels elements. Each element in the returned buffer array will correspond to the same element in the channels array.

Raises

HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.

Examples

Read the first four encoder input channels.

Using array:

>>> from array import array
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   channels = array('I', [0, 1, 2, 3])
>>>   num_channels = len(channels)
>>>   buffer = array('i', [0] * num_channels)
>>>   card.read_encoder(channels, num_channels, buffer)
>>>   # ...
...
>>> finally:
>>>   card.close()

Using numpy:

>>> import numpy as np
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   channels = np.array([0, 1, 2, 3], dtype=np.uint32)
>>>   num_channels = len(channels)
>>>   buffer = np.zeros(num_channels, dtype=np.int32)
>>>   card.read_encoder(channels, num_channels, buffer)
>>>   # ...
...
>>> finally:
>>>   card.close()
HIL.read_digital(channels, num_channels, buffer)

Reads from digital inputs immediately. The function does not return until the data has been read.

Parameters
  • channels (array_like) – An array containing the channel numbers of the digital inputs to be read.

  • num_channels (int) – The number of channels specified in the channels array.

  • buffer (array_like) – An array for receiving the binary values read from the digital inputs. The array must contain num_channels elements. Each element in the returned buffer array will correspond to the same element in the channels array.

Raises

HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.

Examples

Read the first four digital input channels.

Using array:

>>> from array import array
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   channels = array('I', [0, 1, 2, 3])
>>>   num_channels = len(channels)
>>>   buffer = array('b', [0] * num_channels)
>>>   card.read_digital(channels, num_channels, buffer)
>>>   # ...
...
>>> finally:
>>>   card.close()

Using numpy:

>>> import numpy as np
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   channels = np.array([0, 1, 2, 3], dtype=np.uint32)
>>>   num_channels = len(channels)
>>>   buffer = np.zeros(num_channels, dtype=np.int8)
>>>   card.read_digital(channels, num_channels, buffer)
>>>   # ...
...
>>> finally:
>>>   card.close()
HIL.read_other(channels, num_channels, buffer)

Reads from the specified other input channels immediately. The function does not return until the data has been read.

Parameters
  • channels (array_like) – An array containing the channel numbers of the other inputs to be read.

  • num_channels (int) – The number of channels specified in the channels array.

  • buffer (array_like) – An array for receiving the values read from the other inputs. The array must contain num_channels elements. Each element in the returned buffer array will correspond to the same element in the channels array.

Raises

HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.

Examples

Read the first four encoder input channels.

Using array:

>>> from array import array
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   channels = array('I', [0, 1, 2, 3])
>>>   num_channels = len(channels)
>>>   buffer = array('d', [0.0] * num_channels)
>>>   card.read_other(channels, num_channels, buffer)
>>>   # ...
...
>>> finally:
>>>   card.close()

Using numpy:

>>> import numpy as np
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   channels = np.array([0, 1, 2, 3], dtype=np.uint32)
>>>   num_channels = len(channels)
>>>   buffer = np.zeros(num_channels, dtype=np.float64)
>>>   card.read_other(channels, num_channels, buffer)
>>>   # ...
...
>>> finally:
>>>   card.close()
HIL.read(analog_channels, num_analog_channels, encoder_channels, num_encoder_channels, digital_channels, num_digital_channels, other_channels, num_other_channels, analog_buffer, encoder_buffer, digital_buffer, other_buffer)

Reads from the specified input channels immediately. The function does not return until the data has been read.

Parameters
  • analog_channels (array_like or None) – An array containing the channel numbers of the analog outputs to be read. If no analog channels are required, then this parameter may be None. In this case, num_analog_channels must be zero.

  • num_analog_channels (int) – The number of channels specified in the analog_channels array. This parameter may be zero.

  • encoder_channels (array_like or None) – An array containing the channel numbers of the encoder inputs to be read. If no encoder channels are required, then this parameter may be None. In this case, num_pwm_channels must be zero.

  • num_encoder_channels (int) – The number of channels specified in the pwm_channels array. This parameter may be zero.

  • digital_channels (array_like or None) – An array containing the channel numbers of the digital outputs to be read. If no digital channels are required, then this parameter may be None. In this case, num_digital_channels must be zero.

  • num_digital_channels (int) – The number of channels specified in the digital_channels array. This parameter may be zero.

  • other_channels (array_like or None) – An array containing the channel numbers of the other outputs to be read. If no other channels are required, then this parameter may be None. In this case, num_other_channels must be zero.

  • num_other_channels (int) – The number of channels specified in the other_channels array. This parameter may be zero.

  • analog_buffer (array_like or None) – An array for receiving the voltage values read from the analog inputs. The array must contain num_analog_channels elements. Each element in the returned analog_buffer array will correspond to the same element in the analog_channels array. If no analog channels were specified, then this parameter may be None.

  • encoder_buffer (array_like or None) – An array for receiving the count values read from the encoder inputs. The array must contain num_encoder_channels elements. Each element in the returned encoder_buffer array will correspond to the same element in the encoder_channels array. If no digital channels were specified, then this parameter may be None.

  • digital_buffer (array_like or None) – An array for receiving the binary values read from the digital inputs. The array must contain num_digital_channels elements. Each element in the returned digital_buffer array will correspond to the same element in the digital_channels array. If no digital channels were specified, then this parameter may be None.

  • other_buffer (array_like or None) – An array for receiving the values read from the other inputs. The array must contain num_other_channels elements. Each element in the returned other_buffer array will correspond to the same element in the other_channels array. If no other channels were specified, then this parameter may be None.

Raises

HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.

Warning

Many cards allow the digital I/O lines to be programmed as inputs or outputs. The digital I/O lines are configured as inputs or outputs using the set_digital_directions function. All the channels which will be used as digital inputs must be configured as inputs using this function. Failure to configure the digital I/O may result in hardware conflicts where an output is connected to an output!

Examples

Read two analog input channels, two encoder input channels, and four digital input channels.

Using array:

>>> from array import array
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   analog_channels = array('I', [0, 1])
>>>   encoder_channels = array('I', [0, 1])
>>>   digital_channels = array('I', [0, 1, 2, 3])
>>>   num_analog_channels = len(analog_channels)
>>>   num_encoder_channels = len(encoder_channels)
>>>   num_digital_channels = len(digital_channels)
>>>   analog_buffer = array('d', [0.0] * num_analog_channels)
>>>   encoder_buffer = array('i', [0] * num_encoder_channels)
>>>   digital_buffer = array('b', [0] * num_digital_channels)
>>>   card.read(analog_channels, num_analog_channels,
...             encoder_channels, num_encoder_channels,
...             digital_channels, num_digital_channels,
...             None, 0,
...             analog_buffer,
...             encoder_buffer,
...             digital_buffer,
...             None)
>>>   # ...
...
>>> finally:
>>>   card.close()

Using numpy:

>>> import numpy as np
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   analog_channels = np.array([0, 1], dtype=np.uint32)
>>>   encoder_channels = np.array([0, 1], dtype=np.uint32)
>>>   digital_channels = np.array([0, 1, 2, 3], dtype=np.uint32)
>>>   num_analog_channels = len(analog_channels)
>>>   num_encoder_channels = len(encoder_channels)
>>>   num_digital_channels = len(digital_channels)
>>>   analog_buffer = np.zeros(num_analog_channels, dtype=np.float64)
>>>   encoder_buffer = np.zeros(num_encoder_channels, dtype=np.int32)
>>>   digital_buffer = np.zeros(num_digital_channels, dtype=np.int8)
>>>   card.read(analog_channels, num_analog_channels,
...             encoder_channels, num_encoder_channels,
...             digital_channels, num_digital_channels,
...             None, 0,
...             analog_buffer,
...             encoder_buffer,
...             digital_buffer,
...             None)
>>>   # ...
...
>>> finally:
>>>   card.close()