Home > mrg > MRG_metstation > mrg_met_control.m

mrg_met_control

PURPOSE ^

The core Metstation control file

SYNOPSIS ^

function mrg_met_control(verbosity)

DESCRIPTION ^

 The core Metstation control file

 This function opens a serial connection and generates an infinite loop
 that reads incomming data and processes it as required.

 INPUT
   verbosity   An integer specifying the level of output to the console.
               This only effects output to screen and has no effect on 
               the logging to file.
               0 = No output
               1 = Minimal output (The default)
               2 = Outputs every time a string is read
               3 = Also reports the size of data in the buffer

 OUTPUT
   This function reports information to the console and to a file named:
       met_log_yyyy_mmm.txt
   Where yyyy and mmm are the year and abbreviated month name, repectivly.
   Other heavy lifting is done by mrg_met_output and further details can
   be found there.

 REQUIREMENTS
   Requires mrg_met_test_string and mrg_met_output

 AUTHORS
   Bjoern Elsaesser
   Daniel Pritchard

 LICENCE
   Code distributed as part of the MRG toolbox from the Marine Research
   Group at Queens Univeristy Belfast (QUB) School of Planning
   Architecture and Civil Engineering (SPACE). Distributed under a
   creative commons CC BY-SA licence, retaining full copyright of the
   original authors.

   http://creativecommons.org/licenses/by-sa/3.0/
   http://www.qub.ac.uk/space/
   http://www.qub.ac.uk/research-centres/eerc/

 DEVELOPMENT
   v 1.0   2010
           First version. BE.
   v 2.1   24/01/2012 - DP
           Removed useless RS232 reset capabilities.
   v 3.0   August 2013. DP
           Major re-write.
           Clean up and document. Move into MRG toolbox.

% Open serial connection

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function mrg_met_control(verbosity)
0002 % The core Metstation control file
0003 %
0004 % This function opens a serial connection and generates an infinite loop
0005 % that reads incomming data and processes it as required.
0006 %
0007 % INPUT
0008 %   verbosity   An integer specifying the level of output to the console.
0009 %               This only effects output to screen and has no effect on
0010 %               the logging to file.
0011 %               0 = No output
0012 %               1 = Minimal output (The default)
0013 %               2 = Outputs every time a string is read
0014 %               3 = Also reports the size of data in the buffer
0015 %
0016 % OUTPUT
0017 %   This function reports information to the console and to a file named:
0018 %       met_log_yyyy_mmm.txt
0019 %   Where yyyy and mmm are the year and abbreviated month name, repectivly.
0020 %   Other heavy lifting is done by mrg_met_output and further details can
0021 %   be found there.
0022 %
0023 % REQUIREMENTS
0024 %   Requires mrg_met_test_string and mrg_met_output
0025 %
0026 % AUTHORS
0027 %   Bjoern Elsaesser
0028 %   Daniel Pritchard
0029 %
0030 % LICENCE
0031 %   Code distributed as part of the MRG toolbox from the Marine Research
0032 %   Group at Queens Univeristy Belfast (QUB) School of Planning
0033 %   Architecture and Civil Engineering (SPACE). Distributed under a
0034 %   creative commons CC BY-SA licence, retaining full copyright of the
0035 %   original authors.
0036 %
0037 %   http://creativecommons.org/licenses/by-sa/3.0/
0038 %   http://www.qub.ac.uk/space/
0039 %   http://www.qub.ac.uk/research-centres/eerc/
0040 %
0041 % DEVELOPMENT
0042 %   v 1.0   2010
0043 %           First version. BE.
0044 %   v 2.1   24/01/2012 - DP
0045 %           Removed useless RS232 reset capabilities.
0046 %   v 3.0   August 2013. DP
0047 %           Major re-write.
0048 %           Clean up and document. Move into MRG toolbox.
0049 %
0050 %% Open serial connection
0051 s = serial('/dev/ttyS0');
0052 set(s,'BaudRate',9600);
0053 fopen(s);
0054 
0055 disp('Serial port connected. Starting mrg_met_control...')
0056 
0057 %% Set default verbosity (1)
0058 if nargin < 1
0059     verbosity = 1;
0060 end
0061 % Setting verbosity to 0 silences all output to the screen (but log files
0062 % are still generated)
0063 
0064 %% The loop of doom...
0065 while 1
0066     ser_out = fgetl(s);
0067     out = mrg_met_test_string(ser_out);
0068 
0069     if out ~= 0;
0070         % The length of the data suggest we have something to process
0071         try
0072             msg = mrg_met_output(out); 
0073         catch err
0074             msg = [datestr(now,'HH:MM:SS dd/mm/yyyy'), ': FAIL. Failure to convert, save, or upload data. The error was:\n', err];
0075         end
0076         % Regardless of the outcome, write the msg to file
0077         fileID2 = fopen(['met_log_',datestr(date,'yyyy_mmm'),'.txt'],'a');
0078         fprintf(fileID2,[msg,'\r\n']);
0079         fclose(fileID2);
0080         if verbosity > 0
0081             fprintf([msg,'\n\nNB: If you are reading this, do not touch this computer!\n\n']);
0082         end
0083         % Create post-processed figures
0084         mrg_met_pp
0085         % End post-processing
0086     elseif length(ser_out) < 23
0087         % ser_out is not long enough to be real data, so we have the option
0088         % to get chatty to the user, if set in the call to
0089         % mrg_met_control.m
0090         if verbosity > 1 
0091             msg = ['Logging Weather Station @ ', ser_out];
0092             fprintf(msg);
0093         end
0094         if verbosity > 2
0095             msg = ['Bytes in Buffer : ', num2str(s.BytesAvailable), '\n'];
0096             fprintf(msg);
0097         end
0098     end
0099     
0100     % If we get to this stage and there are few than 23 bytes to read, we
0101     % should wait for 10 seconds...
0102     if s.BytesAvailable < 23 
0103         pause(10)
0104     end
0105 end
0106 end

Generated on Thu 29-May-2014 21:29:53 by m2html © 2005