@@ -33,7 +33,6 @@ static T1SPlcaSettings const t1s_plca_settings{T1S_PLCA_NODE_ID};
33
33
static T1SMacSettings const t1s_default_mac_settings;
34
34
35
35
static uint16_t const UDP_SERVER_LOCAL_PORT = 8888 ;
36
- static uint8_t udp_rx_msg_buf[256 ] = {0 };
37
36
38
37
/* *************************************************************************************
39
38
* GLOBAL VARIABLES
@@ -119,34 +118,50 @@ void loop()
119
118
}
120
119
121
120
/* Check for incoming UDP packets. */
122
- int const packet_size = udp_server.parsePacket ();
123
- if (packet_size )
121
+ int const rx_packet_size = udp_server.parsePacket ();
122
+ if (rx_packet_size )
124
123
{
124
+ std::vector<uint8_t > udp_tx_buf (rx_packet_size);
125
+ IPAddress const destination_ip = udp_server.remoteIP ();
126
+ uint16_t const destination_port = udp_server.remotePort ();
127
+
125
128
/* Print some metadata from received UDP packet. */
126
129
Serial.print (" Received " );
127
- Serial.print (packet_size );
130
+ Serial.print (rx_packet_size );
128
131
Serial.print (" bytes from " );
129
132
Serial.print (udp_server.remoteIP ());
130
133
Serial.print (" port " );
131
134
Serial.print (udp_server.remotePort ());
132
135
Serial.println ();
133
136
137
+ Serial.print (" [" );
138
+ Serial.print (millis ());
139
+ Serial.print (" ] UDP_Client received packet content: \" " );
140
+
134
141
/* Read from received UDP packet. */
135
- int const bytes_read = udp_server.read (udp_rx_msg_buf, sizeof (udp_rx_msg_buf));
136
- if (bytes_read > 0 ) {
137
- udp_rx_msg_buf[bytes_read] = 0 ;
142
+ size_t const UDP_RX_MSG_BUF_SIZE = 16 + 1 ; /* Reserve the last byte for the '\0' termination. */
143
+ uint8_t udp_rx_msg_buf[UDP_RX_MSG_BUF_SIZE] = {0 };
144
+ int bytes_read = udp_server.read (udp_rx_msg_buf, UDP_RX_MSG_BUF_SIZE - 1 );
145
+ while (bytes_read != 0 )
146
+ {
147
+ /* Copy received data into transmit buffer for echo functionality. */
148
+ std::copy (udp_rx_msg_buf, udp_rx_msg_buf + bytes_read, std::back_inserter (udp_tx_buf));
149
+
150
+ /* Print received data to Serial. */
151
+ udp_rx_msg_buf[bytes_read] = ' \0 ' ; /* Terminate buffer so that we can print it as a C-string. */
152
+ Serial.print (reinterpret_cast <char *>(udp_rx_msg_buf));
153
+
154
+ /* Continue reading. */
155
+ bytes_read = udp_server.read (udp_rx_msg_buf, UDP_RX_MSG_BUF_SIZE - 1 );
138
156
}
157
+ Serial.println (" \" " );
139
158
140
159
/* Finish reading the current packet. */
141
160
udp_server.flush ();
142
161
143
- Serial.print (" UDP_Server received packet content: \" " );
144
- Serial.print (reinterpret_cast <char *>(udp_rx_msg_buf));
145
- Serial.println (" \" " );
146
-
147
162
/* Send back a reply, to the IP address and port we got the packet from. */
148
- udp_server.beginPacket (udp_server. remoteIP (), udp_server. remotePort () );
149
- udp_server.write (( const uint8_t *)udp_rx_msg_buf, packet_size );
163
+ udp_server.beginPacket (destination_ip, destination_port );
164
+ udp_server.write (udp_tx_buf. data (), udp_tx_buf. size () );
150
165
udp_server.endPacket ();
151
166
}
152
167
}
0 commit comments