-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWS2300base.cs
3037 lines (2565 loc) · 113 KB
/
WS2300base.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Text;
/* C# code based on OpenSource Project open2300 - win2300 library functions
*
* Version 1.10
*
* Control WS2300 weather station
*
* Copyright 2003-2005, Kenneth Lavrsen
* This program is published under the GNU General Public license
*/
namespace WS2300
{
class WS2300base
{
private WEATHERSTATION wsdevice;
public struct config_type
{
//char timezone[6]; //not integer because of half hour time zones
public double wind_speed_conv_factor; //from m/s to km/h or miles/hour
public int temperature_conv; //0=Celsius, 1=Fahrenheit
public double rain_conv_factor; //from mm to inch
public double pressure_conv_factor; //from hPa (=millibar) to mmHg
public string history_log_type; //cvs or sql
public string port;
}
public struct timestamp
{
public int minute;
public int hour;
public int day;
public int month;
public int year;
}
#region ws2300
const int MAXRETRIES = 50;
const int MAXWINDRETRIES = 20;
const int WRITENIB = 0x42;
const int SETBIT = 0x12;
const int UNSETBIT = 0x32;
const int WRITEACK = 0x10;
const int SETACK = 0x04;
const int UNSETACK = 0x0C;
public const byte RESET_MIN = 0x01;
public const byte RESET_MAX = 0x02;
const double METERS_PER_SECOND = 1.0;
const double KILOMETERS_PER_HOUR = 3.6;
const double MILES_PER_HOUR = 2.23693629;
const int CELSIUS = 0;
const int FAHRENHEIT = 1;
const int MILLIMETERS = 1;
const double INCHES = 25.4;
const double HECTOPASCAL = 1.0;
const double MILLIBARS = 1.0;
const double INCHES_HG = 33.8638864;
public WS2300base(String port)
{
wsdevice = new WEATHERSTATION(port);
}
public void close()
{
wsdevice.Close();
}
/********************************************************************
* reset_06 WS2300 by sending command 06 (windows version)
*
* Input: device number of the already open serial port
*
* Returns: nothing, exits progrsm if failing to reset
*
********************************************************************/
void reset_06()
{
byte[] command = { 0x06 };
byte[] answer = { 0xFF };
int i;
for (i = 0; i < MAXRETRIES; i++)
{
//printf("Iteration = " + i.ToString(), true);
//PurgeComm(serdevice, PURGE_RXCLEAR);
wsdevice.DiscardInBuffer();
write_device(command, 1);
// Occasionally 0, then 2 is returned. If zero comes back, continue
// reading as this is more efficient than sending an out-of sync
// reset and letting the data reads restore synchronization.
// Occasionally, multiple 2's are returned. Read with a fast timeout
// until all data is exhausted, if we got a two back at all, we
// consider it a success
while (1 == read_device(answer, 1))
{
if (answer[0] == 2)
{
// clear anything that might come after the response
//PurgeComm(serdevice, PURGE_RXCLEAR);
wsdevice.DiscardInBuffer();
return;
}
}
Sleep(5 * i);
}
printf("Could not reset");
exit(0);
}
/********************************************************************
* read_device WIN32 emulation of Linux read()
* Reads data from the handle
*
* Inputs: serdevice - opened file handle
* buffer - pointer to the buffer to read into
* size - number of bytes to read
*
* Output: *buffer - modified on success
*
* Returns: number of bytes read
*
********************************************************************/
int read_device(byte[] buffer, int size)
{
int i = 0;
try
{
//while (serdevice.InBufferBytes > 0 && i < size)
//while (serdevice.BytesToRead > 0 && i < size)
{
buffer[i++] = (byte)wsdevice.ReadByte();
}
}
catch
{
return -1;
}
return i;
/*
DWORD dwRead = 0;
if (!ReadFile(serdevice, buffer, size, &dwRead, NULL))
{
return -1;
}
return (int)dwRead;
*/
}
int read_device(ref byte buffer)
{
byte[] buff ={ buffer };
int i = read_device(buff, 1);
buffer = buff[0];
return i;
}
/********************************************************************
* write_device WIN32 emulation of Linux write()
* Writes data to the handle
*
* Inputs: serdevice - opened file handle
* buffer - pointer to the buffer to write from
* size - number of bytes to write
*
* Returns: number of bytes written
*
********************************************************************/
int write_device(byte[] buffer, int size)
{
try
{
wsdevice.Write(buffer, 0, size);
}
catch
{
size = -1;
}
return size;
/*
DWORD dwWritten;
if (!WriteFile(serdevice, buffer, size, &dwWritten, NULL))
{
return -1;
}
return (int)dwWritten;
* */
}
int write_device(byte buffer)
{
byte[] buff ={ buffer };
return write_device(buff, 1);
}
/********************************************************************
* sleep_short - Windows version
*
* Inputs: Time in milliseconds (integer)
*
* Returns: nothing
*
********************************************************************/
void sleep_short(int milliseconds)
{
Sleep(milliseconds);
}
/********************************************************************
* sleep_long - Windows version
*
* Inputs: Time in seconds (integer)
*
* Returns: nothing
*
********************************************************************/
void sleep_long(int seconds)
{
Sleep(seconds * 1000);
}
/********************************************************************
* address_encoder converts an 16 bit address to the form needed
* by the WS-2300 when sending commands.
*
* Input: address_in (interger - 16 bit)
*
* Output: address_out - Pointer to an unsigned character array.
* 3 bytes, not zero terminated.
*
* Returns: Nothing.
*
********************************************************************/
private void address_encoder(int address_in, byte[] address_out)
{
int i = 0;
int adrbytes = 4;
int shift;
byte nibble;
for (i = 0; i < adrbytes; i++)
{
//nibble = (byte)((byte)(address_in >> (4 * (3 - i))) & 0x0F);
shift = address_in >> (4 * (3 - i));
nibble = (byte)((byte)shift & 0x0F);
address_out[i] = (byte)(0x82 + (nibble * 4));
}
return;
}
/********************************************************************
* data_encoder converts up to 15 data bytes to the form needed
* by the WS-2300 when sending write commands.
*
* Input: number - number of databytes (integer)
* encode_constant - unsigned char
* 0x12=set bit, 0x32=unset bit, 0x42=write nibble
* data_in - char array with up to 15 hex values
*
* Output: address_out - Pointer to an unsigned character array.
*
* Returns: Nothing.
*
********************************************************************/
void data_encoder(int number, byte encode_constant,
byte[] data_in, byte[] data_out)
{
int i = 0;
for (i = 0; i < number; i++)
{
data_out[i] = (byte)(encode_constant + (data_in[i] * 4));
}
return;
}
/********************************************************************
* numberof_encoder converts the number of bytes we want to read
* to the form needed by the WS-2300 when sending commands.
*
* Input: number interger, max value 15
*
* Returns: bytewhich is the coded number of bytes
*
********************************************************************/
byte numberof_encoder(int number)
{
int coded_number;
coded_number = (byte)(0xC2 + number * 4);
if (coded_number > 0xfe)
coded_number = 0xfe;
return (byte)coded_number;
}
/********************************************************************
* command_check0123 calculates the checksum for the first 4
* commands sent to WS2300.
*
* Input: pointer to char to check
* sequence of command - i.e. 0, 1, 2 or 3.
*
* Returns: calculated checksum as unsigned char
*
********************************************************************/
byte command_check0123(byte command, int sequence)
{
int response;
response = sequence * 16 + ((command) - 0x82) / 4;
return (byte)response;
}
/********************************************************************
* command_check4 calculates the checksum for the last command
* which is sent just before data is received from WS2300
*
* Input: number of bytes requested
*
* Returns: expected response from requesting number of bytes
*
********************************************************************/
byte command_check4(int number)
{
int response;
response = 0x30 + number;
return (byte)response;
}
/********************************************************************
* data_checksum calculates the checksum for the data bytes received
* from the WS2300
*
* Input: pointer to array of data to check
* number of bytes in array
*
* Returns: calculated checksum as unsigned char
*
********************************************************************/
byte data_checksum(byte[] data, int number)
{
int checksum = 0;
int i;
for (i = 0; i < number; i++)
{
checksum += data[i];
}
checksum &= 0xFF;
return (byte)checksum;
}
/********************************************************************
* initialize resets WS2300 to cold start (rewind and start over)
*
* Input: device number of the already open serial port
*
* Returns: 0 if fail, 1 if success
*
********************************************************************/
int initialize()
{
byte[] command = { 0x06 };
byte[] answer = { 0xFF };
write_device(command, 1);
if (read_device(answer, 1) != 1)
return 0;
write_device(command, 1);
write_device(command, 1);
if (read_device(answer, 1) != 1)
return 0;
write_device(command, 1);
if (read_device(answer, 1) != 1)
return 0;
write_device(command, 1);
if (read_device(answer, 1) != 1)
return 0;
if (answer[0] != 2)
return 0;
return 1;
}
/********************************************************************
* read_data reads data from the WS2300 based on a given address,
* number of data read, and a an already open serial port
*
* Inputs: serdevice - device number of the already open serial port
* address (interger - 16 bit)
* number - number of bytes to read, max value 15
*
* Output: readdata - pointer to an array of chars containing
* the just read data, not zero terminated
* commanddata - pointer to an array of chars containing
* the commands that were sent to the station
*
* Returns: number of bytes read, -1 if failed
*
********************************************************************/
int read_data(int address, int number,
byte[] readdata, byte[] commanddata)
{
byte[] answer = { 0xFF };
int i;
// First 4 bytes are populated with converted address range 0000-13B0
address_encoder(address, commanddata);
// Last populate the 5th byte with the converted number of bytes
commanddata[4] = numberof_encoder(number);
for (i = 0; i < 4; i++)
{
if (write_device(commanddata[i]) != 1)
return -1;
if (read_device(answer, 1) != 1)
return -1;
if (answer[0] != command_check0123(commanddata[i], i))
return -1;
}
//Send the final command that asks for 'number' of bytes, check answer
if (write_device(commanddata[4]) != 1)
return -1;
if (read_device(answer, 1) != 1)
return -1;
if (answer[0] != command_check4(number))
return -1;
//Read the data bytes
for (i = 0; i < number; i++)
{
if (read_device(ref readdata[i]) != 1)
return -1;
}
//Read and verify checksum
if (read_device(answer, 1) != 1)
return -1;
if (answer[0] != data_checksum(readdata, number))
return -1;
return i;
}
/********************************************************************
* write_data writes data to the WS2300.
* It can both write nibbles and set/unset bits
*
* Inputs: ws2300 - device number of the already open serial port
* address (interger - 16 bit)
* number - number of nibbles to be written/changed
* must 1 for bit modes (SETBIT and UNSETBIT)
* max 80 for nibble mode (WRITENIB)
* encode_constant - unsigned char
* (SETBIT, UNSETBIT or WRITENIB)
* writedata - pointer to an array of chars containing
* data to write, not zero terminated
* data must be in hex - one digit per byte
* If bit mode value must be 0-3 and only
* the first byte can be used.
*
* Output: commanddata - pointer to an array of chars containing
* the commands that were sent to the station
*
* Returns: number of bytes written, -1 if failed
*
********************************************************************/
int write_data(int address, int number,
byte encode_constant, byte[] writedata,
byte[] commanddata)
{
byte[] answer ={ 0xFF };
byte[] encoded_data = new byte[80];
int i = 0;
byte ack_constant = WRITEACK;
if (encode_constant == SETBIT)
{
ack_constant = SETACK;
}
else if (encode_constant == UNSETBIT)
{
ack_constant = UNSETACK;
}
// First 4 bytes are populated with converted address range 0000-13XX
address_encoder(address, commanddata);
// populate the encoded_data array
data_encoder(number, encode_constant, writedata, encoded_data);
//Write the 4 address bytes
for (i = 0; i < 4; i++)
{
if (write_device(commanddata[i]) != 1)
return -1;
if (read_device(answer, 1) != 1)
return -1;
if (answer[0] != command_check0123(commanddata[i], i))
return -1;
}
//Write the data nibbles or set/unset the bits
for (i = 0; i < number; i++)
{
if (write_device(encoded_data[i]) != 1)
return -1;
if (read_device(answer, 1) != 1)
return -1;
if (answer[0] != (writedata[i] + ack_constant))
return -1;
commanddata[i + 4] = encoded_data[i];
}
return i;
}
/********************************************************************
* read_safe Read data, retry until success or maxretries
* Reads data from the WS2300 based on a given address,
* number of data read, and a an already open serial port
* Uses the read_data function and has same interface
*
* Inputs: ws2300 - device number of the already open serial port
* address (interger - 16 bit)
* number - number of bytes to read, max value 15
*
* Output: readdata - pointer to an array of chars containing
* the just read data, not zero terminated
* commanddata - pointer to an array of chars containing
* the commands that were sent to the station
*
* Returns: number of bytes read, -1 if failed
*
********************************************************************/
int read_safe(int address, int number,
byte[] readdata, byte[] commanddata)
{
int j;
for (j = 0; j < MAXRETRIES; j++)
{
reset_06();
// Read the data. If expected number of bytes read break out of loop.
if (read_data(address, number, readdata, commanddata) == number)
{
break;
}
}
// If we have tried MAXRETRIES times to read we expect not to
// have valid data
if (j == MAXRETRIES)
{
return -1;
}
return number;
}
/********************************************************************
* write_safe Write data, retry until success or maxretries
* Writes data to the WS2300 based on a given address,
* number of data to write, and a an already open serial port
* Uses the write_data function and has same interface
*
* Inputs: serdevice - device number of the already open serial port
* address (interger - 16 bit)
* number - number of nibbles to be written/changed
* must 1 for bit modes (SETBIT and UNSETBIT)
* unlimited for nibble mode (WRITENIB)
* encode_constant - unsigned char
* (SETBIT, UNSETBIT or WRITENIB)
* writedata - pointer to an array of chars containing
* data to write, not zero terminated
* data must be in hex - one digit per byte
* If bit mode value must be 0-3 and only
* the first byte can be used.
*
* Output: commanddata - pointer to an array of chars containing
* the commands that were sent to the station
*
* Returns: number of bytes written, -1 if failed
*
********************************************************************/
int write_safe(int address, int number,
byte encode_constant, byte[] writedata,
byte[] commanddata)
{
int j;
for (j = 0; j < MAXRETRIES; j++)
{
//printf("Iteration = " + j.ToString(), true);
reset_06();
// Read the data. If expected number of bytes read break out of loop.
if (write_data(address, number, encode_constant, writedata,
commanddata) == number)
{
break;
}
}
// If we have tried MAXRETRIES times to read we expect not to
// have valid data
if (j == MAXRETRIES)
{
return -1;
}
return number;
}
#endregion
#region rw2300
/********************************************************************/
/* temperature_indoor
* Read indoor temperature, current temperature only
*
* Input: Handle to weatherstation
* temperature_conv flag (integer) controlling
* convertion to deg F
*
* Returns: Temperature (deg C if temperature_conv is 0)
* (deg F if temperature_conv is 1)
*
********************************************************************/
public double temperature_indoor(int temperature_conv)
{
byte[] data = new byte[20];
byte[] command = new byte[25]; //room for write data also
int address = 0x346;
int bytes = 2;
if (read_safe(address, bytes, data, command) != bytes)
read_error_exit();
if (temperature_conv == 1)
return ((((data[1] >> 4) * 10 + (data[1] & 0xF) +
(data[0] >> 4) / 10.0 + (data[0] & 0xF) / 100.0) -
30.0) * 9 / 5 + 32);
else
return ((((data[1] >> 4) * 10 + (data[1] & 0xF) +
(data[0] >> 4) / 10.0 + (data[0] & 0xF) / 100.0) - 30.0));
}
/********************************************************************/
/* temperature_indoor_minmax
* Read indoor min/max temperatures with timestamps
*
* Input: Handle to weatherstation
* temperature_conv flag (integer) controlling
* convertion to deg F
*
* Output: Temperatures temp_min and temp_max
* (deg C if temperature_conv is 0)
* (deg F if temperature_conv is 1)
* Timestamps for temp_min and temp_max in pointers to
* timestamp structures for time_min and time_max
*
********************************************************************/
public void temperature_indoor_minmax(
int temperature_conv,
ref double temp_min,
ref double temp_max,
ref timestamp time_min,
ref timestamp time_max)
{
byte[] data = new byte[20];
byte[] command = new byte[25]; //room for write data also
int address = 0x34B;
int bytes = 15;
if (read_safe(address, bytes, data, command) != bytes)
read_error_exit();
temp_min = ((data[1] >> 4) * 10 + (data[1] & 0xF) + (data[0] >> 4) / 10.0 +
(data[0] & 0xF) / 100.0) - 30.0;
temp_max = ((data[4] & 0xF) * 10 + (data[3] >> 4) + (data[3] & 0xF) / 10.0 +
(data[2] >> 4) / 100.0) - 30.0;
if (temperature_conv == 1)
{
temp_min = temp_min * 9 / 5 + 32;
temp_max = temp_max * 9 / 5 + 32;
}
time_min.minute = ((data[5] & 0xF) * 10) + (data[4] >> 4);
time_min.hour = ((data[6] & 0xF) * 10) + (data[5] >> 4);
time_min.day = ((data[7] & 0xF) * 10) + (data[6] >> 4);
time_min.month = ((data[8] & 0xF) * 10) + (data[7] >> 4);
time_min.year = 2000 + ((data[9] & 0xF) * 10) + (data[8] >> 4);
time_max.minute = ((data[10] & 0xF) * 10) + (data[9] >> 4);
time_max.hour = ((data[11] & 0xF) * 10) + (data[10] >> 4);
time_max.day = ((data[12] & 0xF) * 10) + (data[11] >> 4);
time_max.month = ((data[13] & 0xF) * 10) + (data[12] >> 4);
time_max.year = 2000 + ((data[14] & 0xF) * 10) + (data[13] >> 4);
return;
}
/********************************************************************/
/* temperature_indoor_reset
* Reset indoor min/max temperatures with timestamps
*
* Input: Handle to weatherstation
* minmax - char (8 bit integer) that controls if minimum,
* maximum or both are reset
* Output: None
*
* Returns: 1 if success
*
********************************************************************/
public int temperature_indoor_reset(byte minmax)
{
byte[] data_read = new byte[20];
byte[] data_value = new byte[20];
byte[] data_time = new byte[20];
byte[] command = new byte[25]; //room for write data also
int address;
int number;
// First read current temperature into data_value
address = 0x346;
number = 2;
if (read_safe(address, number, data_read, command) != number)
read_error_exit();
data_value[0] = (byte)(data_read[0] & 0xF);
data_value[1] = (byte)(data_read[0] >> 4);
data_value[2] = (byte)(data_read[1] & 0xF);
data_value[3] = (byte)(data_read[1] >> 4);
// Get current time from station
address = 0x23B;
number = 6;
if (read_safe(address, number, data_read, command) != number)
read_error_exit();
data_time[0] = (byte)(data_read[0] & 0xF);
data_time[1] = (byte)(data_read[0] >> 4);
data_time[2] = (byte)(data_read[1] & 0xF);
data_time[3] = (byte)(data_read[1] >> 4);
data_time[4] = (byte)(data_read[2] >> 4);
data_time[5] = (byte)(data_read[3] & 0xF);
data_time[6] = (byte)(data_read[3] >> 4);
data_time[7] = (byte)(data_read[4] & 0xF);
data_time[8] = (byte)(data_read[4] >> 4);
data_time[9] = (byte)(data_read[5] & 0xF);
if ((minmax & RESET_MIN) == RESET_MIN) // minimum
{
// Set min value to current value
address = 0x34B;
number = 4;
if (write_safe(address, number, WRITENIB, data_value, command) != number)
write_error_exit();
// Set min value timestamp to current time
address = 0x354;
number = 10;
if (write_safe(address, number, WRITENIB, data_time, command) != number)
write_error_exit();
}
if ((minmax & RESET_MAX) == RESET_MAX)// maximum
{
// Set max value to current value
address = 0x350;
number = 4;
if (write_safe(address, number, WRITENIB, data_value, command) != number)
write_error_exit();
// Set max value timestamp to current time
address = 0x35E;
number = 10;
if (write_safe(address, number, WRITENIB, data_time, command) != number)
write_error_exit();
}
return 1;
}
/********************************************************************/
/* temperature_outdoor
* Read indoor temperature, current temperature only
*
* Input: Handle to weatherstation
* temperature_conv flag (integer) controlling
* convertion to deg F
*
* Returns: Temperature (deg C if temperature_conv is 0)
* (deg F if temperature_conv is 1)
*
********************************************************************/
public double temperature_outdoor(int temperature_conv)
{
byte[] data = new byte[20];
byte[] command = new byte[25]; //room for write data also
int address = 0x373;
int bytes = 2;
if (read_safe(address, bytes, data, command) != bytes)
read_error_exit();
if (temperature_conv == 1)
return ((((data[1] >> 4) * 10 + (data[1] & 0xF) +
(data[0] >> 4) / 10.0 + (data[0] & 0xF) / 100.0) -
30.0) * 9 / 5 + 32);
else
return ((((data[1] >> 4) * 10 + (data[1] & 0xF) +
(data[0] >> 4) / 10.0 + (data[0] & 0xF) / 100.0) - 30.0));
}
/********************************************************************
* temperature_outdoor_minmax
* Read outdoor min/max temperatures with timestamps
*
* Input: Handle to weatherstation
* temperature_conv flag (integer) controlling
* convertion to deg F
*
* Output: Temperatures temp_min and temp_max
* (deg C if temperature_conv is 0)
* (deg F if temperature_conv is 1)
* Timestamps for temp_min and temp_max in pointers to
* timestamp structures for time_min and time_max
*
********************************************************************/
public void temperature_outdoor_minmax(
int temperature_conv,
ref double temp_min,
ref double temp_max,
ref timestamp time_min,
ref timestamp time_max)
{
byte[] data = new byte[20];
byte[] command = new byte[25]; //room for write data also
int address = 0x378;
int bytes = 15;
if (read_safe(address, bytes, data, command) != bytes)
read_error_exit();
temp_min = ((data[1] >> 4) * 10 + (data[1] & 0xF) + (data[0] >> 4) / 10.0 +
(data[0] & 0xF) / 100.0) - 30.0;
temp_max = ((data[4] & 0xF) * 10 + (data[3] >> 4) + (data[3] & 0xF) / 10.0 +
(data[2] >> 4) / 100.0) - 30.0;
if (temperature_conv == 1)
{
temp_min = temp_min * 9 / 5 + 32;
temp_max = temp_max * 9 / 5 + 32;
}
time_min.minute = ((data[5] & 0xF) * 10) + (data[4] >> 4);
time_min.hour = ((data[6] & 0xF) * 10) + (data[5] >> 4);
time_min.day = ((data[7] & 0xF) * 10) + (data[6] >> 4);
time_min.month = ((data[8] & 0xF) * 10) + (data[7] >> 4);
time_min.year = 2000 + ((data[9] & 0xF) * 10) + (data[8] >> 4);
time_max.minute = ((data[10] & 0xF) * 10) + (data[9] >> 4);
time_max.hour = ((data[11] & 0xF) * 10) + (data[10] >> 4);
time_max.day = ((data[12] & 0xF) * 10) + (data[11] >> 4);
time_max.month = ((data[13] & 0xF) * 10) + (data[12] >> 4);
time_max.year = 2000 + ((data[14] & 0xF) * 10) + (data[13] >> 4);
return;
}
/********************************************************************/
/* temperature_outdoor_reset
* Reset outdoor min/max temperatures with timestamps
*
* Input: Handle to weatherstation
* minmax - char (8 bit integer) that controls if minimum,
* maximum or both are reset
* Output: None
*
* Returns: 1 if success
*
********************************************************************/
public int temperature_outdoor_reset(byte minmax)
{
byte[] data_read = new byte[20];
byte[] data_value = new byte[20];
byte[] data_time = new byte[20];
byte[] command = new byte[25]; //room for write data also
int address;
int number;
// First read current temperature into data_value
address = 0x373;
number = 2;
if (read_safe(address, number, data_read, command) != number)
read_error_exit();
data_value[0] = (byte)(data_read[0] & 0xF);
data_value[1] = (byte)(data_read[0] >> 4);
data_value[2] = (byte)(data_read[1] & 0xF);
data_value[3] = (byte)(data_read[1] >> 4);
// Get current time from station
address = 0x23B;
number = 6;
if (read_safe(address, number, data_read, command) != number)
read_error_exit();
data_time[0] = (byte)(data_read[0] & 0xF);
data_time[1] = (byte)(data_read[0] >> 4);
data_time[2] = (byte)(data_read[1] & 0xF);
data_time[3] = (byte)(data_read[1] >> 4);
data_time[4] = (byte)(data_read[2] >> 4);
data_time[5] = (byte)(data_read[3] & 0xF);
data_time[6] = (byte)(data_read[3] >> 4);
data_time[7] = (byte)(data_read[4] & 0xF);
data_time[8] = (byte)(data_read[4] >> 4);
data_time[9] = (byte)(data_read[5] & 0xF);
if ((minmax & RESET_MIN) == RESET_MIN)// minimum
{
// Set min value to current value
address = 0x378;
number = 4;
if (write_safe(address, number, WRITENIB, data_value, command) != number)
write_error_exit();
// Set min value timestamp to current time
address = 0x381;
number = 10;
if (write_safe(address, number, WRITENIB, data_time, command) != number)
write_error_exit();
}
if ((minmax & RESET_MAX) == RESET_MAX) // maximum
{
// Set max value to current value
address = 0x37D;
number = 4;
if (write_safe(address, number, WRITENIB, data_value, command) != number)
write_error_exit();
// Set max value timestamp to current time
address = 0x38B;