53
53
https://minimalmodbus.readthedocs.io/en/stable/serialcommunication.html
54
54
*/
55
55
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
-
63
56
// global variable to keep the results from onReceive()
64
57
String uart_buffer = " " ;
65
58
// The Modbus RTU standard prescribes a silent period corresponding to 3.5 characters between each
@@ -71,42 +64,42 @@ const uint32_t baudrate = 19200;
71
64
// This is a callback function executed from a high priority monitor task
72
65
// All data will be buffered into RX Buffer, which may have its size set to whatever necessary
73
66
void UART0_RX_CB () {
74
- while (UART0 .available ()) {
75
- uart_buffer += (char )UART0 .read ();
67
+ while (Serial0 .available ()) {
68
+ uart_buffer += (char )Serial0 .read ();
76
69
}
77
70
}
78
71
79
72
// setup() and loop() are functions executed by a low priority task
80
73
// Therefore, there are 2 tasks running when using onReceive()
81
74
void setup () {
75
+ // Using Serial0 will work in any case (using or not USB CDC on Boot)
82
76
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
83
77
// UART_CLK_SRC_APB will allow higher values of RX Timeout
84
78
// default for ESP32 and ESP32-S2 is REF_TICK which limits the RX Timeout to 1
85
79
// setClockSource() must be called before begin()
86
- UART0 .setClockSource (UART_CLK_SRC_APB);
80
+ Serial0 .setClockSource (UART_CLK_SRC_APB);
87
81
#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)
91
85
// 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)
93
87
// 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" );
97
90
}
98
91
99
92
uint32_t counter = 0 ;
100
93
void loop () {
101
94
// String <uart_buffer> is filled by the UART Callback whenever data is received and RX Timeout occurs
102
95
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);
110
103
uart_buffer = " " ; // reset uart_buffer for the next UART reading
111
104
}
112
105
delay (1 );
0 commit comments