To show how to write to a single digital I/O bit using an MCC data acquisition device and various software platforms.
Data acquisition system developers who need to monitor or control digital states, and seek to understand its implementation in various software environments.
Digital signals, by definition, are those which can represent just two states, such as on or off. Examples include a warning lamp being lit or dark, a door being latched or unlatched, and a relay being closed or open. Numerically the two states are represented as 1 and 0, or Boolean (true and false). As in most hardware architectures, such individual bits are concatenated together to form bytes and words for more efficient data transfer. Such is the case for the hardware ICs used in modern DAQ devices. Usually, the input and output capability is provided in groups of 8 bits, and all 8 are read or written to in parallel fashion.
In some applications you will want to control or monitor a set of signals exactly at the same time, so be sure to group those signals onto the same port. But for some signals, while you may physically wire them to the same port, you may decide to read or write to them sequentially. In addition to port-wise operations, MCC provides bit-wise operations that automatically mask the remaining bits from the individual bit you want to focus on.
A common question is “how fast should I sample my digital inputs?”, and the answer invariably is: frequently enough so you do not miss a change-of-state. Note that digital inputs generally are not self-latching, nor do they automatically detect input changes. You must read (poll) them on a regular basis, and it is up to your software to decide if the current reading represents a change from the prior reading. For outputs, since they are latched, you need not write to the port again unless you require a change of state.
While real-world digital signals are predominantly unidirectional, DAQ devices usually offer bi-directional capability so as to be adaptable to a wider set of applications. Generally, a DAQ device’s digital ports will power up as inputs. You will need to set (i.e. configure) the direction of each port on the DAQ device for the desired direction. Some devices are bit-configurable for further flexibility. The configuration is to be done once for each digital port, at the start of the program.
For a software-paced digital output operation, independent of the software platform, two steps are required: Set the direction of the port to output, then later write a value to the port/bit. The former is performed just once, while the latter can be repeated as needed throughout the application.
The following information must be specified:
- Port Number
- Port Direction
- Data value to output
And the following information is returned:
The examples below show how to perform a non-continuous scan using the Universal Library™, UL for Android™, NI LabVIEW™, DASYLab®, and DAQami.
The following example shows how to first configure, and then control, a digital output using the Universal Library (UL) and Visual Basic:
PortType = MccDaq.DigitalPortType.AuxPort ‘Specify which digital port
Direction = MccDaq.DigitalPortDirection.DigitalOut ‘Specify the output direction
ULStat = DaqBoard.DConfigPort(PortType, Direction) ‘Configure the port
BitNum = 0 ‘Specify which bit in the port to control
BitValue = 1 ‘Specify the value to write
ULStat = DaqBoard.DBitOut(PortType, BitNum, BitValue) ‘Write the value out
UL for Android
The following example shows how to configure and control a digital output using UL for Android and Java:
DigitalPortType = AUXPORT; //Specify which digital port
DigitalDirection = OUTPUT; //Specify the output direction
mDioDevice.dConfigPort(DigitalPortType, DigitalDirection); //Configure the port
bitNum = 0; //Specify which bit in the port to control
bitState = true; Specify the Boolean value to write
mDioDevice.dBitOut(DigitalPortType, bitNum, bitState); //Write the value out
The following example shows how to control a digital output bit using LabVIEW. The wiring diagram below shows the pertinent VIs and settings. Here just the first bit (Do0) of the Auxport on hardware device #0 is configured for output, and then it is tasked with outputting the true/false value the operator enters
Non-programmers with digital signals to monitor and control can consider the popular DASYLab package. A Digital Output module is shown below, with its data input wired to the output of a Switch Control module. At run time, the operator can select either on or off, resulting in digital output bit D0 of the DAQ hardware toggling to either a 1 or 0.
Double clicking on the Switch module allows you to access a menu where you can specify how often the switch position will be monitored. Below we see that 1 Hz is selected, resulting in digital output bit D0 on the DAQ hardware being updated every second to match the operator’s selection.
Please contact Measurement Computing Corporation if you have any questions or if you would like any further information.
- Knowledgebase: kb.mccdaq.com
- Phone: 508-946-5100
- Fax: 508-946-9500
Additional TechTips are available on our Data Acquisition Technical Articles web page.