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

#define BAUDRATE B57600
#define MODEMDEVICE "/dev/cua0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;
void SetPVA(unsigned long p, unsigned long v, unsigned long a);

int fd;

main(int argc, char **argv)
{
  struct tms tmsbuff;
  clock_t time,starttime;
  long position;
  int i,count,ret;
  int loop;
  unsigned char buf[40];
  struct termios oldtio,newtio;
 unsigned long p; unsigned long v; unsigned long a;

 argc--;*argv++;
 if(argc-- > 0) p= atol(*argv++);
 if(argc-- > 0) v= atol(*argv++);
 if(argc-- > 0) a= atol(*argv++);
 printf("Setting values to  %ld %ld %ld\n",p,v,a);

 fd = open(MODEMDEVICE, O_RDWR);
 if (fd <0) {perror(MODEMDEVICE); exit(-1); }

 tcgetattr(fd,&oldtio); /* save current port settings */

 /* bzero(newtio, sizeof(newtio)); */
 newtio.c_cflag = BAUDRATE | CS8 | CREAD;
 newtio.c_iflag = 0;
 newtio.c_oflag = 0;

 /* set input mode (non-canonical, no echo,...) */
 newtio.c_lflag = 0;

 newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
 newtio.c_cc[VMIN]     = 1;   /* blocking read until 1 chars received */

 tcflush(fd, TCIFLUSH);
 tcsetattr(fd,TCSANOW,&newtio);

 SetPVA(p,v,a);
 starttime = times(&tmsbuff);
for(i=0;i<1000;i++) {
 RequestStatus();
 count = 0;
 do {
   ret = read(fd,&buf[count],6-count);
   if(count == 0) time = times(&tmsbuff);
   count += ret;
  } while (count < 6);
  position = buf[1];
  position |= (buf[2] << 8);
  position |= (buf[3] << 16);
  position |= (buf[4] << 24);
  printf("%f 0x%02x %ld (0x%02x %02x %02x %02x) C = 0x%x\n", (float)((time-starttime)/100.0),buf[0],position,buf[1],buf[2],buf[3],buf[4],buf[5]);
}

 tcsetattr(fd,TCSANOW,&oldtio);
}

void
SetPVA(unsigned long p, unsigned long v, unsigned long ac){
    unsigned char a[40];
    int i = 0;
    int j;
    unsigned char sum = 0;
	int ret;

    a[i++] = 0xAA;
    a[i++] = 0x02;
    a[i++] = 0xd4;
    a[i++] = 0x97;

    a[i++] = p & 0xFF;
    a[i++] = (p >>  8) & 0xff;
    a[i++] = (p >> 16) & 0xff;
    a[i++] = (p >> 24) & 0xff;

    a[i++] = v & 0xFF;
    a[i++] = (v >>  8) & 0xff;
    a[i++] = (v >> 16) & 0xff;
    a[i++] = (v >> 24) & 0xff;

    a[i++] = ac & 0xFF;
    a[i++] = (ac >>  8) & 0xff;
    a[i++] = (ac >> 16) & 0xff;
    a[i++] = (ac >> 24) & 0xff;

	sum = 0;
    for(j=1;j<i;j++) sum += a[j];
    a[i] = sum & 0xFF;

    for(j=0;j<=i;j++) {
		ret = write(fd,&a[j],1);
		if(ret != 1) {
			printf("write returned %d errno = %d\n",ret,errno);
			perror("Write: ");
		}
	}
	ReadStatus();
}

ReadStatus () {
    unsigned char buf[4];
    int ret;
    int count = 0;

 do {
   count += read(fd,&buf[count],2-count);
  } while (count < 2);
	printf("ReadStaus: count = %d 0x%02x %02x\n",count,buf[0],buf[1]);
}

RequestStatus () {
    unsigned char a[40];
    int i = 0;
    int j;
    unsigned char sum = 0;
	int ret;

    a[i++] = 0xAA;
    a[i++] = 0x02;
    a[i++] = 0x13;
    a[i++] = 0x01;

	sum = 0;
    for(j=1;j<i;j++) sum += a[j];
    a[i] = sum & 0xFF;

    for(j=0;j<=i;j++) {
		ret = write(fd,&a[j],1);
		if(ret != 1) {
			printf("write returned %d errno = %d\n",ret,errno);
			perror("Write: ");
		}
	}
}
