Skip to content

Commit f8c9f97

Browse files
committed
UART Arduino section updated
1 parent e04da65 commit f8c9f97

File tree

1 file changed

+52
-12
lines changed
  • content/hardware/04.pro/carriers/portenta-hat-carrier/tutorials/user-manual

1 file changed

+52
-12
lines changed

content/hardware/04.pro/carriers/portenta-hat-carrier/tutorials/user-manual/content.md

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3554,34 +3554,74 @@ For Portenta H7 or C33, the following examples can be used to test UART communic
35543554
Serial.begin(9600);
35553555
```
35563556

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:
35583583

35593584
```arduino
3560-
// Variable for storing incoming data
35613585
String incoming = "";
35623586
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+
35633592
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()) {
35673594
delay(2);
3568-
char c = Serial.read();
3569-
3570-
// Check if the character is a newline (line-ending)
3595+
char c = Serial1.read();
35713596
if (c == '\n') {
3572-
// Process the received data
35733597
processData(incoming);
3574-
3575-
// Clear the incoming data string for the next message
35763598
incoming = "";
35773599
} else {
3578-
// Add the character to the incoming data string
35793600
incoming += c;
35803601
}
35813602
}
35823603
}
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+
}
35833621
```
35843622

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+
35853625
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.
35863626

35873627
```arduino

0 commit comments

Comments
 (0)