You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/hardware/04.pro/carriers/portenta-hat-carrier/tutorials/user-manual/content.md
+52-12Lines changed: 52 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3554,34 +3554,74 @@ For Portenta H7 or C33, the following examples can be used to test UART communic
3554
3554
Serial.begin(9600);
3555
3555
```
3556
3556
3557
-
With the `Serial.available()` function and the `Serial.read()` function, you can read incoming data by using a `while()` loop to repeatedly check for available data and read individual characters. When a line-ending character is received, the code above processes the input and stores the incoming characters in a String variable.:
3557
+
With the `Serial1.available()` function and the `Serial1.read()` function, you can read incoming data by using a `while()` loop to repeatedly check for available data and read individual characters. When a line-ending character is received, the code below processes the input and stores the incoming characters in a String variable.:
3558
+
3559
+
Using the `Serial1.available()` method alongside the `Serial1.read()` method allows you to read incoming data. The technique involves using a `while()` loop to consistently check for available data and then read individual characters.
3560
+
3561
+
Upon receiving a line-ending character, the code processes the accumulated characters and stores them in a _String_ variable. The relevant section of the code is presented below:
3562
+
3563
+
```arduino
3564
+
void loop() {
3565
+
while (Serial1.available()) {
3566
+
delay(2);
3567
+
char c = Serial1.read();
3568
+
if (c == '\n') {
3569
+
processData(incoming);
3570
+
incoming = "";
3571
+
} else {
3572
+
incoming += c;
3573
+
}
3574
+
}
3575
+
}
3576
+
3577
+
void processData(String data) {
3578
+
Serial.println("Received: " + data); // Print on Serial Monitor
3579
+
}
3580
+
```
3581
+
3582
+
Consequently, the following provides a complete example illustrating the reception of incoming data via UART:
3558
3583
3559
3584
```arduino
3560
-
// Variable for storing incoming data
3561
3585
String incoming = "";
3562
3586
3587
+
void setup() {
3588
+
Serial1.begin(9600); // For communication with Arduino using RX1 and TX1
3589
+
Serial.begin(9600); // For debugging over USB
3590
+
}
3591
+
3563
3592
void loop() {
3564
-
// Check for available data and read individual characters
3565
-
while (Serial.available()) {
3566
-
// Allow data buffering and read a single character
3593
+
while (Serial1.available()) {
3567
3594
delay(2);
3568
-
char c = Serial.read();
3569
-
3570
-
// Check if the character is a newline (line-ending)
3595
+
char c = Serial1.read();
3571
3596
if (c == '\n') {
3572
-
// Process the received data
3573
3597
processData(incoming);
3574
-
3575
-
// Clear the incoming data string for the next message
3576
3598
incoming = "";
3577
3599
} else {
3578
-
// Add the character to the incoming data string
3579
3600
incoming += c;
3580
3601
}
3581
3602
}
3582
3603
}
3604
+
3605
+
void processData(String data) {
3606
+
Serial.println("Received: " + data); // Print on Serial Monitor
3607
+
}
3608
+
```
3609
+
3610
+
For transmitting data over UART, which can complement the receiving board as depicted above, refer to the ensuing example:
3611
+
3612
+
```arduino
3613
+
void setup() {
3614
+
Serial1.begin(9600);
3615
+
}
3616
+
3617
+
void loop() {
3618
+
Serial1.println("Hello from Portenta!");
3619
+
delay(1000);
3620
+
}
3583
3621
```
3584
3622
3623
+
Using these codes, you should observe the message `"Hello from Portenta!"` being transmitted to the receiving Portenta board when coupled with a Portenta Hat Carrier.
3624
+
3585
3625
The `Serial.write()` method allows you to send data over UART (Universal Asynchronous Receiver-Transmitter). This method is used when you want to transmit raw bytes or send a byte array.
0 commit comments