//////////////////////////////////////////////////////////////////////
//
// PmcaEx.cpp: PMCA COM Example
//
//////////////////////////////////////////////////////////////////////

#include <windows.h>
#include <stdio.h>
#include "PmcaCom.h"
#include "PmcaErr.h"

static int g_port;
static int g_gain;

// command line: example <com port>
// device type 0 - AUTO DETECT, 1 - PMCA 8000, 2 - PMCA 8000A 
int main(int argc, char *argv[])
{
    int stat = 0;
    int result;
    unsigned long buff[1024];
    if (argc > 1)
        stat = atoi(argv[1]);
    while (stat > 4 || stat < 1)
    {
        printf("Enter Com Port Number:");
        scanf("%d", &stat);
    }
    g_port = stat;
    stat = PmcaPowerUp(g_port);
    if (stat)
        goto L_Terminate;

    printf("\nConnecting - ");
    stat = PmcaConnect(g_port,
            115200,             // Baud rate
            PmcaDeviceType(0)); // Auto detect MCA type
    if (stat)
        goto L_Terminate;

    printf("Connected\n");

    stat = PmcaSetGain(1024);
    if (stat)
        goto L_Terminate;

    stat = PmcaSetGroup(12);
    if (stat)
        goto L_Terminate;

    stat = PmcaAsyncClearDataAndTime(&result);
    if (stat < 0)
        goto L_Terminate;

    stat = PmcaWait(stat);
    if (stat && stat != PMCA_ERROR_REQUEST_NOT_FOUND)
        goto L_Terminate;

    stat = PmcaSetAcquisitionTime(100);     // 100 seconds
    if (stat)
        goto L_Terminate;

    stat = PmcaStartAcquisition(TRUE);
    if (stat)
        goto L_Terminate;

    Sleep(2000);    // Wait 2 seconds

    stat = PmcaStopAcquisition();
    if (stat)
        goto L_Terminate;
    stat = PmcaGetData(buff, 0, 1024);
    if (stat)
        goto L_Terminate;

    printf("Test completed successfully\n");
L_Leave:
    PmcaTerminateCom();
    return stat;
L_Terminate:
    printf("Test failed\n");
    goto L_Leave;
}
