Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Continuously detect digital input change

Hi, I am trying to use Config_DAQ_Event_Message and callback to get notified when a digital line changes. The line has to be monitored for a week continuously. How can I make the DIG_block_in to run continuously? I tried using 2 callbacks, one Event 8 and one Event 1 (tried Event 1 too) but it crashed after about 1 seconds. Thanks for any help.

#define NUM_DI_SCAN 2
int ind2 = 0;
int ind = 0;

// Callback for state change of digital line
void CB2(HWND handle, UINT msg, WPARAM wParam, LPARAM lParam)
{
ind2++;
}

// Callback for completion of DIG_block_in
void CB1(HWND handle, UINT msg, WPARAM wParam, LPARAM lParam)
{
i16 grp0data[NUM_DI_SCAN];
int status;

ind++;

status = DIG_Block_Clear(DEVICE_NUM,
1);
status = DIG_Block_In(DEVICE_NUM, 1, grp0data, NUM_DI_SCAN);
}

void SetupDigCallback(HWND hwnd)
{
i16 grp0data[NUM_DI_SCAN];
int status;
i16 portList[] = {0};

// Set DAQ to interrupt driven
Set_DAQ_Device_Info(DEVICE_NUM, ND_DATA_XFER_MODE_DIO_GR1, ND_INTERRUPTS);

// Setup group
status = DIG_SCAN_Setup(DEVICE_NUM, 1, 1, portList, 0);

// Setup events
status = Config_DAQ_Event_Message(DEVICE_NUM, 1, "DI0", 1, NUM_DI_SCAN,
0, 0, 0, 0, hwnd, 0, (ULONG)&CB1);
status = Config_DAQ_Event_Message(DEVICE_NUM, 1, "DI0", 8, 0x8,
0x8, 0, 0, 0, hwnd, 0, (ULONG)&CB2);

// Start asynchronous operation
status = DIG_Block_In(DEVICE_NUM, 1, grp0data, NUM_DI_SCAN);
}
0 Kudos
Message 1 of 4
(3,308 Views)
Dear Rion,

It�s possible that the short answer to your question is to use the NIDAQYield function to allow the main process to yield to the callback functions. Your problem may require something more than that, however.

Without knowing all of the details of your application, I can suggest that the Continuous Change Detection Example looks like it closely parallels what you are trying to do. I have that example functioning on my system using a PCI-6534.

Make sure that your hardware settings (device number, etc.) a
re correct for your setup. If these suggestions do not help with your problem, please reply with information on what hardware and software you are using, as well as all of the code for this application, including any constants or definitions that you are referencing.

Have a good day,

Kyle Bryson
National Instruments
0 Kudos
Message 2 of 4
(3,308 Views)
Hello Kyle,

Thank you for your answer. I looked through the Continuous Change Detection Example and it seems that it does what I want to do, but my device does not support the DIG_Block_PG_Config function. My device is a Lab-PC 1200 DAQ, programming using Visual C++ 6 on Windows 95, and Ni-daq version 5.1. Hardware settings are all correct and working.

I have attached the project code as a zip file and the main code here.

Thank you very much,
Rion

/*==============CODE START================
This program runs and monitors the digital line (PortA, bit 4 (bit 1 = LSB)).
When the digital line is logical high, the program increments the variable count2
by 1 and displays the value of count2 in an edit box.
count1 is used to count the number of times the DIG_Block_In function has been
called and completed.

This program is used to test the event messaging function of Ni-DAQ. This program
must not use any "while" loop and relies on the callback functions to work. The
idea of the callback event messaging of this program will be used in a software
that controls a machine and monitors a status signal from the machine.
*/

#include
#include "nidaqcns.h"
#include "nidaq.h"
#include "nidaqex.h"
#include "resource.h"

HINSTANCE hInst;

// Device number for the LabPC DAQ Card
#define DEVICE_NUM 1
// Number of scans for digital block in
#define NUM_DI_SCAN 20

