#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/fcntl.h>
#include <sys/termios.h>
#include <sys/ioctl_compat.h>

#define BAUD       1200

unsigned char buff[256] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
char txbuff[256];
float tm;
float rh;
char flag = 1;

int FD232c;
struct termios qtermios;                     /* termios.h */

static char sio[] = "/dev/cuaa0";

int main()
{
	int r, c;
 	if((r = open232c(sio)) > 0) {
	  fprintf(stderr, "%d\n", r);
	  exit (1);
	}
	txbuff[0] = 0x0b;
	while (flag) {
	        flag = 0;
		write(FD232c, txbuff, 1);
		sleep(1);
		if ((c = read(FD232c, buff, 10)) > 0) {
//			fprintf(stderr, "%x %x %x %x %x %x %x %x %x %x :",
//			buff[0], buff[1], buff[2], buff[3], buff[4], buff[5], buff[6], buff[7], buff[8], buff[9]);

			tm = (float)(buff[2] + (buff[3] * 256) - 1000)/10;
			rh = (float)(buff[4] + (buff[5] * 256) - 1000)/10;
//			fprintf(stderr, "%1.1f %1.1f\n", tm, rh);
			fprintf(stdout, "%1.0f\n%1.0f\ndig/rh%%\nTR72S\n", tm, rh);

		}
	}
	close(FD232c);
}


int open232c(char* ttyname)
{
  struct termios  tty;
  int             r;
  
  if((FD232c = open(ttyname, O_RDWR|O_NDELAY|O_NONBLOCK, 0477)) >= 0) {
    qtermios.c_iflag = IXOFF;
    qtermios.c_lflag = (PENDIN|ECHOKE|ECHOE);
    qtermios.c_cflag = (CLOCAL|HUPCL|CREAD|CS8);
    qtermios.c_ispeed = BAUD;
    qtermios.c_ospeed = BAUD;
    tcsetattr (FD232c, TCSANOW, &qtermios);
    return 0;
  }
  perror("Can't open SIO.\n");
  return 1; 
}