@@ -69,18 +69,19 @@ bool ModemClass::passthrough(const uint8_t *data, size_t size) {
6969 break ;
7070 }
7171 }
72- }
73- # ifdef MODEM_DEBUG_PASSTHROUGH
74- Serial. print ( " passthrough, rx |>> " );
75- Serial. print (data_res. c_str () );
76- Serial. println (" <<| " );
72+ }
73+
74+ if (_serial_debug && _debug_level >= 2 ) {
75+ _serial_debug-> print (" ANSWER (passthrough): " );
76+ _serial_debug-> println (data_res. c_str () );
7777 if (res) {
78- Serial. println (" Result: OK" );
78+ _serial_debug-> println (" Result: OK" );
7979 }
8080 else {
81- Serial.println (" Result: FAILED" );
82- }
83- #endif
81+ _serial_debug->println (" Result: FAILED" );
82+ }
83+ }
84+
8485 return res;
8586}
8687
@@ -92,11 +93,13 @@ void ModemClass::write_nowait(const string &cmd, string &str, char * fmt, ...) {
9293 va_start (va, fmt);
9394 vsprintf ((char *)tx_buff, fmt, va);
9495 va_end (va);
95- #ifdef MODEM_DEBUG
96- Serial.print (" Write Call no wait, command sent: " );
97- Serial.write (tx_buff,strlen ((char *)tx_buff));
98- Serial.println ();
99- #endif
96+
97+ if (_serial_debug && _debug_level >= 2 ) {
98+ _serial_debug->print (" REQUEST (passthrough): " );
99+ _serial_debug->write (tx_buff,strlen ((char *)tx_buff));
100+ _serial_debug->println ();
101+ }
102+
100103 _serial->write (tx_buff,strlen ((char *)tx_buff));
101104 return ;
102105}
@@ -105,25 +108,22 @@ void ModemClass::write_nowait(const string &cmd, string &str, char * fmt, ...) {
105108/* -------------------------------------------------------------------------- */
106109bool ModemClass::write (const string &prompt, string &data_res, char * fmt, ...){
107110/* -------------------------------------------------------------------------- */
108- data_res.clear ();
109- memset (tx_buff,0x00 ,MAX_BUFF_SIZE);
110- va_list va;
111- va_start (va, fmt);
112- vsprintf ((char *)tx_buff, fmt, va);
113- va_end (va);
114- #ifdef MODEM_DEBUG
115- Serial.println ();
116- Serial.println (" ###>" );
117- Serial.print (" READ BY SIZE: " );
118- Serial.println ((int )read_by_size);
119- Serial.print (" Write Call, command sent: " );
120- Serial.write (tx_buff,strlen ((char *)tx_buff));
121- Serial.println ();
111+ data_res.clear ();
112+ memset (tx_buff,0x00 ,MAX_BUFF_SIZE);
113+ va_list va;
114+ va_start (va, fmt);
115+ vsprintf ((char *)tx_buff, fmt, va);
116+ va_end (va);
117+
118+ if (_serial_debug) {
119+ _serial_debug->println ();
120+ _serial_debug->print (" REQUEST: " );
121+ _serial_debug->write (tx_buff,strlen ((char *)tx_buff));
122+ _serial_debug->println ();
123+ }
122124
123- Serial.println (" <###" );
124- #endif
125- _serial->write (tx_buff,strlen ((char *)tx_buff));
126- return buf_read (prompt,data_res);;
125+ _serial->write (tx_buff,strlen ((char *)tx_buff));
126+ return buf_read (prompt,data_res);;
127127}
128128
129129
@@ -156,12 +156,22 @@ bool ModemClass::read_by_size_finished(string &rx) {
156156 int pos_space = rx.find (" " );
157157 if (pos != string::npos && pos_space != string::npos) {
158158 string n = rx.substr (pos_space,pos);
159- /* add 4 because OK\r\n is always added at the end of data */
160- data_to_be_received = atoi (n.c_str ()) + 4 ;
161- rx.clear ();
162- data_received = 0 ;
163- st = WAIT_FOR_DATA;
164-
159+ int to_be_rx = atoi (n.c_str ());
160+ if (to_be_rx <= 0 ) {
161+ while ( _serial->available () ){
162+ _serial->read ();
163+ }
164+ rv = true ;
165+ first_call = true ;
166+ st = IDLE;
167+ }
168+ else {
169+ /* add 4 because OK\r\n is always added at the end of data */
170+ data_to_be_received = to_be_rx + 4 ;
171+ data_received = 0 ;
172+ st = WAIT_FOR_DATA;
173+ }
174+ rx.clear ();
165175 }
166176 }
167177 break ;
@@ -190,23 +200,32 @@ bool ModemClass::buf_read(const string &prompt, string &data_res) {
190200 bool res = false ;
191201 bool found = false ;
192202
203+ if (_serial_debug && _debug_level >= 1 ) {
204+ _serial_debug->print (" RAW: " );
205+ }
206+
193207 unsigned long start_time = millis ();
194208 while ((millis () - start_time < _timeout) && !found){
195209 while ( _serial->available () ){
196210 char c = _serial->read ();
197211 data_res += c;
198- # ifdef SELECTABLE_MODEM_DEBUG
199- if (enable_dbg ) {
200- Serial. print (c);
212+
213+ if (_serial_debug && _debug_level >= 1 ) {
214+ _serial_debug-> print (c);
201215 }
202- # endif
216+
203217
204218 if (read_by_size) {
205219 if (read_by_size_finished (data_res)) {
206220 found = true ;
207221 read_by_size = false ;
208222 res = true ;
209- data_res = data_res.substr (0 , data_res.length () - (sizeof (RESULT_OK) - 1 ));
223+ if (data_res.size () > 0 ) {
224+ data_res = data_res.substr (0 , data_res.length () - (sizeof (RESULT_OK) - 1 ));
225+ }
226+ else {
227+ break ;
228+ }
210229 }
211230 }
212231 else {
@@ -249,18 +268,24 @@ bool ModemClass::buf_read(const string &prompt, string &data_res) {
249268 }
250269 trim_results = true ;
251270 read_by_size = false ;
252- #ifdef MODEM_DEBUG
253- Serial.print (" Write Call, response rx |>>" );
254- Serial.print (data_res.c_str ());
255- Serial.println (" <<|" );
271+
272+ if (_serial_debug && _debug_level >= 1 ) {
273+ _serial_debug->print (" <-RAW END" );
274+ _serial_debug->println ();
275+ }
276+
277+ if (_serial_debug) {
278+ _serial_debug->print (" ANSWER: " );
279+ _serial_debug->println (data_res.c_str ());
256280 if (res) {
257- Serial. println (" Result: OK" );
281+ _serial_debug-> println (" Result: OK" );
258282 }
259283 else {
260- Serial.println (" Result: FAILED" );
261- }
262- #endif
263-
284+ _serial_debug->println (" Result: FAILED" );
285+ }
286+ }
287+
288+
264289 return res;
265290}
266291
0 commit comments