Read/Write Functions

HIL.read_analog_write_analog(input_channels, num_input_channels, output_channels, num_output_channels, input_buffer, output_buffer)

Reads from the specified analog input channels and writes to the specified analog output channels in a single function call. The write operation occurs immediately following the read operation. Since the read-write operation occurs at the lowest level, the read and write occur virtually concurrently. The function does not return until the data has been read and written.

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

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

  • output_channels (array_like) – An array containing the channel numbers of the analog outputs to be written.

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

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

  • output_buffer (array_like) – An array of voltage values to write to the analog outputs. The array must contain num_output_channels elements. Each element in the output_buffer array corresponds to the same element in the output_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 and writes to the first two analog output channels.

Using array:

>>> from array import array
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   input_channels = array('I', [0, 1, 2, 3])
>>>   output_channels = array('I', [0, 1])
>>>   num_input_channels = len(input_channels)
>>>   num_output_channels = len(output_channels)
>>>   input_buffer = array('d', [0.0] * num_input_channels])
>>>   output_buffer = array('d', [0.5, 1.5])
>>>   card.read_analog_write_analog(input_channels, num_input_channels,
...                                 output_channels, num_output_channels,
...                                 input_buffer,
...                                 output_buffer)
>>>   # ...
...
>>> finally:
>>>   card.close()

Using numpy:

>>> import numpy as np
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   input_channels = np.array([0, 1, 2, 3], dtype=np.uint32)
>>>   output_channels = np.array([0, 1], dtype=np.uint32)
>>>   num_input_channels = len(input_channels)
>>>   num_output_channels = len(output_channels)
>>>   input_buffer = np.zeros(num_input_channels, dtype=np.float64)
>>>   output_buffer = np.array([0.5, 1.5], dtype=np.float64)
>>>   card.read_analog_write_analog(input_channels, num_input_channels,
...                                 output_channels, num_output_channels,
...                                 input_buffer,
...                                 output_buffer)
>>>   # ...
...
>>> finally:
>>>   card.close()
HIL.read_encoder_write_pwm(input_channels, num_input_channels, output_channels, num_output_channels, input_buffer, output_buffer)

Reads from the specified encoder input channels and writes to the specified PWM output channels in a single function call. The write operation occurs immediately following the read operation. Since the read-write operation occurs at the lowest level, the read and write occur virtually concurrently. The function does not return until the data has been read and written.

The interpretation of the PWM data to be written depends upon the PWM mode. Typically, the data is interpreted as a duty cycle, in which a magnitude of 0.0 denotes a 0% duty cycle and magnitude of 1.0 indicates a 100% duty cycle. The sign determines the polarity of the output for those boards supporting bidirectional PWM outputs. However, other PWM modes are possible with some boards. Refer to the set_pwm_mode function for details.

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

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

  • output_channels (array_like) – An array containing the channel numbers of the PWM outputs to be written.

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

  • input_buffer (array_like) – An array for receiving the counter values read from the encoder inputs. The array must contain num_input_channels elements. Each element in the returned input_buffer array will correspond to the same element in the input_channels array.

  • output_buffer (array_like) – An array containing the values to write to the PWM outputs. How these values are interpreted depends on the PWM mode. The PWM mode is configured using the set_pwm_mode function. The array must contain num_output_channels elements. Each element in the output_buffer array corresponds to the same element in the output_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 and writes to the first two PWM output channels.

Using array:

>>> from array import array
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   input_channels = array('I', [0, 1, 2, 3])
>>>   output_channels = array('I', [0, 1])
>>>   num_input_channels = len(input_channels)
>>>   num_output_channels = len(output_channels)
>>>   input_buffer = array('i', [0] * num_input_channels])
>>>   output_buffer = array('d', [0.7, 0.3])
>>>   card.read_encoder_write_pwm(input_channels, num_input_channels,
...                               output_channels, num_output_channels,
...                               input_buffer,
...                               output_buffer)
>>>   # ...
...
>>> finally:
>>>   card.close()

Using numpy:

>>> import numpy as np
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   input_channels = np.array([0, 1, 2, 3], dtype=np.uint32)
>>>   output_channels = np.array([0, 1], dtype=np.uint32)
>>>   num_input_channels = len(input_channels)
>>>   num_output_channels = len(output_channels)
>>>   input_buffer = np.zeros(num_input_channels, dtype=np.int32)
>>>   output_buffer = np.array([0.7, 0.3], dtype=np.float64)
>>>   card.read_encoder_write_pwm(input_channels, num_input_channels,
...                               output_channels, num_output_channels,
...                               input_buffer,
...                               output_buffer)
>>>   # ...
...
>>> finally:
>>>   card.close()
HIL.read_digital_write_digital(input_channels, num_input_channels, output_channels, num_output_channels, input_buffer, output_buffer)

Reads from digital inputs and writes to digital outputs immediately.

