From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Example Code

How to use DAQmx EVENT in multithread VC++ programming?

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • Other

    Driver

  • NI DAQmx

Code and Documents

Attachment

Overview
The example demonstrates how to how to use DAQmx event in VC++ multithread programming

Description:
This VI would shows you how to utilize DAQmx Event in VC++ multithread programming and could be used in any DAQ digital IO cards which supports DAQmx. In this application, a DC motor is going to be controlled by the VI using the PCI6255.Configure the PCI6255 with port 0 line 0 and line 1. Set the lines to 1 and 0 for moving forward; 0 and 0 for stop; 0 and 1 for backward.There are 2 proximity switches, one for home switch (port0 line 3), and one for position limit (port0 line2). They are normally high, if switches are on they will go to low (The motor should be able to stop automatically when it reaches any proximity switch).The motor should be able to stop by operator at any time.

It is solved by Create a small dialog box with 3 buttons to control movement of motor. When it moves forward, create a thread (DetecLimit) to detect if it reaches the position limit, the thread registers a signal event, when the event is fired it call callback function ( HomeDetectionCallback )to stop the motor.When it moves backward, create another thread (DetecHome) to detect if it reaches the home position, the thread registers a signal event, when the event is fired it call callback function (LimitDetectionCallback) to stop the motor.

Advantage of multithread:     If put all thefunctions in one thread, it still works fine for automatically stop. Theproblem is if operator try to stop motor before it reaches any proximity, theprogram may not have change to response to the command because detecting signalevent can block all commands from user interface (You should try that).

Steps to implement or execute code
To implement this example:
  1. This is a very simple MFC dialog box program called DAQmxEvent, which has only 3 buttons to move forward, stop and move backward  (Main thread , User interface)
  2. All the sub thread functions and help function are in “DAQmxFunctions .cpp ” and “DAQmxFunctions .h ”.</li><li>NIDAQmx.h and NIDAQmx.lib are added to the project for compile.
  3. You don’t have to added these 2 files for compile, but you have to setup include and libraries directory for VC+ \
  4. Comments are added in front of each functions to help reader understand what does each function do


Requirements
Software
Visual C++

Hardware
Any DAQ cards which have digital I/O and support DAQmx.

 

 **This document has been updated to meet the current required format for the NI Code Exchange.**

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.