/*
   C-Language example for the 
   Crandun Technologies CTI-AR600 software library.
   Copyright (c) 2001, Crandun Technologies Inc.

   This example shows how to retrieve the sensor's
   firmware version, and how to change the sensor's baud rate.

*/

#include "CTI_AR600.h"	/* required header */
#include <stdio.h>
#include <memory.h>

#define MAXSAMPLES 200

/* A helper macro for checking return codes */
#define CHK_RETURN_VALUE(retCode, fnName) \
if (retCode < 0) { \
		printf("ERROR: " #fnName " returned error code: %ld\n", rc); \
		setCommClosed(sensorHandle); \
		printf("Please enter any character to exit: "); \
		scanf("%c\n", &c); \
		return (-1); \
}

int main()
{
	char c;
	long i;
	long rc;
	float fullScaleSpan, rangeData[MAXSAMPLES];
	char sFirmwareVersion[100];

	long numSkipped;

	long sensorHandle;

	printf("Crandun Technologies CTI-AR600 Baud Rate change sample program.\n");

	sensorHandle = -1;
	sensorHandle = getNewCTIAR600();

	if (sensorHandle < 0) {
		printf("ERROR: getNewCTIAR600 returned error code %ld\n", sensorHandle);
		return -1;
	}
	
	printf("Enter the Full Scale Span of the AR600 sensor you are using (e.g. 4.0): ");
	scanf("%f", &fullScaleSpan);

	printf("Opening the serial port...\n");
	rc = setCommOpen(sensorHandle, "COM1:", 9600, fullScaleSpan);
	CHK_RETURN_VALUE(rc, setCommOpen)
	printf("Successfully opened the serial port!\n");

	/* Query the sensor's firmware version */
	memset(sFirmwareVersion, 0, sizeof(sFirmwareVersion));
	printf("Querying the sensor firmware version...\n");
	rc = getFirmwareVersion(sensorHandle, sFirmwareVersion, sizeof(sFirmwareVersion));
	CHK_RETURN_VALUE(rc, setCommOpen)
	printf("The sensor firmware version is '%s'\n", sFirmwareVersion);


	/* Now change the baud rate */
	printf("Changing baud rate to 19200\n");
	rc = setBaudRate(sensorHandle, 19200);
	CHK_RETURN_VALUE(rc, setBaudRate)
	printf("Successfully changed baud rate to 19200\n");

	printf("Reading samples from sensor...\n");
	rc = getSamples(sensorHandle, rangeData, MAXSAMPLES, 5, 2000);
	CHK_RETURN_VALUE(rc, getSamples)

	printf("Read %ld range samples from the sensor.\n", rc);
	for (i = 0; i < rc; i++)
		printf("Range %d is %8.3f inches\n", i, rangeData[i]);

	numSkipped = getNumBytesSkipped(sensorHandle);
	printf("%ld bytes of data were skipped.\n", numSkipped);
	

	/* Now change the baud rate to 38400 */
	printf("Changing baud rate to 38400\n");
	rc = setBaudRate(sensorHandle, 38400);
	CHK_RETURN_VALUE(rc, setBaudRate)
	printf("Successfully changed baud rate to 38400\n");

	printf("Reading samples from sensor...\n");
	rc = getSamples(sensorHandle, rangeData, MAXSAMPLES, 5, 2000);
	CHK_RETURN_VALUE(rc, getSamples)

	printf("Read %ld range samples from the sensor.\n", rc);
	for (i = 0; i < rc; i++)
		printf("Range %d is %8.3f inches\n", i, rangeData[i]);

	numSkipped = getNumBytesSkipped(sensorHandle);
	printf("%ld bytes of data were skipped.\n", numSkipped);
	
		

	/* closing the comm port changes the baud rate back to factory default of 9600 */
	setCommClosed(sensorHandle);
	setReleaseHandle(sensorHandle);

	printf("Please enter any character to exit: ");
	scanf("%c\n", &c);
	return 0;
}
