NI 6024 counter

Alright, I've got the following code (thanks Caleb) and I think I now have
the counter working.  However, when I go to read from the counter when
nothing is attached, I keep getting counts.  If I have nothing connected,
I should get 0 on the counts, right?

My code is as follows:

#include<stdio.h>
#include<comedilib.h>
#include<unistd.h>

int main(int argc, char *argv[])
{
    comedi_t *card;
    int nError = 0;
    int i;
    int j;
    lsampl_t op[2];
    lsampl_t data;
    comedi_insn insn;
    int subdevice = 4;      /* subdevice 4 is counter */

    /* open the device */
    card = comedi_open("/dev/comedi0");
    /* lock the subdevice */
    nError = comedi_lock(card, subdevice);
    if(-1 == nError)
    {
        /* there was an error locking the device, we can continue but
might be unexpected results */
        printf("Error locking subdevice: %s\n",
comedi_strerror(comedi_errno()));
    }

    for(j = 0; j < 31; j++)
    {
    /* counter code */
    /* 1. Selected Counter Application
     * 2. Configure buffer (if needed)
     * 3. Change counter attrributes
     * 4. Program counter (arm)
     * 5. Read attributed or buffer
     * 6. If done reset counter
     *    else goto 5
     */
    /* configure counter */
    for(i = 0; i < 5; i++)
    {
        switch(i)
        {
            case 0:
                /* Reset the counter */
                op[0] = GPCT_RESET;
                break;
            case 1:
                /* Set the source to the external pin */
                op[0] = GPCT_SET_SOURCE;
                /* Set the source to the internal timer */
                /* op[1] = GPCT_INT_CLOCK; */
                op[1] = GPCT_EXT_PIN;
                break;
            case 2:
                /* Set the gate to latch to the external pin */
                op[0] = GPCT_SET_GATE;
                op[1] = GPCT_EXT_PIN;
                /* No latch on the gate */
                /*op[1] = GPCT_NO_GATE;*/
                break;
            case 3:
                /* Every event causes the counter to increase */
                op[0] = GPCT_SET_DIRECTION;
                op[1] = GPCT_UP;
                break;
            case 4:
                op[0] = GPCT_SET_OPERATION;
                op[1] = GPCT_SIMPLE_EVENT;
                break;
        }
        insn.insn = INSN_CONFIG;
        if(i == 0) insn.n=1; else insn.n=2;
        insn.data = op;
        insn.subdev = subdevice;
        insn.chanspec = CR_PACK(0, 0, 0);
        /* perform configure */
        comedi_do_insn(card, &insn);
    }
    /* Read the count from the card */
    comedi_data_read(card, 0, 0, 0, 0, &data);
    printf("data: %d\n", data);
    sleep(2);
    }

    /* unlock the subdevice */
    nError = comedi_unlock(card, 2);
    if(-1 == nError)
    {
        /* there was an error unlocking, oh well, we are exiting anyway */
        printf("Error unlocking subdevice: %s\b",
comedi_strerror(comedi_errno()));
    }
    /* close the device */
    comedi_close(card);

    /* exit gracefully */
    return 0;
}

Thanks for any and all help,
Chuck

====================================
Chuck Haines
chaines_at_wpi.edu
http://www.linux-xtreme.net
====================================
WPI ECE Systems Administrator
WPI Game Development Club Developer
WPI Linux Association Lab Manager
Tau Kappa Epsilon Fraternity
TKE-IT Systems Administrator
====================================
AIM: CyberGrex
YIM: CyberGrex_27
ICQ: 3707881
====================================

Received on 2003-07-21Z11:42:15