@@ -33,17 +33,20 @@ 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
4039 **************************************************************************************/
4140
42- auto const tc6_io = new TC6::TC6_Io
43- ( SPI
44- , CS_PIN
45- , RESET_PIN
46- , IRQ_PIN);
41+ auto const tc6_io = new TC6::TC6_Io(
42+ #ifdef ARDUINO_GIGA
43+ SPI1
44+ #else
45+ SPI
46+ #endif
47+ , CS_PIN
48+ , RESET_PIN
49+ , IRQ_PIN);
4750auto const tc6_inst = new TC6::TC6_Arduino_10BASE_T1S(tc6_io);
4851Arduino_10BASE_T1S_UDP udp_server;
4952
@@ -119,34 +122,52 @@ void loop()
119122 }
120123
121124 /* Check for incoming UDP packets. */
122- int const packet_size = udp_server.parsePacket ();
123- if (packet_size )
125+ int const rx_packet_size = udp_server.parsePacket ();
126+ if (rx_packet_size )
124127 {
125- /* Receive incoming UDP packets. */
126- Serial.print (" Received " );
127- Serial.print (packet_size);
128+ std::vector<uint8_t > udp_tx_buf;
129+ IPAddress const destination_ip = udp_server.remoteIP ();
130+ uint16_t const destination_port = udp_server.remotePort ();
131+
132+ /* Print some metadata from received UDP packet. */
133+ Serial.print (" [" );
134+ Serial.print (millis ());
135+ Serial.print (" ] Received " );
136+ Serial.print (rx_packet_size);
128137 Serial.print (" bytes from " );
129138 Serial.print (udp_server.remoteIP ());
130139 Serial.print (" port " );
131140 Serial.print (udp_server.remotePort ());
132- Serial.println ();
133-
134- int const bytes_read = udp_server.read (udp_rx_msg_buf, sizeof (udp_rx_msg_buf));
135- if (bytes_read > 0 ) {
136- udp_rx_msg_buf[bytes_read] = 0 ;
141+ Serial.print (" , data = \" " );
142+
143+ /* Read from received UDP packet. */
144+ size_t const UDP_RX_MSG_BUF_SIZE = 16 + 1 ; /* Reserve the last byte for the '\0' termination. */
145+ uint8_t udp_rx_msg_buf[UDP_RX_MSG_BUF_SIZE] = {0 };
146+ int bytes_read = udp_server.read (udp_rx_msg_buf, UDP_RX_MSG_BUF_SIZE - 1 );
147+ while (bytes_read != 0 )
148+ {
149+ /* Copy received data into transmit buffer for echo functionality. */
150+ std::copy (udp_rx_msg_buf, udp_rx_msg_buf + bytes_read, std::back_inserter (udp_tx_buf));
151+
152+ /* Print received data to Serial. */
153+ udp_rx_msg_buf[bytes_read] = ' \0 ' ; /* Terminate buffer so that we can print it as a C-string. */
154+ Serial.print (reinterpret_cast <char *>(udp_rx_msg_buf));
155+
156+ /* Continue reading. */
157+ bytes_read = udp_server.read (udp_rx_msg_buf, UDP_RX_MSG_BUF_SIZE - 1 );
137158 }
138- Serial.print (" UDP_Server received packet content: \" " );
139- Serial.print (reinterpret_cast <char *>(udp_rx_msg_buf));
140159 Serial.println (" \" " );
141160
161+ /* Finish reading the current packet. */
162+ udp_server.flush ();
163+
142164 /* Send back a reply, to the IP address and port we got the packet from. */
143- udp_server.beginPacket (udp_server. remoteIP (), udp_server. remotePort () );
144- udp_server.write (( const uint8_t *)udp_rx_msg_buf, packet_size );
165+ udp_server.beginPacket (destination_ip, destination_port );
166+ udp_server.write (udp_tx_buf. data (), udp_tx_buf. size () );
145167 udp_server.endPacket ();
146168 }
147169}
148170
149-
150171static void OnPlcaStatus (bool success, bool plcaStatus)
151172{
152173 if (!success)
0 commit comments