-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringSerial.cpp
182 lines (139 loc) · 4.41 KB
/
StringSerial.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
/*
** STRING-SERIAL **
** class for Arduino **
Copyright (C) 2012 TONG Haowen Joel
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.
@Name : TONG Haowen Joel
@Organization : Singapore Academy for Young Engineers and Scientists (SAYES), Science Centre Singapore
@Email : code {at} joeltong {dot} org
@Language : Arduino-C
@Descr : This code simplies Serial communications using the Arduino using the Xbee radio. Strings are C++ - style strings, and are terminated by a terminator character (default: '%').
Works well with either SoftwareSerial class (Arduino IDE ver 1.0 above) or/and hardware Serial UART.
To use, place StringSerial.cpp and StringSerial.h in the directory "<PATH-TO-ARDUINO-IDE-LIBRARY>/StringSerial/".
*/
/* StringSerial.cpp */
#include "Arduino.h"
#include "StringSerial.h"
#include "../SoftwareSerial/SoftwareSerial.h"
StringSerial::StringSerial(unsigned int baud, unsigned long timeOut) {
_useSoftSerial = false;
_delimiter = ' ';
_eol = '%';
_baud = baud;
_timeOut = timeOut;
}
StringSerial::StringSerial(unsigned int baud, unsigned long timeOut, SoftwareSerial& softSerial) {
_useSoftSerial = true;
_delimiter = ' ';
_eol = '%';
_baud = baud;
_timeOut = timeOut;
_softSerial = &softSerial;
}
StringSerial::~StringSerial() {}
boolean StringSerial::init() {
if (_useSoftSerial) {
_softSerial->begin(_baud);
return true;
} else {
Serial.begin(_baud);
return true;
}
return false;
}
boolean StringSerial::setDelimiter(char val) {
_delimiter = val;
return true;
}
boolean StringSerial::setEOL(char val) {
_eol = val;
return true;
}
boolean StringSerial::send(String val) {
if (_useSoftSerial) {
for (int i = 0; i < val.length(); i++) {
_softSerial->print(val[i]);
}
_softSerial->print(_eol);
_softSerial->flush();
} else {
for (int i = 0; i < val.length(); i++) {
Serial.print(val[i]);
}
Serial.print(_eol);
Serial.flush();
}
}
String StringSerial::receive() {
String val = "";
boolean isTransmitting = false;
unsigned long startTime = 0;
unsigned long diff = 0;
if (_useSoftSerial) { //using software UART
if (_softSerial->available()) {
isTransmitting = true;
startTime = millis();
}
while(isTransmitting) {
diff = millis() - startTime;
if (diff > _timeOut) //timeout
return "TIMEOUT";
while(_softSerial->available()) {
char getData = _softSerial->read();
val += getData;
if (val.endsWith((String) _eol))
return val;
}
}
return "NULL";
} else { //hardware UART
if (Serial.available()) {
isTransmitting = true;
startTime = millis();
}
while(isTransmitting) {
diff = millis() - startTime;
if (diff > _timeOut) //timeout
return "TIMEOUT";
while(Serial.available()) {
char getData = Serial.read();
val += getData;
if (val.endsWith((String) _eol))
return val;
}
}
return "NULL";
}
}
boolean StringSerial::parse(String val) {
int counter = 0;
_header = "";
_payload = "";
if (val == "NULL" || val == "TIMEOUT") {
_header = "";
_payload = "";
return false;
}
for (int i = 0; i < val.length(); i++) {
if (counter == 1) {
if (val[i] == '%') {
_payload.trim();
return true;
} else
_payload += val[i];
} else if (val[i] == ' ') {
counter++;
} else {
_header += val[i];
}
}
return false;
}
String StringSerial::header() {
return _header;
}
String StringSerial::payload() {
return _payload;
}