/*
   C-Language example for the 
   Crandun Technologies CTI-AR200 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_AR200.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); \
		{	char errMsg[300]; if (getIsError(sensorHandle)) { \
				getErrorMessage(sensorHandle, errMsg, sizeof(errMsg)); \
				printf("Library err msg is: %s\n", errMsg); \
			} \
		} \
		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];
	char sLibName[100];

	long numSkipped;

	long sensorHandle;

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

	sensorHandle = -1;
	sensorHandle = getNewCTIAR200();

	if (sensorHandle < 0) {
		printf("ERROR: getNewCTIAR200 returned error code %ld.\n", sensorHandle);
		return -1;
	}
	
	printf("Enter the Full Scale Span of the AR200 sensor you are using (e.g. 25): ");
	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");

	/* Get 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, getFirmwareVersion)
	printf("The sensor firmware version is '%s'.\n", sFirmwareVersion);

	/* Get the library's name */
	memset(sLibName, 0, sizeof(sLibName));
	printf("Querying the library's name ...\n");
	rc = getLibraryName(sensorHandle, sLibName, sizeof(sLibName));
	CHK_RETURN_VALUE(rc, getLibraryName)
	printf("The library name is '%s'.\n", sLibName);


	/* 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");

	/* Change the sensor sample rate */
	printf("Changing sample rate to 10 samples/sec.\n");
	rc = setSamplesPerSec(sensorHandle, 10);
	CHK_RETURN_VALUE(rc, setSamplesPerSec)
	printf("Successfully changed sample rate.\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 mm.\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 mm.\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;
}