int count1 = 0;
int count2 = 0;

// Callback function for updating the count2 variable
void Update2CB(HWND handle, UINT msg, WPARAM wParam, LPARAM lParam)
{
count2++;
// Display the value of count2 in an edit box
SetDlgItemInt(handle, IDC_ALG_1, count2, TRUE);
}

// Check digital input callback function
void CheckDigCB(HWND handle, UINT msg, WPARAM wParam, LPARAM lParam)
{
i16 grp0data[NUM_DI_SCAN];
int status;

count1++;
// Display count1 in an edit box
SetDlgItemInt(handle, IDC_ALG_0, count1, TRUE);

// Clear the block transfer
status = DIG_Block_Clear(DEVICE_NUM, 1);

// Start asynchronous block transfer operation
status = DIG_Block_In(DEVICE_NUM, 1, grp0data, NUM_DI_SCAN);
}

// Setup the event messages
void SetupDigCallback(HWND hwnd)
{
i16 grp0data[NUM_DI_SCAN];
int status;
i16 devNum;
i16 portList[] = {0};

Init_DA_Brds(DEVICE_NUM, &devNum);

Config_DAQ_Event_Message(DEVICE_NUM, 0, "", 0, 0, 0, 0, 0, 0, 0, 0, NULL);

// Set DAQ to interrupt driven
Set_DAQ_Device_Info(DEVICE_NUM, ND_DATA_XFER_MODE_DIO_GR1, ND_INTERRUPTS);

// Setup digital group
status = DIG_SCAN_Setup(DEVICE_NUM, 1, 1, portList, 0);

status = Config_DAQ_Event_Message(DEVICE_NUM, 1, "DI0", 1, NUM_DI_SCAN, 0, 0, 0, 0, hwnd, 0, (ULONG)&CheckDigCB);

// Setup event to detect change of digital port 0, bit 4
status = Config_DAQ_Event_Message(DEVICE_NUM, 1, "DI0", 8, 0, 0, 0, 0, 0, hwnd, 0, (ULONG)&Update2CB);

// Start asynchronous operation
status = DIG_Block_In(DEVICE_NUM, 1, grp0data, NUM_DI_SCAN);
}

// Clear the event message
void ReleaseDigCallback(HWND hwnd)
{
// Clear block
DIG_Block_Clear(DEVICE_NUM, 1);

// Release group
DIG_SCAN_Setup(DEVICE_NUM, 0, 0, 0, 0);

// Clear all messages
Config_DAQ_Event_Message(DEVICE_NUM, 0, "", 0, 0, 0, 0, 0, 0, 0, 0, NULL);
}


// Main message handler for the main application dialog box
BOOL CALLBACK MainDlgProc(HWND hdlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
// Monitor the digital line until the user pressed the close button
SetupDigCallback(hdlg);
return TRUE;

case WM_CLOSE:
ReleaseDigCallback(hdlg);
EndDialog(hdlg, 0);
return TRUE;
}
return FALSE;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
hInst = hInstance;
DialogBox(hInst, MAKEINTRESOURCE(IDD_MAIN), NULL, (DLGPROC)MainDlgProc);
return 0;
}
0 Kudos
Message 3 of 4
(3,308 Views)
Dear Rion,

Change detection is a hardware feature that is not supported on the Lab-PC 1200. I believe that you would like the hardware to monitor for a change without software continuously polling; unfortunately, the Lab-PC 1200 contains no mechanism for detecting this and interrupting the software.

I would recommend use of a PCI-6533 or a PCI-6534, both of which support hardware change detection and will function with the example code that I linked previously. Otherwise, the only solution with your existing hardware requires software to poll the state of the line in question to see if it has changed. Please le
t me know if you have any further questions or if I may be of additional assistance.

Thank you,
Kyle Bryson
National Instruments
0 Kudos
Message 4 of 4
(3,308 Views)