// Telerobotics Senior Project
// Webbot 1.0 - Robot Control System
// (c) 2000 Bradley University ECE
// ---------------------------------
// botadc.c - Test an ADC channel
// Written by: John Kiolbasa
// Command line syntax:
//	botadc c [-v]
//	
//	where:	c is 0 - 15
//		-v   verbose output

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>

int main(int argc, char **argv)
{
	int sp;
	struct termios tios;
	int buff;
	int i = 0;
	int amt;
	int val;

	amt = atoi(argv[1]);

	buff = 128+64+32+16;
	buff += amt;

	sp = open("/dev/ttyS0", O_RDWR|O_NOCTTY);
	if(sp == -1)
	{
		printf("Error opening serial port.\n");
		return 1;
	}

	tcflush(sp, TCIOFLUSH);
	write(sp, &buff, 1);

	while(!read(sp, &buff, 1)) {}

	while(!read(sp, &val, 1)) {}

	printf("%c", val);

	if(argc > 2 && argv[2][0] == '-' && argv[2][1] == 'v')
	{
		printf("Command sent: %d\n", buff);
		printf("Value returned: %d\n", val);
	}

	return 0;
}


syntax highlighted by Code2HTML, v. 0.8.11