@@ -33,7 +33,6 @@ static T1SPlcaSettings const t1s_plca_settings{T1S_PLCA_NODE_ID};
3333static T1SMacSettings const t1s_default_mac_settings;
3434
3535static uint16_t const UDP_SERVER_LOCAL_PORT = 8888 ;
36- static uint8_t udp_rx_msg_buf[256 ] = {0 };
3736
3837/* *************************************************************************************
3938 * GLOBAL VARIABLES
@@ -119,34 +118,50 @@ void loop()
119118 }
120119
121120 /* 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 )
124123 {
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+
125128 /* Print some metadata from received UDP packet. */
126129 Serial.print (" Received " );
127- Serial.print (packet_size );
130+ Serial.print (rx_packet_size );
128131 Serial.print (" bytes from " );
129132 Serial.print (udp_server.remoteIP ());
130133 Serial.print (" port " );
131134 Serial.print (udp_server.remotePort ());
132135 Serial.println ();
133136
137+ Serial.print (" [" );
138+ Serial.print (millis ());
139+ Serial.print (" ] UDP_Client received packet content: \" " );
140+
134141 /* 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 );
138156 }
157+ Serial.println (" \" " );
139158
140159 /* Finish reading the current packet. */
141160 udp_server.flush ();
142161
143- Serial.print (" UDP_Server received packet content: \" " );
144- Serial.print (reinterpret_cast <char *>(udp_rx_msg_buf));
145- Serial.println (" \" " );
146-
147162 /* 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 () );
150165 udp_server.endPacket ();
151166 }
152167}
0 commit comments