Skip to content

Commit a03c336

Browse files
committed
Remove String.toString(). Use setSocketReadCallbackPlus in the binary TCP example
1 parent 21cda94 commit a03c336

File tree

4 files changed

+108
-46
lines changed

4 files changed

+108
-46
lines changed

examples/SARA-R5_Example10_SocketPingPong/SARA-R5_Example10_SocketPingPong.ino

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,13 @@ void setup()
294294
IPAddress myAddress;
295295
mySARA.getNetworkAssignedIPAddress(0, &myAddress);
296296
Serial.print(F("\r\nMy IP Address is: "));
297-
Serial.println(myAddress.toString());
297+
Serial.print(myAddress[0]);
298+
Serial.print(F("."));
299+
Serial.print(myAddress[1]);
300+
Serial.print(F("."));
301+
Serial.print(myAddress[2]);
302+
Serial.print(F("."));
303+
Serial.println(myAddress[3]);
298304

299305
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
300306

@@ -392,8 +398,14 @@ void setup()
392398
}
393399
}
394400

395-
Serial.print(F("Remote address is "));
396-
Serial.println(theAddress.toString());
401+
Serial.print(F("Remote address is: "));
402+
Serial.print(theAddress[0]);
403+
Serial.print(F("."));
404+
Serial.print(theAddress[1]);
405+
Serial.print(F("."));
406+
Serial.print(theAddress[2]);
407+
Serial.print(F("."));
408+
Serial.println(theAddress[3]);
397409

398410
// Open the socket
399411
socketNum = mySARA.socketOpen(SARA_R5_TCP);
@@ -525,7 +537,13 @@ void printSocketParameters(int socket)
525537
IPAddress remoteAddress;
526538
int remotePort;
527539
mySARA.querySocketRemoteIPAddress(socket, &remoteAddress, &remotePort);
528-
Serial.println(remoteAddress.toString());
540+
Serial.print(remoteAddress[0]);
541+
Serial.print(F("."));
542+
Serial.print(remoteAddress[1]);
543+
Serial.print(F("."));
544+
Serial.print(remoteAddress[2]);
545+
Serial.print(F("."));
546+
Serial.println(remoteAddress[3]);
529547

530548
Serial.print(F("Socket status (TCP sockets only): "));
531549
SARA_R5_tcp_socket_status_t socketStatus;

examples/SARA-R5_Example10_SocketPingPong_BinaryTCP/SARA-R5_Example10_SocketPingPong_BinaryTCP.ino

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,14 @@ void processSocketListen(int listeningSocket, IPAddress localIP, unsigned int li
143143
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
144144

145145
// processSocketData is provided to the SARA-R5 library via a
146-
// callback setter -- setSocketReadCallback. (See setup())
147-
void processSocketData(int socket, String theData)
146+
// callback setter -- setSocketReadCallbackPlus. (See setup())
147+
void processSocketData(int socket, const char *theData, int dataLength, IPAddress remoteAddress, int remotePort)
148148
{
149149
Serial.println();
150150
Serial.print(F("Data received on socket "));
151151
Serial.print(socket);
152152
Serial.print(F(" :"));
153-
for (int i = 0; i < theData.length(); i++)
153+
for (int i = 0; i < dataLength; i++)
154154
{
155155
Serial.print(F(" 0x"));
156156
if (theData[i] < 16)
@@ -168,18 +168,10 @@ void processSocketData(int socket, String theData)
168168

169169
if ((theData[0] == 0x04) && (theData[1] == 0x05) && (theData[2] == 0x06) && (theData[3] == 0x07)) // Look for the "Pong"
170170
{
171-
// Use the const char * version
172-
//const char ping[] = { 0x00, 0x01, 0x02, 0x03 };
173-
//mySARA.socketWrite(socket, ping, 4); // Send the "Ping"
174-
175-
// Or use the String version. Both are OK for binary data.
176-
String ping = "";
177-
ping.concat('\0'); // Construct the ping in a binary-friendly way
178-
ping.concat('\1');
179-
ping.concat('\2');
180-
ping.concat('\3');
181-
mySARA.socketWrite(socket, ping); // Send the "Ping"
182-
171+
// Use the const char * version for binary data
172+
const char ping[] = { 0x00, 0x01, 0x02, 0x03 };
173+
mySARA.socketWrite(socket, ping, 4); // Send the "Ping"
174+
183175
pingCount++;
184176
}
185177

@@ -215,13 +207,13 @@ void processPSDAction(int result, IPAddress ip)
215207
if (result == 0)
216208
Serial.print(F(" (success)"));
217209
Serial.print(F(" IP Address: \""));
218-
Serial.print(String(ip[0]));
210+
Serial.print(ip[0]);
219211
Serial.print(F("."));
220-
Serial.print(String(ip[1]));
212+
Serial.print(ip[1]);
221213
Serial.print(F("."));
222-
Serial.print(String(ip[2]));
214+
Serial.print(ip[2]);
223215
Serial.print(F("."));
224-
Serial.print(String(ip[3]));
216+
Serial.print(ip[3]);
225217
Serial.println(F("\""));
226218
}
227219

@@ -311,7 +303,13 @@ void setup()
311303
IPAddress myAddress;
312304
mySARA.getNetworkAssignedIPAddress(0, &myAddress);
313305
Serial.print(F("\r\nMy IP Address is: "));
314-
Serial.println(myAddress.toString());
306+
Serial.print(myAddress[0]);
307+
Serial.print(F("."));
308+
Serial.print(myAddress[1]);
309+
Serial.print(F("."));
310+
Serial.print(myAddress[2]);
311+
Serial.print(F("."));
312+
Serial.println(myAddress[3]);
315313

316314
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
317315

@@ -321,7 +319,7 @@ void setup()
321319
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
322320

323321
// Set a callback to process the socket data
324-
mySARA.setSocketReadCallback(&processSocketData);
322+
mySARA.setSocketReadCallbackPlus(&processSocketData);
325323

326324
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
327325

@@ -409,8 +407,14 @@ void setup()
409407
}
410408
}
411409

