Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tlug] measureTemp_cold.c Release early, release often (was Re: just a quick note... more to come later)



On Thu, 27 Jul 2006 10:19:40 +0200 Michal Hajek <hajek1@example.com> wrote:

> Hello again :)

Ahh, the giggling school girl is back. Hi! 

> My plan is also to reveal the whole thing online (experimental setup
> scheme and the the source code itself too) with more comments. I am open
> to all kinds of suggestions and ideas. No problem here.  

> [1] http://material.karlov.mff.cuni.cz/people/hajek/pec/

Release early, release often. Wham! Here goes first pass at cleanup. 
^^^ in comments is grepable for todo list: 

measureTemp_cold.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include <gpib/ib.h>
#include <sys/timeb.h>
#include <math.h>
#include "lojza.h"

#define MAX_KEITHLEY_REPLY_LEN (16) /* ^^^ this probably belongs in keithley.h */

/* ibwrtstr is a wrapper for ibwrt to avoid need for manually counting chars 
*  in strings (and the inevitable bugs therefrom) */

int ibwrtstr(int ud,const void *data)
{
   return ibwrt(ud,data,(long)strlen((char *)data);
}

double measureTemp_cold(void)
{
   char buffer[MAX_KEITHLEY_REPLY_LEN+1/* damn well better have place for [ibcnt]='\0' when ibcnt==MAX_KEITHLEY_REPLY_LEN*/];
   double reference_voltage; /*^^^ units? volts? milliVolts? uV? nV? kV? MV? GV? TV? */

   double t2; /* in Celsius */

   /* MERENI VZORKU */

   /* configure K706 */
   ibwrtstr(K706Ud,"C10C11X");
   ibwrtstr(K2182Ud,"*trg");
   ibwrtstr(K2182Ud,":SENSE:DATA:FRESh?"); /*^^^ just curious: FRESh or FRESH */

   ibrd(K2182Ud,buffer,MAX_KEITHLEY_REPLY_LEN);
   printf("ibcnt = %d \n",ibcnt);
   buffer[ibcnt]='\0';
   ibwrtstr(K706Ud,"N10N11X");

   reference_voltage = atof(buffer);
   printf("Reference voltage = %.12f V\n",reference_voltage);

   t2 = 30.0 - (1000.0 * reference_voltage); /* ^^^ where did this formula come
from. Cite reference */
   printf("Reference temperature = %.2f Celsius \n",t2);

   return t2;
}

Do use indentation. 
Document units of values. 
Cite references for formulas. 
Let the computer do the boring work (counting chars for ibwrt()), 
so spare you the tedium, and to about stupid mistakes. 
Avoid magic numbers. Document them with #define names. 
Follow the convention of using lower case for variable names
   T2 is bad. t2 is OK
Beware of off by one errors 
   Your original code: 
      char reference_volt[16]; 
      reference_volt[ibcnt]='\0'; <---------- BUG!  BUG!  BUG!  BUG!  BUG!  BUG! 
   could write past end of the buffer
Avoid magic numbers 
   char buffer[16+1]; would be better as: 
   #define MAX_KEITHLEY_REPLY_LEN (16)
   char buffer[MAX_KEITHLEY_REPLY_LEN+1];

>  :) ... :) 

Ah the giggling school girl continues. 

> And since my own nature is optimistic, 

Good! 

> I have been experimenting with measureI_Vplus() at the moment mainly. 

I'll look there next. 



Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links