Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

Best way to read a switch - Help!

This should be easy and probably is but I just cant seem to track the state of a switch effectivly.

 

What Im testing:

Code written in C++ using DAQmx on Scientific Linux.

Hardware is a 6221 PCI card.

I have a switch which is configured to be active high. The clean pulses from a press of the switch are from 50ms to 125ms (verified on scope).

 

What I want to accomplish:

I want to keep track of the actual state of port0 which has various inputs wired to it e.g. button inputs x4 for the user to press, alarm and state lines from safety system, hydraulic..etc ALL digital TTL logic.

 

I have a C++ class which runs a thread with a DAQmx task inside. When changes are detected on port0 in the thread, I want to update an array of varibles in the object for others to call in or be notified.

 

I can handle all the C++/multithreaded elements EXCEPT tracking the changes using DAQmx!

 

How Im currently doing it:

I am monitoring lines 0-7 for input change "dev1/port0/line0:7" so I can update an array of bytes inside a class which other applications can request.

 

I have been trying to use DAQmxCfgChangeDetectionTiming() which works, but:

 

a). Returns many sets of the same data - which is fine as Ive written a function to average these.

b). Never triggers on the rising edge despite being setup to.

 

Code

 

I have simplified the code below as I want to get to the nub of the issue - how to track changes and thus make decisions about if a button has been pressed or is held down. This would be easy if I got rising AND falling edge events!

 

 

 

 

pauBufIn // An array which is big enough for samples requested.
m_iLines  = 8; // lines 0-7
m_iSamplesPerLine = 2; // Number of samples per signal;


 

// Create a task to monitor the inputs.
      DAQMXVERIFY(DAQmxCreateTask(
         "DigitalInputMonitoring", 
         &m_hTask));

      // Specify the input channel to monitor.
      DAQMXVERIFY(DAQmxCreateDIChan(
         m_hTask, 
         "dev1/port0/line0:7",
         "",
         DAQmx_Val_ChanForAllLines));

      // Enable change detection on the configured channel.
      DAQMXVERIFY(DAQmxCfgChangeDetectionTiming(
         m_hTask, 
         "dev1/port0/line0:7",
         "dev1/port0/line0:7",
         DAQmx_Val_FiniteSamps, //DAQmx_Val_HWTimedSinglePoint, // DAQmx_Val_ContSamps
         m_iLines * m_iSamplesPerLine));

      // Start monitoring the inputs lines.
      DAQMXVERIFY(DAQmxStartTask(m_hTask));

      printf("CNiInputs::thread() Start Monitoring!\n");

      int32 iSampleSetsRead;
      int32 iBytesPerSample;

      // Loop in thread until we are told to stop.
      do
      {
         iSampleSetsRead = 0;
         iBytesPerSample = 0;

         // Start sampling the digital inputs.
         int32 iResult = DAQmxReadDigitalLines(
            m_hTask,
            m_iSamplesPerLine,
            DAQmx_Val_WaitInfinitely,
            DAQmx_Val_GroupByChannel,
            pauBufIn,
            m_iLines * m_iSamplesPerLine,
            &iSampleSetsRead,
            &iBytesPerSample, 
            NULL);

         if (NI_ABORT_DEVICE_REMOVED == iResult || 
             NI_ABORT_DEVICE_REMOVING == iResult )
         {
            printf("! ABORT !\n");
            break;
         }
         else if (NI_DIO_CHANGE_DECTECTED != iResult)
         {
            DAQMXVERIFY(iResult);

            printf("! UNKNOWN ERROR !\n");
         }
         else
         {
            // YAY! We have samples!
         }
      } 
      while(m_bRunMonitoring);

 

Many thanks for any tips on how to do this as its driving me crazy! Smiley Sad

 

In an embedded system I would do something like the following:

 

- Read the IO lines at a rate dictated by a timer.

- Average the result for some debounce protection.

- Compare the result to previous states held in an array.

-    If the button has been held down for X timer ticks but released, set "PRESSED" state.

-    If the button has been held down for Y timer ticks set "HELD_DOWN" state.

 

 

 

 

 

 

0 Kudos
Message 1 of 2
(5,349 Views)

Ill just poll them... **bleep** solution but there we go...Smiley Indifferent

0 Kudos
Message 2 of 2
(5,340 Views)