Write and Read Digital I/O Line Values

Write Digital Values

    Note   Unlike analog input and analog output objects, you do not control the behavior of DIO objects by configuring properties. This is because buffered DIO is not supported, and data is not stored in the engine. Instead, you either write values directly to, or read values directly from the hardware lines.

You write values to digital lines with the putvalue function. putvalue requires the DIO object and the values to be written as input arguments. You can specify the values to be written as a decimal value or as a binary vector (binvec). A binary vector is a logical array that is constructed with the least significant bit (LSB) in the first column and the most significant bit (MSB) in the last column. For example, the decimal value 23 is written in binvec notation as [1 1 1 0 1] = 20 + 21 + 22 + 24. You might find that binvecs are easier to work with than decimal values because there is a clear association between a given line and the value (1 or 0) that is written to it. You can convert decimal values to binvec values with the dec2binvec function.

For example, suppose you create the digital I/O object dio and add eight output lines to it from port 0.

dio = digitalio('nidaq','Dev1');
addline(dio,0:7,'out');

To write a value of 23 to the eight lines contained by dio, you can write to the device object.

data = 23;
putvalue(dio,data)

Alternatively, you can write to individual lines through the Line property.

putvalue(dio.Line(1:8),data)

To write a binary vector of values using the device object and the Line property:

bvdata = dec2binvec(data,8);
putvalue(dio,bvdata)
putvalue(dio.Line(1:8),bvdata)

The second input argument supplied to dec2binvec specifies the number of bits used to represent the decimal value. Because the preceding commands write to all eight lines contained by dio, an eight element binary vector is required. If you do not specify the number of bits, then the minimum number of bits needed to represent the decimal value is used.

Alternatively, you can create the binary vector without using dec2binvec.

bvdata = logical([1 1 1 0 1 0 0 0]);
putvalue(dio,bvdata)

Rules for Writing Digital Values

Writing values to digital I/O lines follows these rules:

  • If the DIO object contains lines from a port-configurable device, then the data acquisition engine writes to all lines associated with the port even if they are not contained by the device object.

  • When writing decimal values,

    • If the value is too large to be represented by the lines contained by the device object, then an error is returned.

    • You can write to a maximum of 32 lines. To write to more than 32 lines, you must use a binvec value.

  • When writing binvec values,

    • You can write to any number of lines.

    • There must be an element in the binary vector for each line you write to.

  • You can always read from a line configured for output. Reading values is discussed in Read Digital Values.

  • An error is returned if you write a negative value, or if you write to a line configured for input.

Read Digital Values

    Note   Unlike analog input and analog output objects, you do not control the behavior of DIO objects by configuring properties. This is because buffered DIO is not supported, and data is not stored in the engine. Instead, you either write values directly to, or read values directly from the hardware lines.

You can read values from one or more lines with the getvalue function. getvalue requires the DIO object as an input argument. You can optionally specify an output argument, which represents the returned values as a binary vector. Binary vectors are described in Write Digital Values.

For example, suppose you create the digital I/O object dio and add eight input lines to it from port 0.

dio = digitalio('nidaq','Dev1');
addline(dio,0:7,'in');

To read the current value of all the lines contained by dio:

portval = getvalue(dio)
portval =
     1     1     1     0     1     0     0     0

To read the current values of the first five lines contained by dio:

lineval = getvalue(dio.Line(1:5))
lineval =
     1     1     1     0     1

You can convert a binvec to a decimal value with the binvec2dec function. For example, to convert the binary vector lineval to a decimal value:

out = binvec2dec(lineval)
out =
    23

Rules for Reading Digital Values

Reading values from digital I/O lines follows these rules:

  • If the DIO object contains lines from a port-configurable device, then all lines are read even if they are not contained by the device object. However, only values from the lines contained by the object are returned.

  • You can always read from a line configured for output.

  • For National Instruments® hardware using the Traditional NI-DAQ interface, lines configured for output return a value of 1 by default.

      Note:   The Traditional NI-DAQ adaptor will be deprecated in a future version of the toolbox. If you create a Data Acquisition Toolbox™ object for Traditional NI-DAQ adaptor beginning in R2008b, you will receive a warning stating that this adaptor will be removed in a future release. See the supported hardware page at www.mathworks.com/products/daq/supportedio.html for more information.

  • getvalue always returns a binary vector (binvec). To convert the binvec to a decimal value, use the binvec2dec function.

Write and Read Digital Values

This example illustrates how to read and write digital values using a line-configurable subsystem. With line-configurable subsystems, you can transfer values on a line-by-line basis.

You can run this example by typing daqdoc7_1 at the MATLAB Command Window.

  1. Create a device object — Create the digital I/O object dio for a National Instruments board. The installed adaptors and hardware IDs are found with daqhwinfo.

    dio = digitalio('nidaq','Dev1');
  2. Add lines — Add eight output lines from port 0 (line-configurable).

    addline(dio,0:7,'out');
  3. Read and write values — Write a value of 13 to the first four lines as a decimal number and as a binary vector, and read back the values.

    data = 13;
    putvalue(dio.Line(1:4),data)
    val1 = getvalue(dio);
    bvdata = dec2binvec(data);
    putvalue(dio.Line(1:4),bvdata)
    val2 = getvalue(dio);

    Write a value of 3 to the last four lines as a decimal number and as a binary vector, and read back the values.

    data = 3;
    putvalue(dio.Line(5:8),data)
    val3 = getvalue(dio.Line(5:8));
    bvdata = dec2binvec(data,4);
    putvalue(dio.Line(5:8),bvdata)
    val4 = getvalue(dio.Line(5:8));

    Read values from the last four lines but switch the most significant bit (MSB) and the least significant bit (LSB).

    val5 = getvalue(dio.Line(8:-1:5));
  4. Clean up — When you no longer need dio, you should remove it from memory and from the MATLAB workspace.

    delete(dio)
    clear dio
Was this topic helpful?