filmov
tv
Using Serial.read() with Arduino | Part 2

Показать описание
🤩 FREE Arduino Crash Course 👇👇
Want to learn more? Check out our courses!
***Get the code, transcript, challenges, etc for this lesson on our website***
We designed this circuit board for beginners!
SHOP OUR FAVORITE STUFF! (affiliate links)
---------------------------------------------------
Get your Free Trial of Altium PCB design Software
We use Rev Captions for our subtitles
Arduino UNO R3:
Budget Arduino Kits:
Multimeter Options:
Helping Hands:
Soldering Stations:
AFFILIATES & REFERRALS
---------------------------------------------------
FOLLOW US ELSEWHERE
---------------------------------------------------
Now let’s tackle the first step of our algorithm – we create a character array to hold the incoming message and a position variable to help us move through each element in the array. We’ll also create a constant to hold the max length of our message and use this to initialize the character array.
const unsigned int MAX_MESSAGE_LENGTH = 12;
Now we need to check to see if the byte we read is a terminating character or not… We can use an if-else statement for that. If it’s not a terminating character we’ll do one thing, and if it is a terminating character we’ll do something else.
Now if we do get the terminating character that means we have received our entire message and we can actually do something with the message or data we received. In this case we’ll print the message to the Serial Monitor window. We’ll also need to reset our character array to prepare for the next message.
Before we can call this complete, we need to enforce the max message length in the protocol. This will prevent us from exceeding the space that we allotted in our character array. We can add this guard to our existing if statement.
if ( inByte != '\n' && (message_pos - MAX_MESSAGE_LENGTH - 1) )
FULL SERIAL.READ() CODE
//Many thanks to Nick Gammon for the basis of this code
const unsigned int MAX_MESSAGE_LENGTH = 12;
void setup() {
}
void loop() {
//Check to see if anything is available in the serial receive buffer
{
//Create a place to hold the incoming message
static char message[MAX_MESSAGE_LENGTH];
static unsigned int message_pos = 0;
//Read the next available byte in the serial receive buffer
//Message coming in (check not terminating character) and guard for over message size
if ( inByte != '\n' && (message_pos - MAX_MESSAGE_LENGTH - 1) )
{
//Add the incoming byte to our message
message[message_pos] = inByte;
message_pos++;
}
//Full message received...
else
{
//Add null character to string
message[message_pos] = '\0';
//Print the message (or do other things)
//Reset for the next message
message_pos = 0;
}
}
}
OK. This feels like a ton – I know!
CONTINUED…
Want to learn more? Check out our courses!
***Get the code, transcript, challenges, etc for this lesson on our website***
We designed this circuit board for beginners!
SHOP OUR FAVORITE STUFF! (affiliate links)
---------------------------------------------------
Get your Free Trial of Altium PCB design Software
We use Rev Captions for our subtitles
Arduino UNO R3:
Budget Arduino Kits:
Multimeter Options:
Helping Hands:
Soldering Stations:
AFFILIATES & REFERRALS
---------------------------------------------------
FOLLOW US ELSEWHERE
---------------------------------------------------
Now let’s tackle the first step of our algorithm – we create a character array to hold the incoming message and a position variable to help us move through each element in the array. We’ll also create a constant to hold the max length of our message and use this to initialize the character array.
const unsigned int MAX_MESSAGE_LENGTH = 12;
Now we need to check to see if the byte we read is a terminating character or not… We can use an if-else statement for that. If it’s not a terminating character we’ll do one thing, and if it is a terminating character we’ll do something else.
Now if we do get the terminating character that means we have received our entire message and we can actually do something with the message or data we received. In this case we’ll print the message to the Serial Monitor window. We’ll also need to reset our character array to prepare for the next message.
Before we can call this complete, we need to enforce the max message length in the protocol. This will prevent us from exceeding the space that we allotted in our character array. We can add this guard to our existing if statement.
if ( inByte != '\n' && (message_pos - MAX_MESSAGE_LENGTH - 1) )
FULL SERIAL.READ() CODE
//Many thanks to Nick Gammon for the basis of this code
const unsigned int MAX_MESSAGE_LENGTH = 12;
void setup() {
}
void loop() {
//Check to see if anything is available in the serial receive buffer
{
//Create a place to hold the incoming message
static char message[MAX_MESSAGE_LENGTH];
static unsigned int message_pos = 0;
//Read the next available byte in the serial receive buffer
//Message coming in (check not terminating character) and guard for over message size
if ( inByte != '\n' && (message_pos - MAX_MESSAGE_LENGTH - 1) )
{
//Add the incoming byte to our message
message[message_pos] = inByte;
message_pos++;
}
//Full message received...
else
{
//Add null character to string
message[message_pos] = '\0';
//Print the message (or do other things)
//Reset for the next message
message_pos = 0;
}
}
}
OK. This feels like a ton – I know!
CONTINUED…
Комментарии