@@ -33,17 +33,20 @@ 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
40
39
**************************************************************************************/
41
40
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);
47
50
auto const tc6_inst = new TC6::TC6_Arduino_10BASE_T1S(tc6_io);
48
51
Arduino_10BASE_T1S_UDP udp_server;
49
52
@@ -119,34 +122,52 @@ void loop()
119
122
}
120
123
121
124
/* 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 )
124
127
{
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);
128
137
Serial.print (" bytes from " );
129
138
Serial.print (udp_server.remoteIP ());
130
139
Serial.print (" port " );
131
140
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 );
137
158
}
138
- Serial.print (" UDP_Server received packet content: \" " );
139
- Serial.print (reinterpret_cast <char *>(udp_rx_msg_buf));
140
159
Serial.println (" \" " );
141
160
161
+ /* Finish reading the current packet. */
162
+ udp_server.flush ();
163
+
142
164
/* 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 () );
145
167
udp_server.endPacket ();
146
168
}
147
169
}
148
170
149
-
150
171
static void OnPlcaStatus (bool success, bool plcaStatus)
151
172
{
152
173
if (!success)
0 commit comments