Serial Measurement Kits - C Language Interface

Introduction.

This discussion focuses on a general C Language interface for PC control of all of our serial measurement kits.

Currently our serial measurement kits include the K1820 and K1821 Temperature Measurement Kits, the 200 inch Water Level Measurment Kit and serial SONAR controller. All use the same protocol.

The measurement sequence is intitiated by bringing the PIC input /MEAS low (/GO on the serial SONAR). This is easily done by the PC sending a character to the PIC. The start bit causes /MEAS (/GO) to go low. Note that the character is not actually read by the PIC.

The PIC then makes the measurements and returns the result in serial format as a single string, terminated with a new line character at 2400 baud, 8 bit with no parity (2400 8N1).

The PIC then returns to continually scan /MEAS until it again goes low.

Examples of results;

8 Channel Temperature Measurement:

	 18.24 -0.45 -11.12 0.04 26.7 -99.99 -99.99 87.24 
The format is eight temperature readings in degrees C. -99.99 indicates the channel is not equipped with a DS1820 (or DS1821).

200 Inch Water Level Measurement.

	21.8 12.34 124.5
T_ambient is 21.8 degrees C, T_remote is 12.34 and water level is 124.5 inches.

Serial Sonar.

	03FC
This is a time count in 10 usec intervals from launch of INIT to reception of ECHO. Note that this is in hexadecimal. Thus, 0x03fd = 1021 * 10 usecs or 10.21 ms. FFFF indicates no ECHO was received.

Thus, in all cases, the sequence is intitiated by sending a character and the result is a string. The longest string is associated with the Temperature Measurement System; 57 bytes if all channels are not equipped.

TEMPLATE.C

The following C routine may be used as a template. Note that you must also have SER_MEAS.EXE as TEMPLATE.C executes this from within the program to interface with the serial COM port.

/* Program TEMPLATE.C
**
** Illustrates the use of SER_MEAS.EXE to interface with;
**
**	Temperature Measurement Kits (K1820 and K1821)
**	Water Level Measurement Kit
**	Serial SONAR Interface Kit
**
** Opens specified COM port and sends a character to external PIC
** initiating the measurement sequence.  Prints result to terminal.
**
** Note that in call to make_meas, the ComPort must be specified as
** either 1 or 2 and path must be the full path name of SER_MEAS.EXE.
**
** function make_meas executes SER_MEAS.EXE
**
** 	path port pointer
**
** For example;
**
**    "A:\SER_MEAS.EXE 2 03BC:2A3C"
**
** P. H. Anderson, Dec 11, '97
*/

#include <stdio.h>
#include <dos.h>
#include <process.h>

void make_meas(char *path, int port, char *s);

void main(void)
{
   char result[80], path[80];
   int ComPort=2, n;
   strcpy(path, "A:\\SER_MEAS.EXE");
   for(n=0; n<25; n++)
   {
	 make_meas(path, ComPort, result);
	 printf("%s\n", result);
	 sleep(60);
   }
}

void make_meas(char *path, int port, char *s)
{
   int seg, off;
   char arg1[80], arg2[80];
   char arg_str[80];
   seg = FP_SEG(s);	/* make pointer into a string */
   off = FP_OFF(s);
   sprintf(arg1, "%d", port);	     /* com port 1 or 2 */
   sprintf(arg2, "%.4x:%.4x", seg, off);
   sprintf(arg_str, "%s %s %s", path, arg1, arg2);
   system(arg_str);  /* execute SER_MEAS.EXE */
}
Note that the above program executes SER_MEAS.EXE with command line arguments as to the ComPort and the address of the "result" string. SER_MEAS.EXE opens the specified Com Port, sends a single character, intitiating the measurement sequence. It then receives each character in turn from the PIC measurement unit up to the receipt of the new line character and places these in locations beginning at the specified address . Upon receipt of the newline character or timeout (30 seconds) or more than 80 characters, the Com Port is closed and control is returned to the above template program.

The result is a null terminated "result" string.

Note that in TEMPLATE.C, Com Port (1 or 2) and the full path name of SER_MEAS.EXE must be specified.

For example;

	ComPort = 1;
	strcpy (path, "A:\\SER_MEAS.EXE");
or	strcpy (path, "C:\\TC\\T_MEAS\\SER_MEAS.EXE");
SER_MEAS.EXE will accommodate only COM1 (0x3F8-0x03FF, IRQ4) or COM2 (0x2F8-0x02FF, IRQ3).

SER_MEAS.EXE was written using source code from Quinn Curtis Tools. My software license does not permit me to distribute these materials which are protected by copyright. However, executing SER_MEAS from within the template program using command line arguements to communicate appears to be a viable and simple approach.

The template program has been successfully run on one of the earliest PCs from 1993 running DOS 3.3 and on a Pentium, both from DOS 6.0 and from DOS running under Windows95.

Once the result string is obtained, conversion to numbers is relatively simple and I will leave it for you to take it from there.

For the Temperature Measurement Kits;

	float T[8];

	...
	
	sscanf(result, "%f %f %f %f %f %f %f %f",
			&T[0], &T[1], &T[2], &T[3],
			&T[4], &T[5], &T[6], &T[7]);
For the SONAR kit;
	unsigned int count;
	float t, distance;

	...
	sscanf(result, "%x", &count);
	t=count*10.0e-6;
	distance = 1150.0 /2 * 12 * t;
	/* 1150 / 2 round trip feet * 12 inches * t;