1 /***************************************************************************
2  *
3  * Project:  OpenCPN
4  * Purpose:  NMEA0183 Support Classes
5  * Author:   Thomas Rauch
6  *
7  ***************************************************************************
8  *   Copyright (C) 2010 by Samuel R. Blackburn, David S Register           *
9  *                                                                         *
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; either version 2 of the License, or     *
13  *   (at your option) any later version.                                   *
14  *                                                                         *
15  *   This program is distributed in the hope that it will be useful,       *
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  *   GNU General Public License for more details.                          *
19  *                                                                         *
20  *   You should have received a copy of the GNU General Public License     *
21  *   along with this program; if not, write to the                         *
22  *   Free Software Foundation, Inc.,                                       *
23  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,  USA.             *
24  ***************************************************************************
25  *
26  *   S Blackburn's original source license:                                *
27  *         "You can use it any way you like."                              *
28  *   More recent (2010) license statement:                                 *
29  *         "It is BSD license, do with it what you will"                   *
30  */
31 
32 #include "nmea0183.h"
33 
34 //IMPLEMENT_DYNAMIC( XDR, RESPONSE )
35 
XDR()36 XDR::XDR()
37 {
38    Mnemonic = _T("XDR");
39    Empty();
40 }
41 
~XDR()42 XDR::~XDR()
43 {
44    Mnemonic.Empty();
45    Empty();
46 }
47 
Empty(void)48 void XDR::Empty( void )
49 {
50 //   ASSERT_VALID( this );
51      TransducerCnt=0;
52      for (int idx = 0; idx < MaxTransducerCnt; idx++) {
53          TransducerInfo[idx].TransducerType.Empty();
54          TransducerInfo[idx].MeasurementData = 0.0;
55          TransducerInfo[idx].UnitOfMeasurement.Empty();
56          TransducerInfo[idx].TransducerName.Empty();
57      }
58 }
59 
Parse(const SENTENCE & sentence)60 bool XDR::Parse( const SENTENCE& sentence )
61 {
62 //   ASSERT_VALID( this );
63 
64    /*
65    ** XDR - Transducer Measurement
66    **
67    **        1 2   3 4            n
68    **        | |   | |            |
69    ** $--XDR,a,x.x,a,c--c, ..... *hh<CR><LF>
70    **
71    ** Field Number:
72    **  1) Transducer Type
73    **  2) Measurement Data
74    **  3) Unit of Measurement, Celcius
75    **  4) Name of transducer
76    **  ...
77    **  n) Checksum
78    ** There may be any number of quadruplets like this, each describing a sensor. The last field will be a checksum as usual.
79    */
80 
81    /*
82    ** First we check the checksum...
83    */
84    int cksumFieldNr = 0;
85    TransducerCnt = 0;
86    TransducerCnt=(int)sentence.GetNumberOfDataFields()/4;
87    cksumFieldNr=sentence.GetNumberOfDataFields()+1;
88    if (TransducerCnt == 0 || TransducerCnt > MaxTransducerCnt) {
89       SetErrorMessage( _T("Invalid Field count" ));
90       return( FALSE );
91    }
92 
93    if ( sentence.IsChecksumBad( cksumFieldNr ) == NTrue ) {
94       SetErrorMessage( _T("Invalid Checksum" ));
95       return( FALSE );
96    }
97     for (int idx = 0; idx < TransducerCnt; idx++)
98    {
99          TransducerInfo[idx].TransducerType = sentence.Field( idx*4+1 );
100          TransducerInfo[idx].MeasurementData = sentence.Double( idx*4+2 );
101          TransducerInfo[idx].UnitOfMeasurement = sentence.Field( idx*4+3 );
102          TransducerInfo[idx].TransducerName = sentence.Field( idx*4+4 );
103    }
104 
105    return( TRUE );
106 }
107 
Write(SENTENCE & sentence)108 bool XDR::Write( SENTENCE& sentence )
109 {
110 //   ASSERT_VALID( this );
111 
112    /*
113    ** Let the parent do its thing
114    */
115 
116    RESPONSE::Write( sentence );
117 
118    sentence += TransducerCnt;
119    for (int idx = 0; idx < TransducerCnt; idx++) {
120          sentence += TransducerInfo[idx].TransducerType;
121          sentence += TransducerInfo[idx].MeasurementData;
122          sentence += TransducerInfo[idx].UnitOfMeasurement;
123          sentence += TransducerInfo[idx].TransducerName;
124          //sentence.Finish();
125    }
126 
127    sentence.Finish();
128 
129    return( TRUE );
130 }
131 
operator =(const XDR & source)132 const XDR& XDR::operator = ( const XDR& source )
133 {
134 //   ASSERT_VALID( this );
135 
136   TransducerCnt       = source.TransducerCnt;
137    for (int idx = 0; idx < TransducerCnt; idx++) {
138          TransducerInfo[idx].TransducerType = source.TransducerInfo[idx].TransducerType;
139          TransducerInfo[idx].MeasurementData = source.TransducerInfo[idx].MeasurementData;
140          TransducerInfo[idx].UnitOfMeasurement = source.TransducerInfo[idx].UnitOfMeasurement;
141          TransducerInfo[idx].TransducerName = source.TransducerInfo[idx].TransducerName;
142    }
143 
144 
145   return( *this );
146 }
147