Measurement Computing   Easy to Use | Easy to Integrate | Easy to Support catalog banner

Measurement Computing Data Acquisition Knowledgebase

Welcome Guest ( )

Reading digital ports on PersonalDaq 55 & 56

Expand / Collapse

Reading digital ports on PersonalDaq 55 & 56

The standard C program listed below belongs to a Visual Studio console program and demonstrates reading one analog channel and one digital port. The purpose of this article to point out the fact that a PersonalDaq 55 or 56 digital IO has a power on state setting which can cause digital inputs to be unresponsive. It is suggested to first check the power on state to make assure compatibility. This can be done programmatically by checking the power up state and if it is output set it to input and reinitialize the unit as shown in the following code.

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include "daqx.h"

#define  ChanCount      2    
#define  ScanCount      20                       
#define  TotalCount     (ScanCount * ChanCount) 
#define  FREQ           4.0f                     
#define  STARTCHAN      1                       

#pragma comment(lib, "winmm.lib")

// Buffer to hold acquired data                                           
FLOAT buf[ChanCount * ScanCount];
DWORD PortVal;
DaqHandleT handle;
DWORD active,retCount,count,myVal;

// Error handler callback prototype                                       
void WINAPI myHandler(DaqHandleT handle, DaqError errCode);

//timer call back used to read data
void CALLBACK SineGenProc(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
//Read data up to the ScanCount or less if available. Data if analog is returned in engineering units
daqAdcTransferBufData(handle, buf , ScanCount, DabtmRetAvail, &retCount);
if(retCount) //print the one sample for each channel
printf("%+2.3f\t +%2.3f\n",buf[0],buf[1]);

DWORD chans[ChanCount];      //channel sequence array   
DWORD flags[ChanCount];       // flags sequence array
DaqAdcGain gains[ChanCount];       // gains sequence array 
FLOAT actualRate = 0;       // used by daqAdcSetRate 

printf("Simple Acquisition: Example\n");

handle = daqOpen("PDAQ");

if(handle < 0) 
      printf("\nError! Program aborted\nNo PersonalDaq devices installed\n");

//Check power upstate: if a zero is return for each bit of the port it means that bit is set to an output. You can  
//change it by writing a '1' for example 255 configures the port as all inputs (D1 - D7) . 
daqIORead(handle,DiodtPDaqDigIO, DiodpPDaqPowerUpPort1, 0, DioepP1, &myVal);
if(myVal == 0)
daqIOWrite(handle,DiodtPDaqDigIO, DiodpPDaqPowerUpPort1, 0, DioepP1, 255);
daqClose(handle); //reset the device by reopening
handle = daqOpen("PDAQ");

daqSetErrorHandler(handle, myHandler);
// Configure acquisition

// Selecting Range: PgainX1 is 4 volt range by itself; logically OR'd with PgainDiv5 multiplies the 4 volt range by 5 resulting in a
// 20 volt range

//set channel 1 to T-type thermocouple
    chans[0] = 1; 
    gains[0] = (DaqAdcGain)(PgainX1); //don't care for temperature
    flags[0] = DafTcTypeT | DafDifferential | DafMeasDuration110;

// Port 1 
    chans[1] = 1;
    gains[1] = PgainX1; //don't care
    flags[1]  = DafScanDigital; // read 8 bit input port pins 1-8

daqAdcSetScan(handle, chans, gains, flags, ChanCount);
daqAdcSetTrig(handle, DatsImmediate, 0,0,0,0);
daqAdcSetAcq(handle, DaamInfinitePost, 0, ScanCount);
daqAdcSetRate(handle, DarmFrequency, DaasPostTrig, FREQ, &actualRate);

daqAdcTransferSetBuffer(handle, (PVOID)buf, ScanCount, DatmDriverBuf );
printf("Arming...Please Wait.\n");
printf("Acquisition Armed...\n");
UINT uDelay = 1;
UINT uResolution = 1;
DWORD dwUser = NULL;
UINT fuEvent = TIME_PERIODIC; //You also choose TIME_ONESHOT;

SineTimer = timeSetEvent(uDelay, uResolution, SineGenProc, dwUser, fuEvent);

printf("Multi-Media timer failed...");

//wait for data 
do {
        daqAdcTransferGetStat(handle, &active, &retCount); 
} while (retCount > 10);

//read data while waiting for key press to end program
while( !_kbhit() )
printf("\nFinished.  Press ENTER...");


} /* end main */
//error handler...
void WINAPI myHandler(DaqHandleT handle, DaqError errCode) 
    char errMsg[64];
    daqFormatError(errCode, errMsg);
    printf("\nError! Program aborted\nPersonalDaq Error: 0x%x\n%s\n",
               errCode, errMsg);
    if(handle >= 0) daqClose(handle);
    printf("Press ENTER...");

Rate this Article:

Add Your Comments

For comments email

Article ID: 50420

Last Modified:6/13/2013 9:53:45 AM

Article has been viewed 3,596 times.