Hello I am in need of some guidance to optimize serial receive. Currently I am just prototyping at the moment so apologies for the rough messy code. This method I am using currently blocks the Arduino while it process the command.
I know that this is not the best way to parse ASCII serial messages but it was the most simplistic method I could think of at the time.
Basically I have a C# app that is sending commands like #A100.0 this would set the acceleration but in this current mess it takes the Arduino about a second to processes that while it also blocks everything else from running the command are only sent when needed so it's not constantly receiving.
Any advice would be helpful.
void pcsyncin () {
if (Serial.available() >= 2)
{
if (Serial.read() == '#')
{
int command = Serial.read();
if (command == 'U') {
user = Serial.parseInt();
}
else if (command == 'F')
{
getgtfloor = Serial.parseInt();
flrchng = true;
srun = false;
lcdgtfloordsp();
}
else if (command == 'L')
{
lcdlight = Serial.parseInt();
digitalWrite(backLight, lcdlight);
}
else if (command == 'S')
{
MaxElevatorSpeed = Serial.parseFloat();
}
else if (command == 'A')
{
MaxElevatorAcceleration = Serial.parseFloat();
}
else if (command == 'H')
{
hault();
}
else if (command == 'P')
{
liftpreci = Serial.parseInt();
}
}
}
}
If/else if/else if/else if code is both harder to read and slower to execute than a switch statement. I would rewrite your code that matches the first character after the "#" to use a switch statement.
However, even with your long if statement, the code you posted should take milliseconds to parse your command, not a second.
What baud rate are you using? And what do the functions you call like "parseFloat()" do?
Callum:
Hello I am in need of some guidance to optimize serial receive. Currently I am just prototyping at the moment so apologies for the rough messy code. This method I am using currently blocks the Arduino while it process the command.
I know that this is not the best way to parse ASCII serial messages but it was the most simplistic method I could think of at the time.
Basically I have a C# app that is sending commands like #A100.0 this would set the acceleration but in this current mess it takes the Arduino about a second to processes that while it also blocks everything else from running the command are only sent when needed so it's not constantly receiving.
Any advice would be helpful.
void pcsyncin () {
if (Serial.available() >= 2)
{
if (Serial.read() == '#')
{
int command = Serial.read();
if (command == 'U'){
user = Serial.parseInt();
}
else if (command == 'F')
{
getgtfloor = Serial.parseInt();
flrchng = true;
srun = false;
lcdgtfloordsp();
}
else if (command == 'L')
{
lcdlight = Serial.parseInt();
digitalWrite(backLight, lcdlight);
}
else if (command == 'S')
{
MaxElevatorSpeed = Serial.parseFloat();
This function is called in the main loop. I understand that it should only take milliseconds to execute and I do know that a case statement is faster but wouldn't the difference between using else if and a case be negligible but what I can't understand that if this only takes milliseconds for the Arduino to execute why is this blocking it for at least one second. Also serial parse int and serial parse float are functions built into the base Arduino boot-loader. See: Serial.parseInt() - Arduino Reference. It basically scans for a integer or floating point number in the serial buffer and reads it out of the buffer.
Baud rate is at 115200 it's working fine and the code does what it's supposed to do however it blocks everything else while it processes it.
Code formatted.
void pcsyncin () {
if (Serial.available() >= 2)
{
if (Serial.read() == '#')
{
int command = Serial.read();
if (command == 'U') {
user = Serial.parseInt();
}
else if (command == 'F')
{
getgtfloor = Serial.parseInt();
flrchng = true;
srun = false;
lcdgtfloordsp();
}
else if (command == 'L')
{
lcdlight = Serial.parseInt();
digitalWrite(backLight, lcdlight);
}
else if (command == 'S')
{
MaxElevatorSpeed = Serial.parseFloat();
}
else if (command == 'A')
{
MaxElevatorAcceleration = Serial.parseFloat();
}
else if (command == 'H')
{
hault();
}
else if (command == 'P')
{
liftpreci = Serial.parseInt();
}
}
}
}