Temperature Measurement



Ottmar Klaas
 
email: oklaas@scorec.rpi.edu.  

The program given at the end of this web page can be used to read the temperature data from the serial port connected to the 512 Point Temperature Measurement System developed by Prof. Peter H. Anderson and his students. It has been tested on a SUN LX running Solaris 7, but it should work on other Unix implementations as well. The program will make a measurement every 900 seconds assuming that one temperature sensor is connected to take measurements. The data is then written to a file in the following format:

Run  count  temperature    serial number
 1     00      12.09     106F383B 0000007A    Sun Dec 10 23:48:04 2000
 1     00      12.02     106F383B 0000007A    Mon Dec 11 00:03:04 2000
 1     00      11.96     106F383B 0000007A    Mon Dec 11 00:18:04 2000

In developing the code the following links have been helpful:
Linux documentation: Serial Programming Howto
Serial Programming Guide for POSIX Operating Systems

Please feel free to contact me with any questions.

New (12/22/2000): I have an extended version of the program available that reads an arbitrary number of sensors instead of just one. The program is not fully documented and tested yet, so I'm not posting it to the web page at this time. However, please feel free to contact me if you would like to use it.

/*
Copyright (C) Ottmar Klaas
oklaas@scorec.rpi.edu

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You can receive a copy of the GNU General Public License
following the link, or you can write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

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

main()
{
  time_t t;
  struct tm *tt;
  /* FILE to log the temperature */
  FILE *logfile;
  int fd,res,counter;
  struct termios newtio;
  char buf[255];

  /* Open the serial port ttya.
  O_NDELAY: Don't wait for DCD signal line when opening the port.
  O_RDWR: Open for read and write */
  fd = open("/dev/ttya", O_RDWR | O_NDELAY);

  if (fd < 0)
  {
    printf("Unable to open the serial port\n");
    printf("exit ...\n");
    exit(-1);
  }

  bzero(&newtio, sizeof(newtio));

  /* port settings: 9600, 8bit
  CREAD enables port to read data
  CLOCAL prevents that we become the owner of the port and react to
  control characters and are subject to hang up signals etc. */
  newtio.c_cflag = B9600 | CS8 | CREAD | CLOCAL;
  newtio.c_iflag = IGNPAR;
  newtio.c_oflag = 0;
 

  /* Set line options */
  newtio.c_lflag = 0;

  newtio.c_cc[VTIME] = 0; /* ignore timer */
  newtio.c_cc[VMIN] = 0; /* no blocking read */

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

  counter = 0;
  /* infinite loop to measure the temperature */
  while (1==1)
  {
    int i;
    char *buf2;
    logfile = fopen("temp.log","a");

    /* Send a character. This will initiate the temperature reading */
    i = write(fd,"a",1);
    if (i == -1)
    {
      printf("unable to write\n");
      printf("exit ...\n");
      exit(-1);
    }

    /* Wait a short time so the PIC can finish a reading */
    sleep(2);

    /* Read the data sent by the PIC */
    res = read(fd,buf,255);

    /* Print a header every 50 output lines */
    if (counter % 50 == 0)
      fprintf(logfile,"Run count temperature serial number\n");

    /* Print the 'run' on which this reading was performed */
    fprintf(logfile," %c ",buf[0]);

    /* Hexadecimal number counting the readings */
    fprintf(logfile," %c%c ",buf[1],buf[2]);

    /* Now print the temperature */
    /* Temperature starts at position 4 and ends with space character */
    i = 4;
    while (buf[i] != 32)
      fprintf(logfile,"%c",buf[i++]);

    /* Print rest of reading, which is the serial number */
    fprintf(logfile," ");
    for (;i<res; i++) /* We don't want to write the line feed and carriage return character */
    if ((buf[i] != 10) && (buf[i] != 13))
    fprintf(logfile,"%c",buf[i]);

    if (res!=0)
    {
      /* Get the current time */
      t = time(NULL);
      buf2 = ctime(&t);
      fprintf(logfile," %s",buf2);
    }
    ++counter;
    fclose(logfile);
    sleep(898);
  }
} Hit Counter Marini