412-
Serial.print(F("Remote address is "));
413-
Serial.println(theAddress.toString());
410+
Serial.print(F("Remote address is: "));
411+
Serial.print(theAddress[0]);
412+
Serial.print(F("."));
413+
Serial.print(theAddress[1]);
414+
Serial.print(F("."));
415+
Serial.print(theAddress[2]);
416+
Serial.print(F("."));
417+
Serial.println(theAddress[3]);
414418

415419
// Open the socket
416420
socketNum = mySARA.socketOpen(SARA_R5_TCP);
@@ -541,7 +545,13 @@ void printSocketParameters(int socket)
541545
IPAddress remoteAddress;
542546
int remotePort;
543547
mySARA.querySocketRemoteIPAddress(socket, &remoteAddress, &remotePort);
544-
Serial.println(remoteAddress.toString());
548+
Serial.print(remoteAddress[0]);
549+
Serial.print(F("."));
550+
Serial.print(remoteAddress[1]);
551+
Serial.print(F("."));
552+
Serial.print(remoteAddress[2]);
553+
Serial.print(F("."));
554+
Serial.println(remoteAddress[3]);
545555

546556
Serial.print(F("Socket status (TCP sockets only): "));
547557
SARA_R5_tcp_socket_status_t socketStatus;

examples/SARA-R5_Example10_SocketPingPong_MultipleTCP/SARA-R5_Example10_SocketPingPong_MultipleTCP.ino

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,13 @@ void setup()
320320
IPAddress myAddress;
321321
mySARA.getNetworkAssignedIPAddress(0, &myAddress);
322322
Serial.print(F("\r\nMy IP Address is: "));
323-
Serial.println(myAddress.toString());
323+
Serial.print(myAddress[0]);
324+
Serial.print(F("."));
325+
Serial.print(myAddress[1]);
326+
Serial.print(F("."));
327+
Serial.print(myAddress[2]);
328+
Serial.print(F("."));
329+
Serial.println(myAddress[3]);
324330

325331
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
326332

@@ -419,7 +425,13 @@ void setup()
419425
}
420426

421427
Serial.print(F("Remote address is "));
422-
Serial.println(theAddress.toString());
428+
Serial.print(theAddress[0]);
429+
Serial.print(F("."));
430+
Serial.print(theAddress[1]);
431+
Serial.print(F("."));
432+
Serial.print(theAddress[2]);
433+
Serial.print(F("."));
434+
Serial.println(theAddress[3]);
423435

