Skip to content

Commit f444682

Browse files
committed
Improved data receive
Former-commit-id: 3a98cf8
1 parent d478399 commit f444682

File tree

4 files changed

+197
-68
lines changed

4 files changed

+197
-68
lines changed

libraries/WiFiS3/src/Modem.cpp

Lines changed: 125 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,44 @@
44
#define RESULT_ERROR "ERROR\r\n"
55
#define RESULT_DATA "DATA\r\n"
66

7-
ModemClass::ModemClass(int tx, int rx) : beginned(false), delete_serial(false), _timeout(MODEM_TIMEOUT) {
7+
/* -------------------------------------------------------------------------- */
8+
ModemClass::ModemClass(int tx, int rx) : beginned(false), delete_serial(false), _timeout(MODEM_TIMEOUT), trim_results(true), read_by_size(false) {
9+
/* -------------------------------------------------------------------------- */
810
_serial = new UART(tx,rx);
911
}
1012

11-
ModemClass::ModemClass(UART * serial) : beginned(false) , delete_serial(true) , _serial(serial), _timeout(MODEM_TIMEOUT){
12-
13+
/* -------------------------------------------------------------------------- */
14+
ModemClass::ModemClass(UART * serial) : beginned(false) , delete_serial(true) , _serial(serial), _timeout(MODEM_TIMEOUT), trim_results(true), read_by_size(false) {
15+
/* -------------------------------------------------------------------------- */
1316
}
1417

18+
/* -------------------------------------------------------------------------- */
1519
ModemClass::~ModemClass() {
20+
/* -------------------------------------------------------------------------- */
1621
if(_serial != nullptr && !delete_serial){
1722
delete _serial;
1823
_serial = nullptr;
1924
}
2025
}
2126

27+
/* -------------------------------------------------------------------------- */
2228
void ModemClass::begin(int badurate){
29+
/* -------------------------------------------------------------------------- */
2330
if(_serial != nullptr && !beginned) {
2431
_serial->begin(badurate);
2532
beginned = true;
2633
}
2734
}
2835

36+
/* -------------------------------------------------------------------------- */
2937
void ModemClass::end(){
38+
/* -------------------------------------------------------------------------- */
3039
_serial->end();
3140
}
3241

33-
42+
/* -------------------------------------------------------------------------- */
3443
bool ModemClass::passthrough(const uint8_t *data, size_t size) {
35-
Serial.println("[CALL] ModemClass::passthrough");
44+
/* -------------------------------------------------------------------------- */
3645
_serial->write(data,size);
3746
bool res = false;
3847
bool found = false;
@@ -53,7 +62,7 @@ bool ModemClass::passthrough(const uint8_t *data, size_t size) {
5362
}
5463
}
5564
}
56-
#ifdef MODEM_DEBUG
65+
#ifdef MODEM_DEBUG_PASSTHROUGH
5766
Serial.print(" passthrough, rx |>>");
5867
Serial.print(data_res.c_str());
5968
Serial.println("<<|");
@@ -67,8 +76,9 @@ bool ModemClass::passthrough(const uint8_t *data, size_t size) {
6776
return res;
6877
}
6978

79+
/* -------------------------------------------------------------------------- */
7080
void ModemClass::write_nowait(const string &cmd, string &str, char * fmt, ...) {
71-
81+
/* -------------------------------------------------------------------------- */
7282
memset(tx_buff,0x00,MAX_BUFF_SIZE);
7383
va_list va;
7484
va_start (va, fmt);
@@ -84,69 +94,146 @@ void ModemClass::write_nowait(const string &cmd, string &str, char * fmt, ...) {
8494
}
8595

8696

97+
/* -------------------------------------------------------------------------- */
8798
bool ModemClass::write(const string &prompt, string &data_res, char * fmt, ...){
88-
99+
/* -------------------------------------------------------------------------- */
89100
data_res.clear();
90-
91101
memset(tx_buff,0x00,MAX_BUFF_SIZE);
92102
va_list va;
93103
va_start (va, fmt);
94104
vsprintf ((char *)tx_buff, fmt, va);
95105
va_end (va);
96106
#ifdef MODEM_DEBUG
107+
Serial.println();
108+
Serial.println("###>");
109+
Serial.print("READ BY SIZE: ");
110+
Serial.println((int)read_by_size);
97111
Serial.print(" Write Call, command sent: ");
98112
Serial.write(tx_buff,strlen((char *)tx_buff));
99113
Serial.println();
114+
115+
Serial.println("<###");
100116
#endif
101117
_serial->write(tx_buff,strlen((char *)tx_buff));
102118
return buf_read(prompt,data_res);;
103119
}
104120

105121

122+
typedef enum {
123+
IDLE,
124+
WAIT_FOR_SIZE,
125+
WAIT_FOR_DATA
126+
} ReadBySizeSt_t;
127+
128+
129+
/* -------------------------------------------------------------------------- */
130+
bool ModemClass::read_by_size_finished(string &rx) {
131+
/* -------------------------------------------------------------------------- */
132+
bool rv = false;
133+
static bool first_call = true;
134+
static ReadBySizeSt_t st = IDLE;
135+
static int data_to_be_received = 0;
136+
static int data_received = 0;
137+
if(first_call) {
138+
first_call = false;
139+
st = WAIT_FOR_SIZE;
140+
}
141+
142+
switch(st) {
143+
case IDLE:
144+
145+
break;
146+
case WAIT_FOR_SIZE: {
147+
int pos = rx.find("|");
148+
int pos_space = rx.find(" ");
149+
if(pos != string::npos && pos_space != string::npos) {
150+
string n = rx.substr(pos_space,pos);
151+
data_to_be_received = atoi(n.c_str());
152+
rx.clear();
153+
data_received = 0;
154+
st = WAIT_FOR_DATA;
155+
156+
}
157+
}
158+
break;
159+
160+
case WAIT_FOR_DATA:
161+
data_received++;
162+
if(data_received == data_to_be_received) {
163+
rv = true;
164+
first_call = true;
165+
st = IDLE;
166+
}
167+
break;
168+
169+
default:
170+
st = IDLE;
171+
break;
172+
}
173+
return rv;
174+
}
175+
176+
/* -------------------------------------------------------------------------- */
106177
bool ModemClass::buf_read(const string &prompt, string &data_res) {
178+
/* -------------------------------------------------------------------------- */
107179
bool res = false;
108180
bool found = false;
109181
unsigned long start_time = millis();
110182
while(millis() - start_time < _timeout && !found){
111183
while(_serial->available()){
112184
char c = _serial->read();
113185
data_res += c;
114-
if(string::npos != data_res.rfind(RESULT_DATA)) {
115-
found = true;
116-
data_res = data_res.substr(0, data_res.length() - sizeof(RESULT_DATA));
117-
if(prompt != DO_NOT_CHECK_CMD) {
118-
if(removeAtBegin(data_res, prompt)) {
119-
Serial.println("DATA RECIVED");
120-
res = true;
186+
if(read_by_size) {
187+
if(read_by_size_finished(data_res)) {
188+
found = true;
189+
read_by_size = false;
190+
res = true;
191+
while(_serial->available()){
192+
_serial->read();
121193
}
122194
}
123-
else {
124-
res = true;
125-
}
126-
break;
127195
}
128-
else if(string::npos != data_res.rfind(RESULT_OK)){
129-
found = true;
130-
data_res = data_res.substr(0, data_res.length() - sizeof(RESULT_OK));
131-
if(prompt != DO_NOT_CHECK_CMD) {
132-
if(removeAtBegin(data_res, prompt)) {
133-
res = true;
196+
else {
197+
if(string::npos != data_res.rfind(RESULT_DATA)) {
198+
found = true;
199+
data_res = data_res.substr(0, data_res.length() - sizeof(RESULT_DATA));
200+
if(prompt != DO_NOT_CHECK_CMD) {
201+
if(removeAtBegin(data_res, prompt)) {
202+
res = true;
203+
}
204+
}
205+
else {
206+
res = true;
134207
}
208+
break;
135209
}
136-
else {
137-
res = true;
210+
else if(string::npos != data_res.rfind(RESULT_OK)){
211+
found = true;
212+
data_res = data_res.substr(0, data_res.length() - sizeof(RESULT_OK));
213+
if(prompt != DO_NOT_CHECK_CMD) {
214+
if(removeAtBegin(data_res, prompt)) {
215+
res = true;
216+
}
217+
}
218+
else {
219+
res = true;
220+
}
221+
break;
222+
}
223+
else if (string::npos != data_res.rfind(RESULT_ERROR)) {
224+
data_res.substr(0, data_res.length() - sizeof(RESULT_ERROR));
225+
res = false;
226+
break;
138227
}
139-
break;
140-
}
141-
else if (string::npos != data_res.rfind(RESULT_ERROR)) {
142-
data_res.substr(0, data_res.length() - sizeof(RESULT_ERROR));
143-
res = false;
144-
break;
145228
}
146229
}
147-
}
148-
trim(data_res);
149-
#ifdef MODEM_DEBUG
230+
}
231+
if(trim_results) {
232+
trim(data_res);
233+
}
234+
trim_results = true;
235+
236+
#ifdef MODEM_DEBUG
150237
Serial.print(" Write Call, response rx |>>");
151238
Serial.print(data_res.c_str());
152239
Serial.println("<<|");
@@ -156,7 +243,7 @@ bool ModemClass::buf_read(const string &prompt, string &data_res) {
156243
else {
157244
Serial.println(" Result: FAILED");
158245
}
159-
#endif
246+
#endif
160247

161248
return res;
162249
}

libraries/WiFiS3/src/Modem.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#ifndef _MODEM_WIFIS3_H_
22
#define _MODEM_WIFIS3_H_
33

4-
5-
64
#include "Arduino.h"
7-
85
#include "StringHelpers.h"
96

10-
#define MODEM_DEBUG
7+
8+
//#define MODEM_DEBUG
119

1210
#define MODEM_TIMEOUT 10000
1311
#define MAX_BUFF_SIZE 64
@@ -29,7 +27,15 @@ class ModemClass {
2927
void write_nowait(const string &cmd, string &str, char * fmt, ...);
3028

3129
bool passthrough(const uint8_t *data, size_t size);
32-
30+
void avoid_trim_results() {
31+
/* one shot - it works only 1 time the it is necessary to call again this
32+
funtion */
33+
trim_results = false;
34+
}
35+
36+
void read_using_size() {
37+
read_by_size = true;
38+
}
3339
bool beginned;
3440

3541
private:
@@ -38,6 +44,9 @@ class ModemClass {
3844
UART * _serial;
3945
unsigned long _timeout;
4046
uint8_t tx_buff[MAX_BUFF_SIZE];
47+
bool trim_results;
48+
bool read_by_size;
49+
bool read_by_size_finished(string &rx);
4150
};
4251

4352
extern ModemClass modem;

0 commit comments

Comments
 (0)