-
Notifications
You must be signed in to change notification settings - Fork 48
/
NMEA0183.cpp
206 lines (168 loc) · 6.06 KB
/
NMEA0183.cpp
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
/*
NMEA0183.cpp
Copyright (c) 2015-2024 Timo Lappalainen, Kave Oy, www.kave.fi
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef ARDUINO
#include <cstdio>
#endif
#include "NMEA0183.h"
//*****************************************************************************
tNMEA0183::tNMEA0183(tNMEA0183Stream *stream, uint8_t _SourceID)
: port(0), MsgCheckSumStartPos(SIZE_MAX),
MsgInPos(0), MsgInStarted(false),
MsgOutWritePos(0), MsgOutReadPos(0), MsgOutBuf(0), MsgOutBufSize(3*MAX_NMEA0183_MSG_BUF_LEN),
MsgHandler(0)
{
SetMessageStream(stream,_SourceID);
}
//*****************************************************************************
void tNMEA0183::SetMessageStream(tNMEA0183Stream *stream, uint8_t _SourceID) {
SourceID=_SourceID;
port=stream;
}
//*****************************************************************************
bool tNMEA0183::Open() {
if ( !IsOpen() ) {
if ( MsgOutBuf==0 ) MsgOutBuf=new char[MsgOutBufSize];
MsgInPos=0; MsgInStarted=false;
MsgOutWritePos=0; MsgOutReadPos=0;
return IsOpen();
}
return true;
}
#ifdef ARDUINO
//*****************************************************************************
void tNMEA0183::Begin(HardwareSerial *_port, uint8_t _SourceID, unsigned long _baud) {
_port->begin(_baud);
SetMessageStream(_port,_SourceID);
Open();
}
#endif
//*****************************************************************************
void tNMEA0183::SetSendBufferSize(size_t size) {
if ( MsgOutBuf==0 ) {
MsgOutBufSize=size;
}
}
//*****************************************************************************
void tNMEA0183::ParseMessages() {
tNMEA0183Msg NMEA0183Msg;
if ( !Open() ) return;
while (GetMessage(NMEA0183Msg)) {
if (MsgHandler!=0) MsgHandler(NMEA0183Msg);
}
kick();
}
//*****************************************************************************
bool tNMEA0183::GetMessage(tNMEA0183Msg &NMEA0183Msg) {
if ( !IsOpen() ) return false;
bool result=false;
while (port->available() > 0 && !result) {
int NewByte=port->read();
if (NewByte=='$' || NewByte=='!') { // Message start
MsgInStarted=true;
MsgInPos=0;
MsgInBuf[MsgInPos]=NewByte;
MsgInPos++;
} else if (MsgInStarted) {
MsgInBuf[MsgInPos]=NewByte;
if (NewByte=='*') MsgCheckSumStartPos=MsgInPos;
MsgInPos++;
if (MsgCheckSumStartPos!=SIZE_MAX and MsgCheckSumStartPos+3==MsgInPos) { // We have full checksum and so full message
MsgInBuf[MsgInPos]=0; // add null termination
if (NMEA0183Msg.SetMessage(MsgInBuf)) {
NMEA0183Msg.SourceID=SourceID;
result=true;
}
MsgInStarted=false;
MsgInPos=0;
MsgCheckSumStartPos=SIZE_MAX;
}
if (MsgInPos>=MAX_NMEA0183_MSG_BUF_LEN) { // Too may chars in message. Start from beginning
MsgInStarted=false;
MsgInPos=0;
MsgCheckSumStartPos=SIZE_MAX;
}
}
}
return result;
}
//*****************************************************************************
bool tNMEA0183::SendMessage(const tNMEA0183Msg &NMEA0183Msg) {
if ( !Open() ) return false;
char buf[7]={NMEA0183Msg.GetPrefix(),0};
SendBuf(buf);
SendBuf(NMEA0183Msg.Sender());
SendBuf(NMEA0183Msg.MessageCode());
for (int i=0; i<NMEA0183Msg.FieldCount(); i++) {
SendBuf(",");
SendBuf(NMEA0183Msg.Field(i));
}
sprintf(buf,"*%02X\r\n",NMEA0183Msg.GetCheckSum());
return SendBuf(buf);
}
//*****************************************************************************
// availableForWrite does not exists on all implementations.
bool tNMEA0183::CanSendByte() {
#if defined(ARDUINO_ARCH_ESP32)
return true;
#else
return port->availableForWrite() > 0;
#endif
}
//*****************************************************************************
void tNMEA0183::kick() {
if ( !Open() ) return;
while ( MsgOutWritePos!=MsgOutReadPos && CanSendByte() ) {
port->write(MsgOutBuf[MsgOutReadPos]);
MsgOutReadPos=(MsgOutReadPos + 1) % MsgOutBufSize;
}
}
//*****************************************************************************
bool tNMEA0183::SendBuf(const char *buf) {
kick();
if ( buf==0 ) return true;
size_t iBuf=0;
if ( MsgOutWritePos==MsgOutReadPos ) { // try to send immediately
for (; CanSendByte() > 0 && buf[iBuf]!=0; iBuf++ ) {
port->write(buf[iBuf]);
}
}
if ( buf[iBuf]==0 ) {
return true;
}
// Could not send immediately, so buffer message
if ( strlen(buf)-iBuf >= MsgOutBufFreeSize() ) return false; // No room for message
size_t wp=MsgOutWritePos;
for (size_t temp = (MsgOutWritePos + 1) % MsgOutBufSize;
buf[iBuf]!=0 && temp!=MsgOutReadPos;
temp = (MsgOutWritePos + 1) % MsgOutBufSize, iBuf++ ) {
MsgOutBuf[MsgOutWritePos]=buf[iBuf];
MsgOutWritePos=temp;
}
if ( buf[iBuf]!=0 ) {
MsgOutWritePos=wp; // Cancel sending
return false;
}
return true;
}
//*****************************************************************************
bool tNMEA0183::SendMessage(const char *buf) {
if ( !Open() ) return false;
// Add check that there is crlf at end.
return SendBuf(buf);
}