Reads from the specified digital input channels and writes to the specified digital output channels in a single function call. The write operation occurs immediately following the read operation. Since the read-write operation occurs at the lowest level the read and write occur virtually concurrently. The function does not return until the data has been read and written.

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

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

  • output_channels (array_like) – An array containing the channel numbers of the digital outputs to be written.

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

  • input_buffer (array_like) – An array for receiving the voltage values read from the digital inputs. The array must contain num_input_channels elements. Each element in the returned input_buffer array will correspond to the same element in the input_channels array.

  • output_buffer (array_like) – An array of voltage values to write to the digital outputs. The array must contain num_output_channels elements. Each element in the output_buffer array corresponds to the same element in the output_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 and writes to digital output channels 5 and 7.

Using array:

>>> from array import array
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   input_channels = array('I', [0, 1, 2, 3])
>>>   output_channels = array('I', [5, 7])
>>>   num_input_channels = len(input_channels)
>>>   num_output_channels = len(output_channels)
>>>   input_buffer = array('b', [0] * num_input_channels])
>>>   output_buffer = array('b', [1, 0])
>>>   card.read_digital_write_digital(input_channels, num_input_channels,
...                                   output_channels, num_output_channels,
...                                   input_buffer,
...                                   output_buffer)
>>>   # ...
...
>>> finally:
>>>   card.close()

Using numpy:

>>> import numpy as np
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   input_channels = np.array([0, 1, 2, 3], dtype=np.uint32)
>>>   output_channels = np.array([5, 7], dtype=np.uint32)
>>>   num_input_channels = len(input_channels)
>>>   num_output_channels = len(output_channels)
>>>   input_buffer = np.zeros(num_input_channels, dtype=np.int8)
>>>   output_buffer = np.array([1, 0], dtype=np.int8)
>>>   card.read_digital_write_digital(input_channels, num_input_channels,
...                                   output_channels, num_output_channels,
...                                   input_buffer,
...                                   output_buffer)
>>>   # ...
...
>>> finally:
>>>   card.close()
HIL.read_other_write_other(input_channels, num_input_channels, output_channels, num_output_channels, input_buffer, output_buffer)

Reads from digital inputs and writes to digital outputs immediately.

Reads from the specified digital input channels and writes to the specified digital output channels in a single function call. The write operation occurs immediately following the read operation. Since the read-write operation occurs at the lowest level the read and write occur virtually concurrently. The function does not return until the data has been read and written.

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

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

  • output_channels (array_like) – An array containing the channel numbers of the digital outputs to be written.

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

  • input_buffer (array_like) – An array for receiving the voltage values read from the digital inputs. The array must contain num_input_channels elements. Each element in the returned input_buffer array will correspond to the same element in the input_channels array.

  • output_buffer (array_like) – An array of voltage values to write to the digital outputs. The array must contain num_output_channels elements. Each element in the output_buffer array corresponds to the same element in the output_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 other input channels and writes to the first two other output channels.

Using array:

>>> from array import array
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   input_channels = array('I', [0, 1, 2, 3])
>>>   output_channels = array('I', [0, 1])
>>>   num_input_channels = len(input_channels)
>>>   num_output_channels = len(output_channels)
>>>   input_buffer = array('d', [0] * num_input_channels])
>>>   output_buffer = array('d', [0.5, 1.5])
>>>   card.read_other_write_other(input_channels, num_input_channels,
...                               output_channels, num_output_channels,
...                               input_buffer,
...                               output_buffer)
>>>   # ...
...
>>> finally:
>>>   card.close()

Using numpy:

>>> import numpy as np
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   input_channels = np.array([0, 1, 2, 3], dtype=np.uint32)
>>>   output_channels = np.array([0, 1], dtype=np.uint32)
>>>   num_input_channels = len(input_channels)
>>>   num_output_channels = len(output_channels)
>>>   input_buffer = np.zeros(num_input_channels, dtype=np.float64)
>>>   output_buffer = np.array([0.5, 1.5], dtype=np.float64)
>>>   card.read_other_write_other(input_channels, num_input_channels,
...                               output_channels, num_output_channels,
...                               input_buffer,
...                               output_buffer)
>>>   # ...
...
>>> finally:
>>>   card.close()
HIL.read_write(analog_input_channels, num_analog_input_channels, encoder_input_channels, num_encoder_input_channels, digital_input_channels, num_digital_input_channels, other_input_channels, num_other_input_channels, analog_output_channels, num_analog_output_channels, pwm_output_channels, num_pwm_output_channels, digital_output_channels, num_digital_output_channels, other_output_channels, num_other_output_channels, analog_input_buffer, encoder_input_buffer, digital_input_buffer, other_input_buffer, analog_output_buffer, pwm_output_buffer, digital_output_buffer, other_output_buffer)

Reads from inputs and writes to outputs immediately.

