Index: /hvcontrol/History.txt
===================================================================
--- /hvcontrol/History.txt	(revision 97)
+++ /hvcontrol/History.txt	(revision 98)
@@ -19,2 +19,4 @@
 24/7/2009   Lockfile is deleted if configuration file cannot be read
 27/7/2009   Slow data is written for every HV ramp to file given in configuration
+29/7/2009   Fixed bug with loading HV settings. No error is produced on negative
+    	    DAC values (the value is set to zero).
Index: /hvcontrol/src/HV.cc
===================================================================
--- /hvcontrol/src/HV.cc	(revision 97)
+++ /hvcontrol/src/HV.cc	(revision 98)
@@ -162,5 +162,5 @@
 
 /* Set high voltage - uses TRead() and has same return values */
-int HVBoard::SetHV(int chain, unsigned int channel, unsigned int hv) {
+int HVBoard::SetHV(int chain, unsigned int channel, int hv) {
 
   if (fTestMode){
@@ -171,8 +171,9 @@
   unsigned char wbuf[] = {0,0,0};
 
-  if (!(hv>=0 && hv<=0x3FFF)) {
-    m->PrintMessage(" Error: HV beyond limits [0 - 0x3FFF]\n"); 
-    return 0;
-  }
+  if (hv > 0x3FFF) {
+    m->PrintMessage(" Error: HV beyond upper 0x3FFF\n"); 
+    return 0;
+  }
+  if (hv<0) hv = 0;
   
   switch (chain) {
Index: /hvcontrol/src/HV.h
===================================================================
--- /hvcontrol/src/HV.h	(revision 97)
+++ /hvcontrol/src/HV.h	(revision 98)
@@ -56,5 +56,5 @@
    int Reset();
    int GetStatus();
-   int SetHV(int, unsigned int, unsigned int);
+   int SetHV(int, unsigned int, int);
    int GetBoardNumber() {return BoardNumber;}
    int Communicate(unsigned char*, int);
Index: /hvcontrol/src/ProcessIO.cc
===================================================================
--- /hvcontrol/src/ProcessIO.cc	(revision 97)
+++ /hvcontrol/src/ProcessIO.cc	(revision 98)
@@ -296,5 +296,5 @@
 
     char Buffer[MAX_COM_SIZE];
-    int NBoards = 0, Errors = 0, Chain;
+    int NBoards = 0, Errors = 0, Chain, Channel;
     unsigned int DACValue;
     FILE *File;
@@ -313,21 +313,27 @@
       for (int Board=0; Board<NumHVBoards; Board++) {
 	if (Match(fHVBoard[Board]->BoardName, Buffer)) {
-
-	  PrintMessage("Found HV settings for board %d (%s)\n",fHVBoard[Board]->GetBoardNumber(), fHVBoard[Board]->BoardName);
-
-	  Chain = 0;
-	  while (fgets(Buffer, sizeof(Buffer), File) && Chain<NUM_CHAINS) {
-    	    if (strlen(Buffer) == 1) continue;  // Ignore if only newline character
-
-    	    for (int Channel=0; Channel<NUM_CHANNELS; Channel++) {
-	      if (sscanf(Buffer, "%u", &DACValue) != 1) {
-    	    	PrintMessage("Error reading DAC values from file, terminating\n");
-		return;
-	      }	      
-	      if (!RampVoltage(DACValue, Board, Chain, Channel)) Errors++;
+	  PrintMessage("Found HV settings for board %d (%s)\n\r",fHVBoard[Board]->GetBoardNumber(), fHVBoard[Board]->BoardName);
+
+	  Chain = 0;  Channel = 0;
+	  while (fscanf(File, "%u", &DACValue)==1 && Chain<NUM_CHAINS) {
+    	    if (!RampVoltage(DACValue, Board, Chain, Channel)) {
+	      Errors++;
+	      PrintMessage("Error: Could not ramp chain %d, channel %d\n", Chain, Channel);
+    	    }
+	    else {
+	      PrintMessage("Ramped chain %d, channel %d to %u (%.2f V)                         \r",
+	      	 Chain,Channel,DACValue,calib->DACToHV(DACValue,Board,Chain,Channel));
+    	    }
+	    fHVBoard[Board]->HVV[Chain][Channel] = calib->DACToHV(fHVBoard[Board]->HV[Chain][Channel], Board, Chain, Channel);
+	    if(++Channel == NUM_CHANNELS) {
+	      Chain++;
+	      Channel = 0;
 	    }
-	    Chain++;
 	  }
-	  PrintMessage("Finished updating board.\n");
+	  if (ferror(File) != 0) {
+	    PrintMessage("Error reading DAC value from file, terminating. (%s)\n",strerror(errno));
+    	    return;
+	  }
+	  else PrintMessage("\nFinished updating board\n");
     	  NBoards++;
 	}
@@ -583,16 +589,19 @@
     if(strlen(Textbuffer)>0 && Textbuffer[strlen(Textbuffer)-1]=='\n') {
       printf("\r%s%s", Textbuffer, Prompt);   // New prompt
-      fflush(stdout);
     }
     else printf("%s", Textbuffer);
-  }
-  // Print to log file
-  if((Target & MsgToLog) && Logfile!=NULL) {
-    fprintf(Logfile, "%s", Textbuffer);
-    fflush(Logfile);
+    fflush(stdout);
   }
   // Print to socket
   if((Target & MsgToSocket) && Socket!=-1) {
     write(Socket, Textbuffer, strlen(Textbuffer));
+  }
+  // Print to log file (repace carriage return by linefeed)
+  if((Target & MsgToLog) && Logfile!=NULL) {
+    for (unsigned int i=0; i<strlen(Textbuffer); i++) {
+      if(Textbuffer[i] == '\r') Textbuffer[i] = '\n';
+    }
+    fprintf(Logfile, "%s", Textbuffer);
+    fflush(Logfile);
   }
 }
