Reception of commands through a serial port does not work

Good day! There is a code that must accept commands through (UART)

#define RAPI_BUFLEN 13
int LAMP_STATE = 0;


class RAPI {
  int m_RAPIinByte; // RAPI byte being read in
  char m_RAPIinstr[RAPI_BUFLEN]; // RAPI byte being read in
  int m_RAPIstrCount; //RAPI string counter

public:
  RAPI();
  void Init();
 
  void flush() {
    Serial.flush();
  }
  void getInput();
  uint8_t getInt();
};

RAPI g_RAPI;

RAPI::RAPI()
{
  m_RAPIstrCount = 0;
}

void RAPI::Init()
{
g_RAPI.flush();

}

uint8_t RAPI::getInt()
{
  uint8_t c;
  uint8_t num = 0;

  do {
    c = Serial.read(); // read the byte
    if ((c >= '0') && (c <= '9')) {
      num = (num * 10) + c - '0';
    }
  } while (c != 13);
  return num;
}

void RAPI::getInput()
{
  if(Serial.available()) { // if byte(s) are available to be read
    m_RAPIinByte = Serial.read(); // read the byte
    Serial.print(char(m_RAPIinByte));
    digitalWrite(13, HIGH);
    if(m_RAPIinByte != 13) {
     m_RAPIinstr[m_RAPIstrCount] = char(m_RAPIinByte);
      m_RAPIstrCount++;
    }
    if(m_RAPIinByte == 13) {
      if (strcmp(m_RAPIinstr, "$ST 1") == 0){ //if match
        LAMP_STATE = 1;
          
      }
      else if (strcmp(m_RAPIinstr, "$ST 2") == 0) { // string compare
        LAMP_STATE = 2;
      }
     
      g_RAPI.flush();
      m_RAPIstrCount = 0; // get ready for new input... reset strCount
      m_RAPIinByte = 0; // reset the inByte variable
      digitalWrite(13, LOW);
      for(int i = 0; m_RAPIinstr[i] != '\0'; i++) { // while the string does not have null
        m_RAPIinstr[i] = '\0'; // fill it with null to erase it
      }
    } 
}
}


void setup() {
  pinMode(9, OUTPUT);
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}

void loop() {
 
{
g_RAPI.getInput();
} 
 
  if(LAMP_STATE == 0) {
    digitalWrite(9, LOW); 
  }
 
  if (LAMP_STATE == 1) {
    digitalWrite(9, HIGH);
  }
   if (LAMP_STATE == 2) {
  digitalWrite(9, LOW);
  }

}

I load the sketch, open the serial port and enter commands like $ ST 1 / $ ST 2, but no action occurs, the LED on 9 pin should light up. On the UNO, during the command input, RX / TX flashes. What is the problem?

{
g_RAPI.getInput();
}

{Useless} {curly} {braces}.

Piss-poor indenting.

      g_RAPI.flush();

What do you think that is doing? Blocking until the outgoing serial data buffer is empty, when you never write anything to the outgoing buffer, is silly.

      for(int i = 0; m_RAPIinstr[i] != '\0'; i++) { // while the string does not have null
        m_RAPIinstr[i] = '\0'; // fill it with null to erase it
      }

You CLEARLY do not understand that NULL-terminated means.

  int m_RAPIinByte; // RAPI byte being read in

It is stupid to use a type (Byte) in the name of a variable that has a different type.

I load the sketch, open the serial port and enter commands like $ ST 1 / $ ST 2, but no action occurs

Your code does not expect spaces between the $ and the ST.

PaulS:

{

g_RAPI.getInput();
}



{Useless} {curly} {braces}.

Piss-poor indenting.



g_RAPI.flush();



What do you think that is doing? Blocking until the outgoing serial data buffer is empty, when you never write anything to the outgoing buffer, is silly.



for(int i = 0; m_RAPIinstr[i] != '\0'; i++) { // while the string does not have null
        m_RAPIinstr[i] = '\0'; // fill it with null to erase it
      }



You CLEARLY do not understand that NULL-terminated means.



int m_RAPIinByte; // RAPI byte being read in



It is stupid to use a type (Byte) in the name of a variable that has a different type.
Your code does not expect spaces between the $ and the ST.

PaulS, thanks for criticizing the code! I took it from the Internet and decided to process the RAPI commands. Now I will try to make changes

When interacting with the Serial Monitor, I haven't found flush of serial port to be necessary.

You really don't need a fancy class to do what you are doing.
Keep reading bytes until you get CR character, then do char array compare.

ieee488:
When interacting with the Serial Monitor, I haven’t found flush of serial port to be necessary.

You really don’t need a fancy class to do what you are doing.
Keep reading bytes until you get CR character, then do char array compare.

According to the idea there will be several Arduino UNO, connected on UART and exchanging commands. Now I decided for the sake of experiment to replace one Arduino with the Arduino IDE Port Monitor and from there enter the commands.

Serial Input Basics.

Hi,
Welcome to the forum.

According to the idea there will be several Arduino UNO, connected on UART and exchanging commands.

What idea, can you tell us what the application is?

If you need several UNO connected, they will all need separate UART ports.

Can you please tell us your electronics, programming, Arduino, hardware experience?

Thanks.. Tom.. :slight_smile:

TomGeorge:
Hi,
Welcome to the forum.

What idea, can you tell us what the application is?

If you need several UNO connected, they will all need separate UART ports.

Can you please tell us your electronics, programming, Arduino, hardware experience?

Thanks.. Tom.. :slight_smile:

Hi, Tom :slight_smile:
I plan to use two Arduno UNO and ESP8266. # 1 Arduino UNO will communicate with ESP8266 on UART, and # 2 Arduino UNO on I2C.