Reads from the specified analog, encoder, digital and/or other input channels and writes to the specified analog, PWM, digital and/or other output channels in a single function call. The write operation occurs immediately following the read operation. Since the read-write operation occurs at the lowest level the read and write occur virtually concurrently. The function does not return until the data has been read and written.

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

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

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

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

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

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

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

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

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

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

  • pwm_output_channels (array_like or None) – An array containing the channel numbers of the PWM outputs to be written. If no PWM output channels are required, then this parameter may be None. In this case, num_pwm_output_channels must be zero.

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

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

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

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

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

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

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

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

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

  • analog_output_buffer (array_like or None) – An array of voltage values to write to the analog outputs. The array must contain num_analog_output_channels elements. Each element in the analog_output_buffer array corresponds to the same element in the analog_output_channels array. If no analog output channels were specified, then this parameter may be None.

  • pwm_output_buffer (array_like or None) – An array containing the values to write to the PWM outputs. How these values are interpreted depends on the PWM mode. The PWM mode is configured using the hil_set_pwm_mode function. The array must contain num_pwm_output_channels elements. Each element in the pwm_output_buffer array corresponds to the same element in the pwm_output_channels array. If no PWM output channels were specified, then this parameter may be None.

  • digital_output_buffer (array_like or None) – An array of voltage values to write to the digital outputs. The array must contain num_digital_output_channels elements. Each element in the digital_output_buffer array corresponds to the same element in the digital_output_channels array. If no digital output channels were specified, then this parameter may be None.

  • other_output_buffer (array_like or None) – An array of values to write to the other outputs. The array must contain num_other_output_channels elements. Each element in the other_output_buffer array corresponds to the same element in the other_output_channels array. If no other output 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.

Examples

Write to two analog output channels, two PWM output channels, and four digital output channels.

Using array:

>>> from array import array
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   analog_input_channels = array('I', [0, 1])
>>>   encoder_input_channels = array('I', [0, 1])
>>>   digital_input_channels = array('I', [0, 1])
>>>   analog_output_channels = array('I', [0, 1])
>>>   digital_output_channels = array('I', [5, 6, 7])
>>>   num_analog_input_channels = len(analog_input_channels)
>>>   num_encoder_input_channels = len(encoder_input_channels)
>>>   num_digital_input_channels = len(digital_input_channels)
>>>   num_analog_output_channels = len(analog_output_channels)
>>>   num_digital_output_channels = len(digital_output_channels)
>>>   analog_input_buffer = array('d', [0.0] * num_analog_input_channels)
>>>   encoder_input_buffer = array('i', [0] * num_encoder_input_channels)
>>>   digital_input_buffer = array('b', [0] * num_digital_input_channels)
>>>   analog_output_buffer = array('d', [0.5, 1.5])
>>>   digital_output_buffer = array('b', [1, 0, 1])
>>>   card.read_write(analog_input_channels, num_analog_input_channels,
...                   encoder_input_channels, num_encoder_input_channels,
...                   digital_input_channels, num_digital_input_channels,
...                   None, 0,
...                   analog_output_channels, num_analog_output_channels,
...                   None, 0,
...                   digital_output_channels, num_digital_output_channels,
...                   None, 0,
...                   analog_input_buffer,
...                   encoder_input_buffer,
...                   digital_input_buffer,
...                   None,
...                   analog_output_buffer,
...                   None,
...                   digital_output_buffer,
...                   None)
>>>   # ...
...
>>> finally:
>>>   card.close()

Using numpy:

>>> import numpy as np
>>> from quanser.hardware import HIL
>>> card = HIL("q8_usb", "0")
>>> try:
>>>   analog_input_channels = np.array([0, 1], dtype=np.uint32)
>>>   encoder_input_channels = np.array([0, 1], dtype=np.uint32)
>>>   digital_input_channels = np.array([0, 1], dtype=np.uint32)
>>>   analog_output_channels = np.array([0, 1], dtype=np.uint32)
>>>   digital_output_channels = np.array([5, 6, 7], dtype=np.uint32)
>>>   num_analog_input_channels = len(analog_input_channels)
>>>   num_encoder_input_channels = len(encoder_input_channels)
>>>   num_digital_input_channels = len(digital_input_channels)
>>>   num_analog_output_channels = len(analog_output_channels)
>>>   num_digital_output_channels = len(digital_output_channels)
>>>   analog_input_buffer = np.zeros(num_analog_input_channels, dtype=np.float64)
>>>   encoder_input_buffer = np.zeros(num_encoder_input_channels, dtype=np.int32)
>>>   digital_input_buffer = np.zeros(num_digital_input_channels, dtype=np.int8)
>>>   analog_output_buffer = np.array([0.5, 1.5], dtype=np.float64)
>>>   digital_output_buffer = np.array([1, 0, 1], dtype=np.int8)
>>>   card.read_write(analog_input_channels, num_analog_input_channels,
...                   encoder_input_channels, num_encoder_input_channels,
...                   digital_input_channels, num_digital_input_channels,
...                   None, 0,
...                   analog_output_channels, num_analog_output_channels,
...                   None, 0,
...                   digital_output_channels, num_digital_output_channels,
...                   None, 0,
...                   analog_input_buffer,
...                   encoder_input_buffer,
...                   digital_input_buffer,
...                   None,
...                   analog_output_buffer,
...                   None,
...                   digital_output_buffer,
...                   None)
...
>>>   # ...
...
>>> finally:
>>>   card.close()