424436
// Open the sockets
425437
for (int i = 0; i < numConnections; i++)
@@ -569,7 +581,13 @@ void printSocketParameters(int socket)
569581
IPAddress remoteAddress;
570582
int remotePort;
571583
mySARA.querySocketRemoteIPAddress(socket, &remoteAddress, &remotePort);
572-
Serial.println(remoteAddress.toString());
584+
Serial.print(remoteAddress[0]);
585+
Serial.print(F("."));
586+
Serial.print(remoteAddress[1]);
587+
Serial.print(F("."));
588+
Serial.print(remoteAddress[2]);
589+
Serial.print(F("."));
590+
Serial.println(remoteAddress[3]);
573591

574592
Serial.print(F("Socket status (TCP sockets only): "));
575593
SARA_R5_tcp_socket_status_t socketStatus;

examples/SARA-R5_Example10_SocketPingPong_MultipleUDP/SARA-R5_Example10_SocketPingPong_MultipleUDP.ino

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,13 @@ void processSocketData(int socket, const char *theData, int length, IPAddress re
121121
Serial.print(F("Data received on socket "));
122122
Serial.print(socket);
123123
Serial.print(F(" from IP "));
124-
Serial.print(remoteAddress.toString());
124+
Serial.print(remoteAddress[0]);
125+
Serial.print(F("."));
126+
Serial.print(remoteAddress[1]);
127+
Serial.print(F("."));
128+
Serial.print(remoteAddress[2]);
129+
Serial.print(F("."));
130+
Serial.print(remoteAddress[3]);
125131
Serial.print(F(" using port "));
126132
Serial.print(remotePort);
127133
Serial.print(F(" :"));
@@ -148,18 +154,10 @@ void processSocketData(int socket, const char *theData, int length, IPAddress re
148154
{
149155
if (pingCount[connection] < pingPongLimit)
150156
{
151-
// Use the const char * version
152-
//const char ping[] = { 0x00, 0x01, 0x02, 0x03 };
153-
//mySARA.socketWriteUDP(socket, remoteAddress, remotePort, ping, 4); // Send the "Ping"
157+
// Use the const char * version for binary data
158+
const char ping[] = { 0x00, 0x01, 0x02, 0x03 };
159+
mySARA.socketWriteUDP(socket, remoteAddress, remotePort, ping, 4); // Send the "Ping"
154160

155-
// Or use the String version. Both are OK for binary data.
156-
String ping = "";
157-
ping.concat('\0'); // Construct the ping in a binary-friendly way
158-
ping.concat('\1');
159-
ping.concat('\2');
160-
ping.concat('\3');
161-
mySARA.socketWriteUDP(socket, remoteAddress.toString(), remotePort, ping); // Send the "Ping"
162-
163161
pingCount[connection]++;
164162
}
165163
}
@@ -196,7 +194,13 @@ void processPSDAction(int result, IPAddress ip)
196194
if (result == 0)
197195
Serial.print(F(" (success)"));
198196
Serial.print(F(" IP Address: \""));
199-
Serial.print(ip.toString());
197+
Serial.print(ip[0]);
198+
Serial.print(F("."));
199+
Serial.print(ip[1]);
200+
Serial.print(F("."));
201+
Serial.print(ip[2]);
202+
Serial.print(F("."));
203+
Serial.print(ip[3]);
200204
Serial.println(F("\""));
201205
}
202206

@@ -292,7 +296,13 @@ void setup()
292296
IPAddress myAddress;
293297
mySARA.getNetworkAssignedIPAddress(0, &myAddress);
294298
Serial.print(F("\r\nMy IP Address is: "));
295-
Serial.println(myAddress.toString());
299+
Serial.print(myAddress[0]);
300+
Serial.print(F("."));
301+
Serial.print(myAddress[1]);
302+
Serial.print(F("."));
303+
Serial.print(myAddress[2]);
304+
Serial.print(F("."));
305+
Serial.println(myAddress[3]);
296306

297307
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
298308

@@ -344,7 +354,13 @@ void setup()
344354
}
345355

346356
Serial.print(F("Remote address is "));
347-
Serial.println(theAddress.toString());
357+
Serial.print(theAddress[0]);
358+
Serial.print(F("."));
359+
Serial.print(theAddress[1]);
360+
Serial.print(F("."));
361+
Serial.print(theAddress[2]);
362+
Serial.print(F("."));
363+
Serial.println(theAddress[3]);
348364

349365
// Open the sockets
350366
for (int i = 0; i < numConnections; i++)

0 commit comments

Comments
 (0)