5353 https://minimalmodbus.readthedocs.io/en/stable/serialcommunication.html
5454*/
5555
56- // this will make UART0 work in any case (using or not USB)
57- #if ARDUINO_USB_CDC_ON_BOOT
58- #define UART0 Serial0
59- #else
60- #define UART0 Serial
61- #endif
62-
6356// global variable to keep the results from onReceive()
6457String uart_buffer = " " ;
6558// The Modbus RTU standard prescribes a silent period corresponding to 3.5 characters between each
@@ -71,42 +64,42 @@ const uint32_t baudrate = 19200;
7164// This is a callback function executed from a high priority monitor task
7265// All data will be buffered into RX Buffer, which may have its size set to whatever necessary
7366void UART0_RX_CB () {
74- while (UART0 .available ()) {
75- uart_buffer += (char )UART0 .read ();
67+ while (Serial0 .available ()) {
68+ uart_buffer += (char )Serial0 .read ();
7669 }
7770}
7871
7972// setup() and loop() are functions executed by a low priority task
8073// Therefore, there are 2 tasks running when using onReceive()
8174void setup () {
75+ // Using Serial0 will work in any case (using or not USB CDC on Boot)
8276#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
8377 // UART_CLK_SRC_APB will allow higher values of RX Timeout
8478 // default for ESP32 and ESP32-S2 is REF_TICK which limits the RX Timeout to 1
8579 // setClockSource() must be called before begin()
86- UART0 .setClockSource (UART_CLK_SRC_APB);
80+ Serial0 .setClockSource (UART_CLK_SRC_APB);
8781#endif
88- // the amount of data received or waiting to be processed shall not exceed this limit ( 1024 bytes)
89- UART0 .setRxBufferSize (1024 ); // default is 256 bytes
90- UART0 .begin (baudrate); // default pins and default mode 8N1 (8 bits data, no parity bit, 1 stopbit)
82+ // the amount of data received or waiting to be proessed shall not exceed this limit of 1024 bytes
83+ Serial0 .setRxBufferSize (1024 ); // default is 256 bytes
84+ Serial0 .begin (baudrate); // default pins and default mode 8N1 (8 bits data, no parity bit, 1 stopbit)
9185 // set RX Timeout based on UART symbols ~ 3.5 symbols of 11 bits (MODBUS standard) ~= 2 ms at 19200
92- UART0 .setRxTimeout (modbusRxTimeoutLimit); // 4 symbols at 19200 8N1 is about 2.08 ms (40 bits)
86+ Serial0 .setRxTimeout (modbusRxTimeoutLimit); // 4 symbols at 19200 8N1 is about 2.08 ms (40 bits)
9387 // sets the callback function that will be executed only after RX Timeout
94- UART0.onReceive (UART0_RX_CB, true );
95-
96- UART0.println (" Send data to UART0 in order to activate the RX callback" );
88+ Serial0.onReceive (UART0_RX_CB, true );
89+ Serial0.println (" Send data using Serial Monitor in order to activate the RX callback" );
9790}
9891
9992uint32_t counter = 0 ;
10093void loop () {
10194 // String <uart_buffer> is filled by the UART Callback whenever data is received and RX Timeout occurs
10295 if (uart_buffer.length () > 0 ) {
103- // process the received data from UART0 - example, just print it beside a counter
104- UART0 .print (" [" );
105- UART0 .print (counter++);
106- UART0 .print (" ] [" );
107- UART0 .print (uart_buffer.length ());
108- UART0 .print (" bytes] " );
109- UART0 .println (uart_buffer);
96+ // process the received data from Serial - example, just print it beside a counter
97+ Serial0 .print (" [" );
98+ Serial0 .print (counter++);
99+ Serial0 .print (" ] [" );
100+ Serial0 .print (uart_buffer.length ());
101+ Serial0 .print (" bytes] " );
102+ Serial0 .println (uart_buffer);
110103 uart_buffer = " " ; // reset uart_buffer for the next UART reading
111104 }
112105 delay (1 );
0 commit comments