Index: rw2300.h
===================================================================
--- rw2300.h	(revision 12)
+++ rw2300.h	(working copy)
@@ -88,6 +88,7 @@
 
 struct timestamp
 {
+	int second; //Only used for setting time.
 	int minute;
 	int hour;
 	int day;
@@ -253,6 +254,9 @@
                         
 void light(WEATHERSTATION ws2300, int control);
 
+void datetime(WEATHERSTATION ws2300,
+			  struct timestamp *utc_time,
+			  int timezone);
 
 /* Generic functions */
 
Index: Makefile
===================================================================
--- Makefile	(revision 12)
+++ Makefile	(working copy)
@@ -34,6 +34,7 @@
 INTERVALOBJ = interval2300.o rw2300.o linux2300.o win2300.o
 MINMAXOBJ = minmax2300.o rw2300.o linux2300.o win2300.o
 MYSQLHISTLOGOBJ = mysqlhistlog2300.o rw2300.o linux2300.o win2300.o
+SYNCOBJ = sync2300.o rw2300.o linux2300.o win2300.o
 
 VERSION = 1.11
 
@@ -47,7 +48,7 @@
 
 ####### Build rules
 
-all: open2300 dump2300 log2300 fetch2300 wu2300 cw2300 history2300 histlog2300 bin2300 xml2300 light2300 interval2300 minmax2300 mysql2300 mysqlhistlog2300
+all: open2300 dump2300 log2300 fetch2300 wu2300 cw2300 history2300 histlog2300 bin2300 xml2300 light2300 sync2300 interval2300 minmax2300 mysql2300 mysqlhistlog2300
 
 open2300 : $(OBJ)
 	$(CC) $(CFLAGS) -o $@ $(OBJ) $(CC_LDFLAGS)
@@ -93,10 +94,12 @@
 	
 minmax2300: $(MINMAXOBJ)
 	$(CC) $(CFLAGS) -o $@ $(MINMAXOBJ) $(CC_LDFLAGS) $(CC_WINFLAG)
-	
+		
 mysqlhistlog2300 :
 	$(CC) $(CFLAGS) -o mysqlhistlog2300 mysqlhistlog2300.c rw2300.c linux2300.c $(CC_LDFLAGS) $(CC_WINFLAG) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient
 
+sync2300: $(SYNCOBJ)
+	$(CC) $(CFLAGS) -o $@ $(SYNCOBJ) $(CC_LDFLAGS)
 
 install:
 	mkdir -p $(bindir)
@@ -113,12 +116,14 @@
 	$(INSTALL) minmax2300 $(bindir)
 	$(INSTALL) mysql2300 $(bindir)
 	$(INSTALL) mysqlhistlog2300 $(bindir)
+	$(INSTALL) sync2300 $(bindir)
 	
 uninstall:
-	rm -f $(bindir)/open2300 $(bindir)/dump2300 $(bindir)/log2300 $(bindir)/fetch2300 $(bindir)/wu2300 $(bindir)/cw2300 $(bindir)/xml2300 $(bindir)/light2300 $(bindir)/interval2300 $(bindir)/minmax2300 $(bindir)/mysql2300 $(bindir)/mysqlhistlog2300
+	rm -f $(bindir)/open2300 $(bindir)/dump2300 $(bindir)/log2300 $(bindir)/fetch2300 $(bindir)/wu2300 $(bindir)/cw2300 $(bindir)/xml2300 $(bindir)/light2300 $(bindir)/interval2300 $(bindir)/minmax2300 $(bindir)/mysql2300 $(bindir)/mysqlhistlog2300 $(bindir)/sync2300
 
 clean:
-	rm -f *~ *.o open2300 dump2300 log2300 fetch2300 wu2300 cw2300 history2300 histlog2300 bin2300 xml2300 mysql2300 pgsql2300 light2300 interval2300 minmax2300 mysql2300 mysqlhistlog2300
+	rm -f *~ *.o open2300 dump2300 log2300 fetch2300 wu2300 cw2300 history2300 histlog2300 bin2300 xml2300 mysql2300 pgsql2300 light2300 interval2300 minmax2300 mysql2300 mysqlhistlog2300 sync2300
 
 cleanexe:
-	rm -f *~ *.o open2300.exe dump2300.exe log2300.exe fetch2300.exe wu2300.exe cw2300.exe history2300.exe histlog2300.exe bin2300.exe xml2300.exe mysql2300.exe pgsql2300.exe light2300.exe interval2300.exe minmax2300.exe
\ No newline at end of file
+	rm -f *~ *.o open2300.exe dump2300.exe log2300.exe fetch2300.exe wu2300.exe cw2300.exe history2300.exe histlog2300.exe bin2300.exe xml2300.exe mysql2300.exe pgsql2300.exe light2300.exe interval2300.exe minmax2300.exe sync2300.exe
+
Index: memory_map_2300.txt
===================================================================
--- memory_map_2300.txt	(revision 12)
+++ memory_map_2300.txt	(working copy)
@@ -518,7 +518,7 @@
 01FE 0
 01FF 0
 
-TIME
+TIME (UTC)
 0200 0    Current time: Seconds BCD 1s
 0201 1    Current time: Seconds BCD 10s
 0202 6    Current time: Minutes BCD 1s
@@ -579,7 +579,7 @@
 0239 0
 023A 0
 
-TIME
+TIME (LOCAL - reset on minute interval from UTC time)
 023B 6    Current Time: Minutes BCD 1s
 023C 2    Current Time: Minutes BCD 10s
 023D 3    Current Time: Hours BCD 1s
@@ -606,10 +606,15 @@
 0250 0    Month the unit was last set to: BCD 10s
 0251 3    Year the unit was last set to: BCD 1s
 0252 0    Year the unit was last set to: BCD 10s
-0253 0
-0254 0
-0255 0
-0256 0
+
+TIMEZONE
+0253 0    Zone: Binary (2's comp) nibble 0 [Range F4 (-12) to 0C (+12)]
+0254 0    Zone: Binary (2's comp) nibble 1 (high bit is also sign for BCD value)
+0255 0    Zone: BCD 1s 
+0256 0    Zone: BCD 10s
+Note: Must set both binary and BCD values to change zone.
+      Local time is recalculated at the next minute interval
+
 0257 0
 0258 0
 0259 0
@@ -625,7 +630,11 @@
 0263 0
 0264 3
 0265 5
+
+LCD
 0266 4    LCD contrast value -1. Changing it has no impact on LCD. Read only
+
+
 0267 1
 0268 0
 0269 0
Index: sync2300.c
===================================================================
--- sync2300.c	(revision 0)
+++ sync2300.c	(revision 0)
@@ -0,0 +1,98 @@
+/*  open2300 - histlog2300.c
+ *  
+ *  Version 1.11
+ *  
+ *  Control WS2300 weather station
+ *  
+ *  Copyright 2003-2005, Kenneth Lavrsen
+ *  This program is published under the GNU General Public license
+ */
+
+#include "rw2300.h"
+
+/********************************************************************
+ * print_usage prints a short user guide
+ *
+ * Input:   none
+ * 
+ * Output:  prints to stdout
+ * 
+ * Returns: exits program
+ *
+ ********************************************************************/
+void print_usage(void)
+{
+	printf("\n");
+	printf("sync2300 - Set WS2300 data from config file.\n");
+	printf("Version %s (C)2007 Kenneth Lavrsen.\n", VERSION);
+	printf("This program is released under the GNU General Public License (GPL)\n\n");
+	printf("Usage:\n");
+	printf("sync2300 config_filename\n");
+	exit(0);
+}
+
+ 
+/********** MAIN PROGRAM ************************************************
+ *
+ * This program synchronises the control structures of the WS23xx
+ * with values specified in the config file.
+ * It also sets the date, time and timezone to the server's time
+ * 
+ * Just run the program without parameters for usage.
+ *
+ * It uses the config file for device name.
+ * Config file locations - see open2300.conf-dist
+ *
+ ***********************************************************************/
+int main(int argc, char *argv[])
+{
+	WEATHERSTATION ws2300;
+	struct config_type config;
+
+	struct timestamp timestamp_now;
+	time_t time_t_now;
+	struct tm tm_now;
+
+	if (argc > 1)
+	{
+		print_usage();
+	}
+
+	// Get serial port from config file. Use 2nd command line parameter
+
+	get_configuration(&config, argv[1]);
+
+	// Setup serial port
+
+	ws2300 = open_weatherstation(config.serial_device_name);
+	
+	time(&time_t_now);
+	//Call localtime to get gmtoffset Is there a better way?
+	localtime_r(&time_t_now,&tm_now);
+	
+	timestamp_now.second = tm_now.tm_sec;
+	timestamp_now.minute = tm_now.tm_min;
+	timestamp_now.hour = tm_now.tm_hour;
+	timestamp_now.day =  tm_now.tm_mday;
+	timestamp_now.month = tm_now.tm_mon + 1;
+	timestamp_now.year = tm_now.tm_year % 100;		
+	
+	int timezone = 0;
+	/*
+	printf("Localtime %d %d %d %d:%d:%d %d\n",
+			timestamp_now.year,
+			timestamp_now.month,
+			timestamp_now.day,
+			timestamp_now.hour,
+			timestamp_now.minute,
+			timestamp_now.second,
+			timezone);
+	*/	
+	datetime(ws2300,&timestamp_now,timezone);
+	
+	// Goodbye and Goodnight
+	close_weatherstation(ws2300);
+	
+	return(0);
+}
+

Property changes on: sync2300.c
___________________________________________________________________
Name: svn:executable
   + *

Index: interval2300.c
===================================================================
--- interval2300.c	(revision 12)
+++ interval2300.c	(working copy)
@@ -27,8 +27,8 @@
 	printf("Version %s (C)2003-2004 Kenneth Lavrsen.\n", VERSION);
 	printf("This program is released under the GNU General Public License (GPL)\n\n");
 	printf("Usage:\n");
-	printf("Set Interval: light2300 interval_minutes next_dataset_minutes config_filename\n");
-	printf("Read current Interval: light2300 0 0 config_filename\n");
+	printf("Set Interval: interval2300 interval_minutes next_dataset_minutes config_filename\n");
+	printf("Read current Interval: interval2300 0 0 config_filename\n");
 	printf("If interval is set to 0 the current setting is displayed but nothing changed\n");
 	exit(0);
 }
Index: rw2300.c
===================================================================
--- rw2300.c	(revision 12)
+++ rw2300.c	(working copy)
@@ -2169,7 +2169,7 @@
  *         timelast - Time/Date for last measurement (timestamp struct)
  *         no_records - number of valid records (integer)
  *
- * Returns: interger pointing to last written record. [0x00-0xAE]
+ * Returns: integer pointing to last written record. [0x00-0xAE]
  *
  ********************************************************************/
 int read_history_info(WEATHERSTATION ws2300, int *interval, int *countdown,
@@ -2336,8 +2336,113 @@
 	return;	
 }
 
+/********************************************************************
+ * Binary Coded Decimal
+ * value split into  exponent decimal digits
+ * and placed into data array starting at offset
+ ********************************************************************/
+void bcd_encoder(int value,int exponent, unsigned char *data, int offset)
+{
+	int i = 0;
+	for (i = 0; i < exponent; i++)
+	{
+		data[offset+i] = value % 10;
+		value = value / 10;		
+	}
+}
 
 /********************************************************************
+ * BCD encode hour and minute into data starting at offset
+ ********************************************************************/
+void time_encoder(struct timestamp *time, unsigned char *data, int offset)
+{
+	bcd_encoder(time->minute,2,data,offset);
+	bcd_encoder(time->hour,2,data,offset+2);
+}
+
+/********************************************************************
+ * BCD encode day,month,year into data starting at offset
+ ********************************************************************/
+void date_encoder(struct timestamp *time, unsigned char *data, int offset)
+{
+	bcd_encoder(time->day,2,data,offset+0);
+	bcd_encoder(time->month,2,data,offset+2);
+	bcd_encoder(time->year,2,data,offset+4);
+}
+
+/********************************************************************
+ * Encode timezone
+ * 1 byte 2's complement 
+ * absolute value encoded as BCD.
+ ********************************************************************/
+void timezone_encoder(int timezone, unsigned char *data)
+{
+	//Check for bogus timezone
+	if (timezone < -12 || timezone > 12) 
+	{
+		fprintf(stderr,"Timezone %d\n",timezone);
+		timezone = 0;
+	}
+	
+	//Binary 
+	data[0] = timezone & 0xF;
+	data[1] = ( timezone >> 4 ) & 0xF;
+	
+	//Also stored as BCD of absolute value
+	if (timezone < 0)
+		timezone = timezone * -1;
+	
+	bcd_encoder(timezone,2,data,2);
+
+}
+
+void debug_data(char *msg, int address, unsigned char *data, int nibbles) 
+{
+	printf("%s",msg);
+	int i=0;
+	// Write out the data
+	for (i=0; i<nibbles;i++)
+	{
+		printf("Address: %04X - Data: %01X\n",
+		       address+i, data[i] & 0xF);
+	}
+}
+
+/********************************************************************
+ * Set date, time and timezone on the base station
+ ********************************************************************/
+void datetime(WEATHERSTATION ws2300, struct timestamp *utc_time, int timezone)
+{
+	unsigned char data[6];
+	unsigned char command[25];
+	int utc_address=0x200;
+	int date_address=0x024D;
+	int tz_address=0x0253;
+	
+	bcd_encoder(utc_time->second,2,data,0);
+	time_encoder(utc_time,data,2);
+	
+	//debug_data("Time\n",utc_address,data,6);
+	
+	if (write_safe(ws2300,utc_address,6,WRITENIB, data,command)!=6) 
+		write_error_exit();
+	
+	date_encoder(utc_time,data,0);
+	//debug_data("Date\n",date_address,data,6);
+	
+	if (write_safe(ws2300,date_address,6,WRITENIB,data,command)!=6) 
+			write_error_exit();
+	
+	timezone_encoder(timezone,data);
+	//debug_data("Timezone\n",tz_address,data,4);
+	
+	if (write_safe(ws2300,tz_address,4,WRITENIB, data,command)!=4) 
+			write_error_exit();
+	
+}
+
+
+/********************************************************************
  * read_error_exit
  * exit location for all calls to read_safe for error exit.
  * includes error reporting.
