~vbursian/research-assistant/intervers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
////////////////////////////////////////////////////////////////////////////////
/*! @file Counters_NIDAQ.cpp Краткое описание модуля, возможно, с переносом на
                             другую(ие) строку(и), с точкой в конце.
- Part of NIDAQmxDrivers - a Research Assistant Plugin package.
- Uses  RAGUI - the Research Assistant Graphical User Interface.
- Uses  QtGui v.4.6  -  http://qt.nokia.com/
- Uses  RANet - Research Assistant Net Library (based on ANSI C++).
- Copyright(C) 2010, Dmitry K. Nelson, St.Petersburg, Russia.
                     D.Nelson@mail.ioffe.ru
*///////////////////////////////////////////////////////////////////////////////
#include "Counters_NIDAQ.h"
#include "NetOps.h"
#include <QByteArray>
#include <QDebug>
namespace RA {
//------------------------------------------------------------------------------

DEFINE_CLASS_TAG(sCounters_NIDAQ)

//------------------------------------------------------ sCounters_NIDAQ ---

sCounters_NIDAQ::~sCounters_NIDAQ()
{
  StopCounting();
};


sCounters_NIDAQ::sCounters_NIDAQ (sString   DevicetId
                                 ,sNodePtr  DeviceCfg)
    :sDevice(DevicetId,DeviceCfg)
    ,Counting(false)
    ,KinMeasuring(false)
    ,MeasureTime (1.0)
    ,PrevCount (0)
    ,CounterHandle(0)
    ,TimerHandle(0)
    ,readArray(NULL)
    ,DataArray(NULL)
    ,StartStopCounts(1000)
{


};

bool  sCounters_NIDAQ::StartContCounting ()
{
  bool Error;
  long  ErrorCode;
  ErrorCode = DAQmxCreateTask ("", &TimerHandle);
  Error = !(ErrorCode == 0);
  ErrorCode = DAQmxCreateCOPulseChanTime (TimerHandle, "Dev1/ctr0"
                                         ,"", DAQmx_Val_Seconds
                                         ,DAQmx_Val_Low
                                         ,MeasureTime
                                         ,0.5*MeasureTime
                                         ,0.5*MeasureTime);
// qDebug() << " Timer Create Chanel, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);
  ErrorCode = DAQmxCfgImplicitTiming (TimerHandle
                                     ,DAQmx_Val_ContSamps
                                     ,1000);
//  qDebug() << " Timer Configure Chanel, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);

  ErrorCode = DAQmxCreateTask ("", &CounterHandle);
  Error = Error || !(ErrorCode == 0);
  ErrorCode = DAQmxCreateCICountEdgesChan(CounterHandle,"Dev1/ctr1"
                                         ,"",DAQmx_Val_Rising
                                         ,0,DAQmx_Val_CountUp);
//  qDebug() << " Counter Create Chanel, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);
  ErrorCode = DAQmxCfgSampClkTiming (CounterHandle,"/Dev1/Ctr0InternalOutput", 1000
                                    ,DAQmx_Val_Rising
                                    ,DAQmx_Val_ContSamps , 1000);
  Error = Error || !(ErrorCode == 0);

  ErrorCode = DAQmxRegisterEveryNSamplesEvent (CounterHandle
                                        ,DAQmx_Val_Acquired_Into_Buffer
                                        ,1, 0, &RA::sCounters_NIDAQ::OnReadyToReadC, this);
  qDebug() << " Counter Callback Initiated, ErrorCode = " << ErrorCode;
  if (ErrorCode != 0) {
                        char EString[128];
                        DAQmxGetErrorString (ErrorCode, EString, 128);
                        qDebug() << EString;
                      };
  Error = Error || !(ErrorCode == 0);

  bool TimerStarted;
  bool CounterStarted;
  if (!Error)
      ErrorCode = DAQmxStartTask(TimerHandle);
  TimerStarted = (ErrorCode == 0);
  Error = Error || !(ErrorCode == 0);
  if (!Error)
      ErrorCode = DAQmxStartTask(CounterHandle);
  CounterStarted = (ErrorCode == 0);
  Error = Error || !(ErrorCode == 0);

  if (Error) {
      if (TimerStarted) ErrorCode = DAQmxStopTask(TimerHandle);
      if (CounterStarted) ErrorCode = DAQmxStopTask(CounterHandle);
      if (TimerHandle != 0) ErrorCode = DAQmxClearTask (TimerHandle);
      if (CounterHandle != 0) ErrorCode = DAQmxClearTask (CounterHandle);
      Counting = false;
  } else{
      Counting = true;
      qDebug() << " Counting Successfully Started !";
  }
  return !Error;
};

bool  sCounters_NIDAQ::StartKineticsCounting ()
{
  bool Error;
  long  ErrorCode;
  ErrorCode = DAQmxCreateTask ("", &TimerHandle);
  Error = !(ErrorCode == 0);
  ErrorCode = DAQmxCreateCOPulseChanTime (TimerHandle, "Dev1/ctr0"
                                         ,"", DAQmx_Val_Seconds
                                         ,DAQmx_Val_Low
                                         ,MeasureTime
                                         ,0.5*MeasureTime
                                         ,0.5*MeasureTime);
  qDebug() << " Kinetics Timer Create Chanel, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);
  ErrorCode = DAQmxCfgImplicitTiming (TimerHandle
                                     ,DAQmx_Val_ContSamps
                                     ,ArraySize);
  qDebug() << " Kinetics Timer Configure Chanel, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);

  ErrorCode = DAQmxCreateTask ("", &CounterHandle);
  Error = Error || !(ErrorCode == 0);
  ErrorCode = DAQmxCreateCICountEdgesChan(CounterHandle,"Dev1/ctr1"
                                         ,"",DAQmx_Val_Rising
                                         ,0,DAQmx_Val_CountUp);
  qDebug() << " Kinetics Counter Create Chanel, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);
  ErrorCode = DAQmxCfgSampClkTiming (CounterHandle,"/Dev1/Ctr0InternalOutput", 1/MeasureTime
                                    ,DAQmx_Val_Rising
                                    ,DAQmx_Val_FiniteSamps , ArraySize);
  qDebug() << " Kinetics Counter Configure Chanel, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);

  ErrorCode = DAQmxRegisterDoneEvent (CounterHandle, 0
                                         , &RA::sCounters_NIDAQ::OnReadyToReadK, this);
  qDebug() << " Kinetics Callback Initiated, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);

  ErrorCode = DAQmxSetArmStartTrigType (TimerHandle, DAQmx_Val_DigEdge);
  qDebug() << " Kinetics Timer SetArmStartTrigType, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);
  ErrorCode = DAQmxSetDigEdgeArmStartTrigSrc (TimerHandle, "/Dev1/PFI9");
  qDebug() << " Kinetics Timer SetArmStartTrigSrc, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);
  ErrorCode = DAQmxSetDigEdgeArmStartTrigEdge (TimerHandle, DAQmx_Val_Rising );
  qDebug() << " Kinetics Timer SetArmStartTrigEdge, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);

  ErrorCode = DAQmxSetArmStartTrigType (CounterHandle, DAQmx_Val_DigEdge);
  qDebug() << " Kinetics Counter SetArmStartTrigType, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);
  ErrorCode = DAQmxSetDigEdgeArmStartTrigSrc (CounterHandle, "/Dev1/PFI9");
  qDebug() << " Kinetics Counter SetArmStartTrigSrc, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);
  ErrorCode = DAQmxSetDigEdgeArmStartTrigEdge (CounterHandle, DAQmx_Val_Rising );
  qDebug() << " Kinetics Counter SetArmStartTrigEdge, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);

  bool TimerStarted = false;
  bool CounterStarted =false;
  if (!Error)
      ErrorCode = DAQmxStartTask(TimerHandle);
  qDebug() << " Kinetics Timer Start Task, ErrorCode = " << ErrorCode;
  if (ErrorCode != 0) {
                        char EString[128];
                        DAQmxGetErrorString (ErrorCode, EString, 128);
                        qDebug() << EString;
                      };
  TimerStarted = (ErrorCode == 0);
  Error = Error || !(ErrorCode == 0);
  if (!Error)
      ErrorCode = DAQmxStartTask(CounterHandle);
  qDebug() << " Kinetics Counter Start Task, ErrorCode = " << ErrorCode;
  if (ErrorCode != 0) {
                        char EString[128];
                        DAQmxGetErrorString (ErrorCode, EString, 128);
                        qDebug() << EString;
                      };
  CounterStarted = (ErrorCode == 0);
  Error = Error || !(ErrorCode == 0);

  if (Error) {
      if (TimerStarted) ErrorCode = DAQmxStopTask(TimerHandle);
      if (CounterStarted) ErrorCode = DAQmxStopTask(CounterHandle);
      if (TimerHandle != 0) ErrorCode = DAQmxClearTask (TimerHandle);
      if (CounterHandle != 0) ErrorCode = DAQmxClearTask (CounterHandle);
      KinMeasuring = false;
  } else{
      KinMeasuring = true;
      qDebug() << " Kinetics Successfully Started !";
  }
  return !Error;
};

bool  sCounters_NIDAQ::Start_StartStopCounting (int Mode)
{
  bool Error = false;
  long  ErrorCode = 0;
/*
  ErrorCode = DAQmxCreateTask ("", &TimerHandle);
  Error = !(ErrorCode == 0);
  ErrorCode = DAQmxCreateCOPulseChanTime(TimerHandle, "Dev1/ctr0"
                                         ,"StartStopTimer", DAQmx_Val_Seconds
                                         ,DAQmx_Val_Low,MeasureTime*ArraySize*0.8,MeasureTime*ArraySize*0.8,0.0000005);
  qDebug() << " StartStop Timer Create Chanel, ErrorCode = " << ErrorCode;
  qDebug() << " StartStop Timer Delay = " << (float) MeasureTime*ArraySize*0.8*1000000;
  Error = Error || !(ErrorCode == 0);
  ErrorCode = DAQmxCfgImplicitTiming (TimerHandle,DAQmx_Val_FiniteSamps,1);
  qDebug() << " StartStop Timer Configure Chanel, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);
*/
  ErrorCode = DAQmxCreateTask ("", &CounterHandle);
  Error = Error || !(ErrorCode == 0);
  ErrorCode = DAQmxCreateCITwoEdgeSepChan (CounterHandle,"Dev1/ctr1"
                                     ,"StartStopCounter",0,1000000,DAQmx_Val_Ticks
                                     ,DAQmx_Val_Rising,DAQmx_Val_Rising,NULL);
  qDebug() << " StartStop Counter Create Chanel, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);
  ErrorCode = DAQmxCfgImplicitTiming  (CounterHandle, DAQmx_Val_FiniteSamps, StartStopCounts);
  qDebug() << " StartStop Counter Configure Chanel, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);

  char InputName[128];

  if (Mode == 1){
    ErrorCode = DAQmxSetCICtrTimebaseSrc(CounterHandle, "StartStopCounter"
                                        ,"/Dev1/20MHzTimebase");
    ErrorCode = DAQmxGetCICtrTimebaseSrc(CounterHandle, "StartStopCounter", InputName, 128);
  } else {
    ErrorCode = DAQmxSetCICtrTimebaseSrc(CounterHandle, "StartStopCounter"
                                        ,"/Dev1/80MHzTimebase");
    ErrorCode = DAQmxGetCICtrTimebaseSrc(CounterHandle, "StartStopCounter", InputName, 128);
  }
  qDebug() << " StartStop Counter Configure Source, ErrorCode = " << ErrorCode;
  qDebug() << "Mode - " << Mode << " Input - " << InputName;
  Error = Error || !(ErrorCode == 0);

  ErrorCode = DAQmxRegisterDoneEvent (CounterHandle, 0
                                         , &RA::sCounters_NIDAQ::OnReadyToReadSS, this);
  qDebug() << " StartStop Callback Initiated, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);
/*
  ErrorCode = DAQmxCfgDigEdgeStartTrig (TimerHandle, "/Dev1/PFI9", DAQmx_Val_Rising);
  Error = Error || !(ErrorCode == 0);
  ErrorCode = DAQmxSetStartTrigRetriggerable (TimerHandle, TRUE);
  Error = Error || !(ErrorCode == 0);
  if (Error) qDebug() << " StartStop Timer Configure Start Trigger ERROR, ErrorCode = " << ErrorCode;
*/
//  ErrorCode = DAQmxSetCITwoEdgeSepFirstTerm (CounterHandle,"StartStopCounter", "/Dev1/PFI9");
  ErrorCode = DAQmxSetCITwoEdgeSepFirstTerm (CounterHandle,"StartStopCounter", "/Dev1/PFI3");
  qDebug() << " StartStop Counter Set Start Source, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);
//  ErrorCode = DAQmxSetCITwoEdgeSepSecondTerm (CounterHandle,"StartStopCounter", "/Dev1/PFI3");
  ErrorCode = DAQmxSetCITwoEdgeSepSecondTerm (CounterHandle,"StartStopCounter", "/Dev1/PFI9");
//  ErrorCode = DAQmxSetCITwoEdgeSepSecondTerm (CounterHandle,"StartStopCounter", "/Dev1/Ctr0InternalOutput");
  qDebug() << " StartStop Counter Set Stop Source, ErrorCode = " << ErrorCode;
  Error = Error || !(ErrorCode == 0);

  bool TimerStarted = false;
  bool CounterStarted =false;

  if (!Error)
      ErrorCode = DAQmxStartTask(CounterHandle);
  qDebug() << " StartStop Counter Start Task, ErrorCode = " << ErrorCode;
  if (ErrorCode != 0) {
                        char EString[128];
                        DAQmxGetErrorString (ErrorCode, EString, 128);
                        qDebug() << EString;
                      };
  CounterStarted = (ErrorCode == 0);
  Error = Error || !(ErrorCode == 0);
/*
  if (!Error)
      ErrorCode = DAQmxStartTask(TimerHandle);
  qDebug() << " StartStop Timer Start Task, ErrorCode = " << ErrorCode;
  if (ErrorCode != 0) {
                        char EString[128];
                        DAQmxGetErrorString (ErrorCode, EString, 128);
                        qDebug() << EString;
                      };
  TimerStarted = (ErrorCode == 0);
  Error = Error || !(ErrorCode == 0);
*/
  if (Error) {
      if (TimerStarted) ErrorCode = DAQmxStopTask(TimerHandle);
      if (CounterStarted) ErrorCode = DAQmxStopTask(CounterHandle);
      if (TimerHandle != 0) ErrorCode = DAQmxClearTask (TimerHandle);
      if (CounterHandle != 0) ErrorCode = DAQmxClearTask (CounterHandle);
      KinMeasuring = false;
  } else{
      KinMeasuring = true;
      qDebug() << " StartStop Successfully Started !";
  }
  return !Error;
}

void  sCounters_NIDAQ::StopCounting ()
{
  long  ErrorCode;
  if (TimerHandle != 0){
      ErrorCode = DAQmxStopTask(TimerHandle);
      ErrorCode = DAQmxClearTask (TimerHandle);
  }
  if (CounterHandle != 0){
      ErrorCode = DAQmxStopTask(CounterHandle);
      ErrorCode = DAQmxClearTask (CounterHandle);
  }
  if (readArray){
    delete [] readArray;
    readArray = NULL;
  }
  if (DataArray){
    delete [] DataArray;
    DataArray = NULL;
  }
  Counting = false;
  KinMeasuring = false;
  qDebug() << " Counting Stoped !";
};

void  sCounters_NIDAQ::Connect ()
{
  if( DeviceState() == Offline ){
    SetDeviceState(Warming);
    MeasureTime = 0.5;
    PrevCount = 0;
    if (StartContCounting()){
      SetDeviceState(Standby);
      emit NowStandby ();
    } else{
      StopCounting ();
      SetDeviceState(Offline);
      emit NowOffline();
    }
  }
}

void  sCounters_NIDAQ::Disconnect ()
{
  if( DeviceState() != Offline ){
    if (Counting){
      StopCounting();
    }
    SetDeviceState(Offline);
    emit NowOffline();
  };
};

void sCounters_NIDAQ::StartCount (real Time)
{
  if (DeviceState() == Standby){
    if (Counting || KinMeasuring){
      StopCounting();
    }
    MeasureTime = Time;
    PrevCount = 0;
    qDebug() << "! Counting for Scanning is starting !";
    if (StartContCounting()){
      SetDeviceState(Busy);
    } else{
      StopCounting ();
      SetDeviceState(Offline);
      emit NowOffline();
    }
  }
}

void sCounters_NIDAQ::StartKinetics (real Time, int Size)
{
  if (DeviceState() == Standby){
    if (Counting || KinMeasuring){
      StopCounting();
    }
    MeasureTime = Time*0.000001;
    PrevCount = 0;
    ArraySize = Size;
    readArray = new unsigned long [ArraySize];
    DataArray = new real [ArraySize];
    qDebug() << "! Kinetics measuring is starting !";
    if (StartKineticsCounting()){
      SetDeviceState(Busy);
    qDebug() << "! Kinetics is started !";
    } else{
      StopCounting ();
      SetDeviceState(Offline);
      emit NowOffline();
    }
  }
}

void sCounters_NIDAQ::Start_StartStop (int Mode, int Size, int NumCounts)
{
  if (DeviceState() == Standby){
    if (Counting || KinMeasuring){
      StopCounting();
    }
    StartStopCounts = NumCounts;
    MeasureTime = 0.0125*0.000001;
    if (Mode == 1) MeasureTime = 0.05*0.000001;
    PrevCount = 0;
    ArraySize = Size;
    readArray = new unsigned long [StartStopCounts];
    DataArray = new real [ArraySize];
    qDebug() << "! StartStop measuring is starting !";
    if (Start_StartStopCounting (Mode)){
      SetDeviceState(Busy);
    qDebug() << "! StartStop is started !";
    } else{
      StopCounting ();
      SetDeviceState(Offline);
      emit NowOffline();
    }
  }
}

void sCounters_NIDAQ::StopCounters ()
{
  if (Counting || KinMeasuring){
    StopCounting();
  }
}

void sCounters_NIDAQ::RestoreStandby ()
{
  if (Counting || KinMeasuring){
    StopCounting();
  }
  MeasureTime = 0.5;
  PrevCount = 0;
  if (StartContCounting()){
    SetDeviceState(Standby);
    emit NowStandby ();
  } else{
    StopCounting ();
    SetDeviceState(Offline);
    emit NowOffline();
  }
}

void sCounters_NIDAQ::ReadCounterSampling ()
{
  if (Counting){
//    qDebug() << " Read Counter Sampling";
    unsigned long CurrentCount = 0;
    long ErrorCode = DAQmxReadCounterScalarU32 (CounterHandle, 1.0, &CurrentCount, NULL);
    if (ErrorCode == 0){
        unsigned long C = CurrentCount - PrevCount;
        PrevCount = CurrentCount;
        real Intensity = C;
        Intensity = Intensity/MeasureTime;
        if (DeviceState() == Standby) {emit ShowData(Intensity);};
        emit NewData (Intensity);
    } else{
        StopCounting ();
        SetDeviceState(Offline);
        qDebug() << " Counter Read Error, ErrorCode = " << ErrorCode;
        char EString[128];
        DAQmxGetErrorString (ErrorCode, EString, 128);
        qDebug() << EString;
        emit NowOffline();
    }
  }
}

void sCounters_NIDAQ::ReadKineticsSampling ()
{
  if (KinMeasuring){
//    qDebug() << " Begin to read Kinetics Sampling";
    bool Error = true;
    long PNumRead = 0;
    long ErrorCode = DAQmxReadCounterU32 (CounterHandle, ArraySize, 1.0, readArray, ArraySize, &PNumRead, NULL);
//    DAQmxStopTask(TimerHandle);
    DAQmxStopTask(CounterHandle);
    if ((ErrorCode == 0) && (ArraySize == PNumRead)){
        for (int I = 0; I < ArraySize; I++){
          long C = readArray[I] - PrevCount;
          PrevCount = readArray[I];
          real Intensity = C;
          Intensity = Intensity/MeasureTime;
          DataArray[I] = Intensity;
        }
        emit NewDataArray (DataArray);
        PrevCount = 0;
        ErrorCode = DAQmxStartTask(CounterHandle);
        Error = !(ErrorCode == 0);
//        if (!Error)
//          ErrorCode = DAQmxStartTask(CounterHandle);
//        Error = Error || !(ErrorCode == 0);
    }
    if (Error) {
        StopCounting ();
        SetDeviceState(Offline);
        qDebug() << " Counter Read Error, ErrorCode = " << ErrorCode;
        qDebug() << " Want to read = " << ArraySize << "Actually read" << PNumRead;
        char EString[128];
        DAQmxGetErrorString (ErrorCode, EString, 128);
        qDebug() << EString;
        emit NowOffline();
        return;
    }
  }
}

void sCounters_NIDAQ::ReadStartStopSampling ()
{
    if (KinMeasuring){
      long PNumRead = 0;
      long ErrorCode = DAQmxReadCounterU32 (CounterHandle, -1, 0, readArray, 2*StartStopCounts, &PNumRead, NULL);
      bool ReadError = !(ErrorCode == 0);
      if (ReadError) qDebug() << " Error reading Start-Stop Arrray !!! ";
      DAQmxStopTask(CounterHandle);
//      DAQmxStopTask(TimerHandle);
      if (!ReadError) {
          for (int I = 0; I < ArraySize; I++){
              DataArray[I] = 0;
          }
          for (int I = 0; I < PNumRead; I++){
//               int N = readArray[I];
               int N = ArraySize - readArray[I];
               if ((N < ArraySize) && (N >= 0))
                 DataArray[N] = DataArray[N] + 1.0/MeasureTime/StartStopCounts;
          }
          emit NewDataArray (DataArray);
      }
      ErrorCode = DAQmxStartTask(CounterHandle);
      bool Error = !(ErrorCode == 0);
/*      if (!Error)
          ErrorCode = DAQmxStartTask(TimerHandle);
      Error = Error || !(ErrorCode == 0);*/
      if (Error) {
              char EString[128];
              DAQmxGetErrorString (ErrorCode, EString, 128);
              qDebug() << EString;
          StopCounting ();
          SetDeviceState(Offline);
          emit NowOffline();
          return;
      }
    }
}

int32  CVICALLBACK sCounters_NIDAQ::OnReadyToReadC (TaskHandle /*theHandle*/
                                                   ,int32      /*eventType*/
                                                   ,uInt32     /*nSamples*/
                                                   ,void *     DataPtr)
{
  sCounters_NIDAQ* obj = static_cast<sCounters_NIDAQ*>(DataPtr);
  QMetaObject::invokeMethod(obj, "ReadCounterSampling",Qt::QueuedConnection);
  return 0;
}

int32  CVICALLBACK sCounters_NIDAQ::OnReadyToReadK (TaskHandle /*theHandle*/
                                                   ,int32      /*OK*/
                                                   ,void *     DataPtr)
{
  sCounters_NIDAQ* obj = static_cast<sCounters_NIDAQ*>(DataPtr);
  QMetaObject::invokeMethod(obj, "ReadKineticsSampling", Qt::QueuedConnection);

  return 0;
}

int32  CVICALLBACK sCounters_NIDAQ::OnReadyToReadSS (TaskHandle /*theHandle*/
                                                    ,int32      /*OK*/
                                                    ,void *     DataPtr)
{
  sCounters_NIDAQ* obj = static_cast<sCounters_NIDAQ*>(DataPtr);
  QMetaObject::invokeMethod(obj, "ReadStartStopSampling", Qt::QueuedConnection);

  return 0;
}

//------------------------------------------------------------------------------
}; //namespace RA